# Simple Pendulum Project

**URL:** <https://discourse.processing.org/t/simple-pendulum-project/7048>\
**Category:** Coding Questions\
**Created:** [December 31, 2018, 8:00am UTC](https://discourse.processing.org/t/simple-pendulum-project/7048 "2018-12-31T08:00:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![oheuysal](https://avatars.discourse-cdn.com/v4/letter/o/ba9def/32.png) [@oheuysal](https://discourse.processing.org/u/oheuysal)\
**Post date:** [December 31, 2018, 8:00am UTC](https://discourse.processing.org/t/simple-pendulum-project/7048/1 "2018-12-31T08:00:16Z")

</div>

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:

 ![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/3bd886d089d2b2e2a445f74ff3df7f12dc168f49.png) 

I PREPARE THIS CODE:

```auto
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:

```auto
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 😃 Thank you

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [December 31, 2018, 8:28am UTC](https://discourse.processing.org/t/simple-pendulum-project/7048/2 "2018-12-31T08:28:51Z")

</div>

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

```auto
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?
