Skip to content

Instantly share code, notes, and snippets.

@FlorianCassayre
Last active February 7, 2017 15:32
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 FlorianCassayre/404d7301f65d3235f955c14f01606214 to your computer and use it in GitHub Desktop.
Save FlorianCassayre/404d7301f65d3235f955c14f01606214 to your computer and use it in GitHub Desktop.
final float g = 0.2;
final float m1 = 1, l1 = 100, m2 = 1, l2 = 100;
float a1 = 0, v1 = 0, x1 = PI / 2;
float a2 = 0, v2 = -0.005, x2 = PI;
float lastX = 0, lastY = 0;
boolean first = true;
PGraphics graphics;
void setup()
{
size(500, 500);
graphics = createGraphics(width, height);
}
void draw()
{
// Draw
background(255);
strokeWeight(3);
final float handle = 5;
final float xp1 = width / 2, yp1 = height / 2;
final float xp2 = xp1 + l1 * cos(x1 + HALF_PI), yp2 = yp1 + l1 * sin(x1 + HALF_PI);
final float xp3 = xp2 + l2 * cos(x2 + HALF_PI), yp3 = yp2 + l2 * sin(x2 + HALF_PI);
if(!first)
{
graphics.beginDraw();
graphics.stroke(255, 0, 0);
graphics.line(lastX, lastY, xp3, yp3);
graphics.endDraw();
}
else
{
first = false;
}
image(graphics, 0, 0);
line(xp1, yp1, xp2, yp2);
line(xp2, yp2, xp3, yp3);
ellipse(xp1, yp1, handle, handle);
ellipse(xp2, yp2, handle, handle);
// Calculate
a1 = (-g * (2 * m1 + m2) * sin(x1) - m2 * g * sin(x1 - 2 * x2) - 2 * sin(x1 - x2) * m2 * (v2 * v2 * l2 + v1 * v1 * l1 * cos(x1 - x2))) / (l1 * (2 * m1 + m2 - m2 * cos(2 * x1 - 2 * x2)));
a2 = (2 * sin(x1 - x2) * (v1 * v1 * l1 * (m1 + m2) + g * (m1 + m2) * cos(x1) + v2 * v2 * l2 * m2 * cos(x1 - x2))) / (l2 * (2 * m1 + m2 - m2 * cos(2 * x1 - 2 * x2)));
v1 += a1;
v2 += a2;
x1 += v1;
x2 += v2;
lastX = xp3;
lastY = yp3;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment