Simple Pendulum Project

I have a project.This is very important to me.I have similar code to this project but this code is too hard and not understanding to me.I want to very easy code because this project wants to be some calculation and simple pendulum.Details:

I PREPARE THIS CODE:

void setup() {
 size(1000,900);
}
float t = 0;
void draw() {
  float g = 9.8;
  float l = 1;
  float w = sqrt(g/l);
  float theta0 = 1.5;
  float theta = theta0 * sin(w*t);
  println(theta);
  t += 0.1;
}

AND I FIND THIS CODE:

Pendulum p;
void setup() {
  size(1000,950);
  // Make a new Pendulum with an origin position and armlength
  p = new Pendulum(new PVector(width/2,500),300);
}
void draw() {
  background(255);
  p.go();  
}
void mousePressed() {
  p.clicked(mouseX,mouseY);
}
void mouseReleased() {
  p.stopDragging();
}
class Pendulum {
  PVector position;    // position of pendulum ball
  PVector origin;      // position of arm origin
  float L;             // Length of length
  float angle;         // Pendulum arm angle
  float aVelocity;     // Angle velocity
  float aAcceleration; // Angle acceleration
  float ballr;         // Ball radius
  float damping;       // Arbitary damping amount

  boolean dragging = false;

  Pendulum(PVector origin_, float r_) {
   
    origin = origin_.get();
    position = new PVector();
    L = r_;
    angle = PI/2;
   aVelocity = 0.0;
    aAcceleration = 0.0;
    damping = 0.995;   // Arbitrary damping
    ballr = 100;      // Arbitrary ball radius
  }
 void go() {
    update();
    drag();   
    display();
  }
 void update() {
    
    if (!dragging) {
      float gravity = 0.05;                              // gravity constant
      aAcceleration =sqrt((gravity / L)) * cos(angle);  // Calculate acceleration(w) 
      aVelocity += aAcceleration;                 // Increment velocity
      aVelocity *= damping;                       // Arbitrary damping
      angle += aVelocity;                         // Increment angle
                    }
              }
 void display() {
    position.set(L*cos(angle), L*sin(angle), 0);         // Polar to cartesian conversion
    position.add(origin);                              // Make sure the position is relative to the pendulum's origin

    stroke(0);
    strokeWeight(5);
   
    line(origin.x, origin.y, position.x, position.y);
    ellipseMode(CENTER);
   ellipse(position.x, position.y, ballr, ballr);
              }


  void clicked(int mx, int my) {
    float d = dist(mx, my, position.x, position.y);
    if (d < ballr) {
      dragging = true;
    }
  }

  // This tells us we are not longer clicking on the ball
 void stopDragging() {
    if (dragging) {
      aVelocity = 0; // No velocity once you let go
      dragging = false;
    }
  }

  void drag() {
    // If we are draging the ball, we calculate the angle between the 
    // pendulum origin and mouse position
    // we assign that angle to the pendulum
    if (dragging) {
      PVector diff = PVector.sub(origin, new PVector(mouseX, mouseY));      // Difference between 2 points
      angle = atan2(-1*diff.x, diff.y) - radians(90);                      // Angle relative to vertical axis
    }
  }
}

BUT SECOND CODE VERY HARD AND COMPLICATE TO ME. Please help me to simple code :smiley: Thank you

1 Like

Here, have a simple, working pendulum that you can tinker with.

void setup(){
  size(400,400);
}

void draw(){
  background(0);
  stroke(255);
  fill(0);
  translate(200,20);
  rotate(HALF_PI);
  rotate( PI/8.0 * cos( map( millis()%7000, 0, 7000,0,TWO_PI) ) );
  line(0,0,350,0);
  ellipse(350,0,20,20);
}

Can you fix this so the pendulum’s period depends on its length?

1 Like