From 322df8c2a1aa32210102b2924b44be6e20cdf8ae Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Thu, 24 Jun 2021 09:28:20 -0400 Subject: Decently working demo for rigidbody 1 --- 2d/rigidbody/rigidbody_1.html | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to '2d/rigidbody/rigidbody_1.html') 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 int32 NUM_IMPULSES = 4; void update(float32 deltaTimeSeconds) { applyGravity(deltaTimeSeconds); - // Add up all of the forces acting at this moment Vector2 force; for (int32 idx = 0; idx < numImpulses; idx++) { Impulse& i = activeImpulses[idx]; float32 nextTimeAppliedSeconds = i.timeAppliedSeconds + deltaTimeSeconds; if (nextTimeAppliedSeconds >= i.timeOfApplicationSeconds) { - nextTimeAppliedSeconds = i.timeOfApplicationSeconds; + nextTimeAppliedSeconds = i.timeOfApplicationSeconds; // Do the remainder of the time i.isDead = true; } + // We apply the force spread out over timeOfApplicationSeconds, so we need + // to calculate the fractional amount of force that was applied in this frame. float32 impulseDtSeconds = nextTimeAppliedSeconds - i.timeAppliedSeconds; - force += i.force * impulseDtSeconds; + Vector2 forceToApply = i.force * (impulseDtSeconds / i.timeOfApplicationSeconds); + force += forceToApply * impulseDtSeconds; i.timeAppliedSeconds = nextTimeAppliedSeconds; } Vector2 acceleration = force / mass; - velocity += (acceleration * impulseDtSeconds); + velocity += (acceleration * deltaTimeSeconds); position += (velocity * deltaTimeSeconds); // Cleanup any impulses that have expired in the mean time @@ -232,7 +234,7 @@ const int32 NUM_IMPULSES = 4;
  • Remove any dead impulses from the list
  • - 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.

    -- cgit v1.2.1