From a36f425491aaf019243a31179e80cb10ea62db59 Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Mon, 21 Jun 2021 20:05:08 -0400 Subject: (mkosarek) Transpiling code snippets in a shotty way, but it is good enough --- 2d/rigidbody/rigidbody_1.html | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to '2d/rigidbody/rigidbody_1.html') diff --git a/2d/rigidbody/rigidbody_1.html b/2d/rigidbody/rigidbody_1.html index e7e848d..0751499 100644 --- a/2d/rigidbody/rigidbody_1.html +++ b/2d/rigidbody/rigidbody_1.html @@ -93,8 +93,14 @@

Now that we have that understanding, we can begin setting up our rigidbody data structure. - {{{rigidbody_1/snippet1.cpp}}} - +

struct Rigidbody {
+    Vector2 force = { 0, 0 };
+    Vector2 acceleration = { 0, 0 };
+    Vector2 velocity = { 0, 0 };
+    Vector2 position = { 0, 0 };
+    float32 mass = 1.f;
+};
+
As you can see, the base data structure exactly mirrors what we already know from 2D newtonian physics.

@@ -103,8 +109,30 @@

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 };
+    }
+};
+

-- cgit v1.2.1