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/snippet2.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2d/rigidbody/rigidbody_1/snippet2.cpp (limited to '2d/rigidbody/rigidbody_1/snippet2.cpp') diff --git a/2d/rigidbody/rigidbody_1/snippet2.cpp b/2d/rigidbody/rigidbody_1/snippet2.cpp new file mode 100644 index 0000000..8ad468c --- /dev/null +++ b/2d/rigidbody/rigidbody_1/snippet2.cpp @@ -0,0 +1,25 @@ + + +struct Rigidbody { + Vector2 force = { 0, 0 }; + Vector2 velocity = { 0, 0 }; + Vector2 position = { 0, 0 }; + float32 mass = 1.f; + + void applyForce(Vector2 f) { + force += f; + } + + void applyGravity(float32 deltaTimeSeconds) { + velocity += (Vector2 { 0.f, -50.f } * deltaTimeSeconds); + } + + void update(float32 deltaTimeSeconds) { + applyGravity(deltaTimeSeconds); + + Vector2 acceleration = force / mass; + velocity += (acceleration * deltaTimeSeconds); + position += (velocity * deltaTimeSeconds); + force = Vector2 { 0.f, 0.f }; + } +}; -- cgit v1.2.1