Replaced scalar to float

This commit is contained in:
Joecheong2006
2025-07-22 11:59:46 -07:00
parent b6ae5af4e3
commit 10fadd9185

View File

@@ -32,13 +32,11 @@
#define SIMULATION_STEPS 30 #define SIMULATION_STEPS 30
#define G 9.81 #define G 9.81
#define scalar float
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Module Functions Declaration // Module Functions Declaration
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
static Vector2 CalculatePendulumEndPoint(scalar l, scalar theta); static Vector2 CalculatePendulumEndPoint(float l, float theta);
static Vector2 CalculateDoublePendulumEndPoint(scalar l1, scalar theta1, scalar l2, scalar theta2); static Vector2 CalculateDoublePendulumEndPoint(float l1, float theta1, float l2, float theta2);
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Program main entry point // Program main entry point
@@ -52,18 +50,18 @@ int main(void)
// Simulation Paramters // Simulation Paramters
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
scalar l1 = 15, m1 = 0.2, theta1 = DEG2RAD * 170, w1 = 0; float l1 = 15, m1 = 0.2, theta1 = DEG2RAD * 170, w1 = 0;
scalar l2 = 15, m2 = 0.1, theta2 = DEG2RAD * 0, w2 = 0; float l2 = 15, m2 = 0.1, theta2 = DEG2RAD * 0, w2 = 0;
scalar lengthScaler = 0.1; float lengthScaler = 0.1;
scalar totalM = m1 + m2; float totalM = m1 + m2;
Vector2 previousPosition = CalculateDoublePendulumEndPoint(l1, theta1, l2, theta2); Vector2 previousPosition = CalculateDoublePendulumEndPoint(l1, theta1, l2, theta2);
previousPosition.x += CENTER_X; previousPosition.x += CENTER_X;
previousPosition.y += CENTER_Y; previousPosition.y += CENTER_Y;
// Scale length // Scale length
scalar L1 = l1 * lengthScaler; float L1 = l1 * lengthScaler;
scalar L2 = l2 * lengthScaler; float L2 = l2 * lengthScaler;
// Draw Parameters // Draw Parameters
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
@@ -82,25 +80,25 @@ int main(void)
while (!WindowShouldClose()) // Detect window close button or ESC key while (!WindowShouldClose()) // Detect window close button or ESC key
{ {
// Update // Update
scalar dt = GetFrameTime(); float dt = GetFrameTime();
scalar step = dt / SIMULATION_STEPS, step2 = step * step; float step = dt / SIMULATION_STEPS, step2 = step * step;
// Update Physics - larger steps = better approximation // Update Physics - larger steps = better approximation
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
for (int i = 0; i < SIMULATION_STEPS; ++i) for (int i = 0; i < SIMULATION_STEPS; ++i)
{ {
scalar delta = theta1 - theta2; float delta = theta1 - theta2;
scalar sinD = sin(delta), cosD = cos(delta), cos2D = cos(2 * delta); float sinD = sin(delta), cosD = cos(delta), cos2D = cos(2 * delta);
scalar ww1 = w1 * w1, ww2 = w2 * w2; float ww1 = w1 * w1, ww2 = w2 * w2;
// Calculate a1 // Calculate a1
scalar a1 = (-G * (2 * m1 + m2) * sin(theta1) float a1 = (-G * (2 * m1 + m2) * sin(theta1)
- m2 * G * sin(theta1 - 2 * theta2) - m2 * G * sin(theta1 - 2 * theta2)
- 2 * sinD * m2 * (ww2 * L2 + ww1 * L1 * cosD)) - 2 * sinD * m2 * (ww2 * L2 + ww1 * L1 * cosD))
/ (L1 * (2 * m1 + m2 - m2 * cos2D)); / (L1 * (2 * m1 + m2 - m2 * cos2D));
// Calculate a2 // Calculate a2
scalar a2 = (2 * sinD * (ww1 * L1 * totalM float a2 = (2 * sinD * (ww1 * L1 * totalM
+ G * totalM * cos(theta1) + G * totalM * cos(theta1)
+ ww2 * L2 * m2 * cosD)) + ww2 * L2 * m2 * cosD))
/ (L2 * (2 * m1 + m2 - m2 * cos2D)); / (L2 * (2 * m1 + m2 - m2 * cos2D));
@@ -170,13 +168,13 @@ int main(void)
} }
// Calculate Pendulum End Point // Calculate Pendulum End Point
static Vector2 CalculatePendulumEndPoint(scalar l, scalar theta) 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 // Calculate Double Pendulum End Point
static Vector2 CalculateDoublePendulumEndPoint(scalar l1, scalar theta1, scalar l2, scalar theta2) static Vector2 CalculateDoublePendulumEndPoint(float l1, float theta1, float l2, float theta2)
{ {
Vector2 endpoint1 = CalculatePendulumEndPoint(l1, theta1); Vector2 endpoint1 = CalculatePendulumEndPoint(l1, theta1);
Vector2 endpoint2 = CalculatePendulumEndPoint(l2, theta2); Vector2 endpoint2 = CalculatePendulumEndPoint(l2, theta2);