From 2ab6b6cfe81505b029f2da397cef0bb58989444f Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Thu, 17 Jun 2021 21:32:57 -0400 Subject: (mkosarek) Beginning to explain rigidbody physics in a reasonable way --- 2d/rigidbody/rigidbody_1/main.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to '2d/rigidbody/rigidbody_1/main.cpp') diff --git a/2d/rigidbody/rigidbody_1/main.cpp b/2d/rigidbody/rigidbody_1/main.cpp index 8d4ab0e..321e3e5 100644 --- a/2d/rigidbody/rigidbody_1/main.cpp +++ b/2d/rigidbody/rigidbody_1/main.cpp @@ -11,6 +11,8 @@ #include #include +const float32 MAX_VELOCITY = 200.f; + struct Rigidbody { Vector2 force = { 0, 0 }; Vector2 velocity = { 0, 0 }; @@ -35,6 +37,14 @@ struct Rigidbody { Vector2 acceleration = force / mass; velocity += (acceleration * deltaTimeSeconds); + + if (ABS(velocity.x) > MAX_VELOCITY) { + velocity.x = SIGN(velocity.x) * MAX_VELOCITY; + } + if (ABS(velocity.y) > MAX_VELOCITY) { + velocity.y = SIGN(velocity.y) * MAX_VELOCITY; + } + position += (velocity * deltaTimeSeconds); force = Vector2 { 0.f, 0.f }; } -- cgit v1.2.1