Skip to content

Instantly share code, notes, and snippets.

@jkwok91
Created March 28, 2014 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkwok91/90e1999b78e491c7e5bc to your computer and use it in GitHub Desktop.
Save jkwok91/90e1999b78e491c7e5bc to your computer and use it in GitHub Desktop.
multiple pendulums
class Pendulum {
PVector origin;
PVector bob;
float arm; // length of arm
float r;
float angle, aV, aA;
Pendulum(float x, float y, float a) {
origin = new PVector(x, y);
bob = new PVector(origin.x, arm);
arm = 150.0;
r = 20;
angle = a;
aV = 0.0;
aA = 0.0;
}
void swing() {
aA = -0.00981*sin(angle);
aV += aA;
aV *= 0.999;
angle += aV;
bob.x = origin.x+arm*sin(angle);
bob.y = origin.y+arm*cos(angle);
}
void display() {
stroke(255);
line(origin.x, origin.y, bob.x, bob.y);
ellipse(bob.x, bob.y, r, r);
}
}
int w = 800;
int h = 300;
int cx, cy;
float theta;
int numPends;
float G = 0.981;
PVector gravity;
Pendulum[] pends;
void setup() {
size(w, h);
background(0);
cx = width/2;
cy = height/2;
gravity = new PVector(0, G);
theta = 0.0;
setupPends();
}
void setupPends() {
numPends = 5;
pends = new Pendulum[numPends];
for (int i = 0; i < numPends; i++) {
Pendulum p = new Pendulum(cx, 0, theta-(i*(theta/numPends)));
pends[i] = p;
}
}
void draw() {
background(0);
text("click to start", cx, cy+50);
for (int i = 0; i < pends.length; i++) {
Pendulum p = pends[i];
p.swing();
p.display();
}
}
void mouseReleased() {
float dx = mouseX - cx;
float dy = mouseY;
theta = atan(dx/dy);
setupPends();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment