summaryrefslogtreecommitdiff
path: root/2d/rigidbody/rigidbody_1.html
diff options
context:
space:
mode:
authorMatthew Kosarek <mattkae@protonmail.com>2021-06-24 09:28:20 -0400
committerMatthew Kosarek <mattkae@protonmail.com>2021-06-24 09:28:20 -0400
commit322df8c2a1aa32210102b2924b44be6e20cdf8ae (patch)
treef980a6d772559499fdda410c5956ca5569aa43e9 /2d/rigidbody/rigidbody_1.html
parent953c41b0ec054997cdbc77bf6953187a22d9bd12 (diff)
Decently working demo for rigidbody 1
Diffstat (limited to '2d/rigidbody/rigidbody_1.html')
-rw-r--r--2d/rigidbody/rigidbody_1.html12
1 files changed, 7 insertions, 5 deletions
diff --git a/2d/rigidbody/rigidbody_1.html b/2d/rigidbody/rigidbody_1.html
index 00ea74a..9ef056c 100644
--- a/2d/rigidbody/rigidbody_1.html
+++ b/2d/rigidbody/rigidbody_1.html
@@ -174,24 +174,26 @@ const <span class="code_keyword">int32</span> NUM_IMPULSES = 4;
<span class="code_keyword">void</span> update(float32 deltaTimeSeconds) {
applyGravity(deltaTimeSeconds);
- <span class="code_comment">// Add up all of the forces acting at this moment</span>
<span class="code_keyword">Vector2</span> force;
for (int32 idx = 0; idx < numImpulses; idx++) {
Impulse& i = activeImpulses[idx];
<span class="code_keyword">float32</span> nextTimeAppliedSeconds = i.timeAppliedSeconds + deltaTimeSeconds;
if (nextTimeAppliedSeconds >= i.timeOfApplicationSeconds) {
- nextTimeAppliedSeconds = i.timeOfApplicationSeconds;
+ nextTimeAppliedSeconds = i.timeOfApplicationSeconds; <span class="code_comment">// Do the remainder of the time</span>
i.isDead = true;
}
+ <span class="code_comment">// We apply the force spread out over timeOfApplicationSeconds, so we need</span>
+ <span class="code_comment">// to calculate the fractional amount of force that was applied in this frame.</span>
<span class="code_keyword">float32</span> impulseDtSeconds = nextTimeAppliedSeconds - i.timeAppliedSeconds;
- force += i.force * impulseDtSeconds;
+ <span class="code_keyword">Vector2</span> forceToApply = i.force * (impulseDtSeconds / i.timeOfApplicationSeconds);
+ force += forceToApply * impulseDtSeconds;
i.timeAppliedSeconds = nextTimeAppliedSeconds;
}
<span class="code_keyword">Vector2</span> acceleration = force / mass;
- velocity += (acceleration * impulseDtSeconds);
+ velocity += (acceleration * deltaTimeSeconds);
position += (velocity * deltaTimeSeconds);
<span class="code_comment">// Cleanup any impulses that have expired in the mean time</span>
@@ -232,7 +234,7 @@ const <span class="code_keyword">int32</span> NUM_IMPULSES = 4;
<li>Remove any dead impulses from the list</li>
</ul>
- Feel free to look at the example program below (and browse/download the code) if you want to see it in action.
+ Keep in mind that you can still have what amounts to an "instant" force being applied if you set the duration of the impulse to be very small. Feel free to look at the example program below (and browse/download the code) if you want to see it in action.
</p>
</section>
<section>