diff options
Diffstat (limited to '2d/rigidbody/rigidbody_1.html')
-rw-r--r-- | 2d/rigidbody/rigidbody_1.html | 36 |
1 files changed, 32 insertions, 4 deletions
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 @@ <p> Now that we have that understanding, we can begin setting up our rigidbody data structure. - {{{rigidbody_1/snippet1.cpp}}} - + <pre><code><span class="code_keyword">struct</span> Rigidbody { + <span class="code_keyword">Vector2</span> force = { 0, 0 }; + <span class="code_keyword">Vector2</span> acceleration = { 0, 0 }; + <span class="code_keyword">Vector2</span> velocity = { 0, 0 }; + <span class="code_keyword">Vector2</span> position = { 0, 0 }; + <span class="code_keyword">float32</span> mass = 1.f; +}; +</code></pre> As you can see, the base data structure exactly mirrors what we already know from 2D newtonian physics. </p> </section> @@ -103,8 +109,30 @@ <p> Now, let's put that Rigidbody data structure to work! As I mentioned earlier, you can think of dynamics as the <i>input</i> to the system. What we're going to do now is add a way to - {{{rigidbody_1/snippet2.cpp}}} - </p> + <pre><code><span class="code_keyword">struct</span> Rigidbody { + <span class="code_keyword">Vector2</span> force = { 0, 0 }; + <span class="code_keyword">Vector2</span> velocity = { 0, 0 }; + <span class="code_keyword">Vector2</span> position = { 0, 0 }; + <span class="code_keyword">float32</span> mass = 1.f; + + <span class="code_keyword">void</span> applyForce(Vector2 f) { + force += f; + } + + <span class="code_keyword">void</span> applyGravity(float32 deltaTimeSeconds) { + velocity += (Vector2 { 0.f, -50.f } * deltaTimeSeconds); + } + + <span class="code_keyword">void</span> update(float32 deltaTimeSeconds) { + applyGravity(deltaTimeSeconds); + + <span class="code_keyword">Vector2</span> acceleration = force / mass; + velocity += (acceleration * deltaTimeSeconds); + position += (velocity * deltaTimeSeconds); + force = <span class="code_keyword">Vector2</span> { 0.f, 0.f }; + } +}; +</code></pre> </p> </section> <section> <h2> |