|
|
|
@ -46,8 +46,8 @@ int main(void) |
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - double pendulum"); |
|
|
|
|
|
|
|
// Simulation Paramters |
|
|
|
float l1 = 15, m1 = 0.2, theta1 = DEG2RAD * 170, w1 = 0; |
|
|
|
float l2 = 15, m2 = 0.1, theta2 = DEG2RAD * 0, w2 = 0; |
|
|
|
float l1 = 15, m1 = 0.2, theta1 = DEG2RAD*170, w1 = 0; |
|
|
|
float l2 = 15, m2 = 0.1, theta2 = DEG2RAD*0, w2 = 0; |
|
|
|
float lengthScaler = 0.1; |
|
|
|
float totalM = m1 + m2; |
|
|
|
|
|
|
|
@ -56,8 +56,8 @@ int main(void) |
|
|
|
previousPosition.y += (screenHeight/2 - 100); |
|
|
|
|
|
|
|
// Scale length |
|
|
|
float L1 = l1 * lengthScaler; |
|
|
|
float L2 = l2 * lengthScaler; |
|
|
|
float L1 = l1*lengthScaler; |
|
|
|
float L2 = l2*lengthScaler; |
|
|
|
|
|
|
|
// Draw parameters |
|
|
|
int lineThick = 20, trailThick = 2; |
|
|
|
@ -76,26 +76,26 @@ int main(void) |
|
|
|
// Update |
|
|
|
//---------------------------------------------------------------------------------- |
|
|
|
float dt = GetFrameTime(); |
|
|
|
float step = dt / SIMULATION_STEPS, step2 = step * step; |
|
|
|
float step = dt/SIMULATION_STEPS, step2 = step*step; |
|
|
|
|
|
|
|
// Update Physics - larger steps = better approximation |
|
|
|
for (int i = 0; i < SIMULATION_STEPS; ++i) |
|
|
|
{ |
|
|
|
float delta = theta1 - theta2; |
|
|
|
float sinD = sinf(delta), cosD = cosf(delta), cos2D = cosf(2*delta); |
|
|
|
float ww1 = w1 * w1, ww2 = w2 * w2; |
|
|
|
float ww1 = w1*w1, ww2 = w2*w2; |
|
|
|
|
|
|
|
// Calculate a1 |
|
|
|
float a1 = (-G*(2*m1 + m2)*sinf(theta1) |
|
|
|
- m2*G*sinf(theta1 - 2*theta2) |
|
|
|
- 2*sinD*m2*(ww2*L2 + ww1*L1*cosD)) |
|
|
|
/ (L1*(2*m1 + m2 - m2*cos2D)); |
|
|
|
/(L1*(2*m1 + m2 - m2*cos2D)); |
|
|
|
|
|
|
|
// Calculate a2 |
|
|
|
float a2 = (2*sinD*(ww1*L1*totalM |
|
|
|
+ G*totalM*cosf(theta1) |
|
|
|
+ ww2*L2*m2*cosD)) |
|
|
|
/ (L2*(2*m1 + m2 - m2*cos2D)); |
|
|
|
/(L2*(2*m1 + m2 - m2*cos2D)); |
|
|
|
|
|
|
|
// Update thetas |
|
|
|
theta1 += w1*step + 0.5f*a1*step2; |
|
|
|
@ -118,7 +118,7 @@ int main(void) |
|
|
|
|
|
|
|
// Draw trail |
|
|
|
DrawCircleV(previousPosition, trailThick, RED); |
|
|
|
DrawLineEx(previousPosition, currentPosition, trailThick * 2, RED); |
|
|
|
DrawLineEx(previousPosition, currentPosition, trailThick*2, RED); |
|
|
|
EndTextureMode(); |
|
|
|
|
|
|
|
// Update previous position |
|
|
|
@ -135,12 +135,12 @@ int main(void) |
|
|
|
DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE); |
|
|
|
|
|
|
|
// Draw double pendulum |
|
|
|
DrawRectanglePro((Rectangle){ screenWidth/2, screenHeight/2 - 100, 10 * l1, lineThick }, |
|
|
|
(Vector2){0, lineThick * 0.5}, 90 - RAD2DEG * theta1, RAYWHITE); |
|
|
|
DrawRectanglePro((Rectangle){ screenWidth/2, screenHeight/2 - 100, 10*l1, lineThick }, |
|
|
|
(Vector2){0, lineThick*0.5}, 90 - RAD2DEG*theta1, RAYWHITE); |
|
|
|
|
|
|
|
Vector2 endpoint1 = CalculatePendulumEndPoint(l1, theta1); |
|
|
|
DrawRectanglePro((Rectangle){ screenWidth/2 + endpoint1.x, screenHeight/2 - 100 + endpoint1.y, 10 * l2, lineThick }, |
|
|
|
(Vector2){0, lineThick * 0.5}, 90 - RAD2DEG * theta2, RAYWHITE); |
|
|
|
DrawRectanglePro((Rectangle){ screenWidth/2 + endpoint1.x, screenHeight/2 - 100 + endpoint1.y, 10*l2, lineThick }, |
|
|
|
(Vector2){0, lineThick*0.5}, 90 - RAD2DEG*theta2, RAYWHITE); |
|
|
|
|
|
|
|
EndDrawing(); |
|
|
|
//---------------------------------------------------------------------------------- |
|
|
|
@ -159,7 +159,7 @@ int main(void) |
|
|
|
// Calculate Pendulum End Point |
|
|
|
static Vector2 CalculatePendulumEndPoint(float l, float theta) |
|
|
|
{ |
|
|
|
return (Vector2){ 10 * l * sin(theta), 10 * l * cos(theta) }; |
|
|
|
return (Vector2){ 10*l*sin(theta), 10*l*cos(theta) }; |
|
|
|
} |
|
|
|
|
|
|
|
// Calculate Double Pendulum End Point |
|
|
|
|