From a36f425491aaf019243a31179e80cb10ea62db59 Mon Sep 17 00:00:00 2001
From: Matthew Kosarek
Now that we have that understanding, we can begin setting up our rigidbody data structure.
- {{{rigidbody_1/snippet1.cpp}}}
-
+
As you can see, the base data structure exactly mirrors what we already know from 2D newtonian physics.
struct Rigidbody {
+ Vector2 force = { 0, 0 };
+ Vector2 acceleration = { 0, 0 };
+ Vector2 velocity = { 0, 0 };
+ Vector2 position = { 0, 0 };
+ float32 mass = 1.f;
+};
+
Now, let's put that Rigidbody data structure to work! As I mentioned earlier, you can think of dynamics as the input to the system. What we're going to do now is add a way to - {{{rigidbody_1/snippet2.cpp}}} -
+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 };
+ }
+};
+