diff options
author | Matthew Kosarek <mattkae@protonmail.com> | 2021-06-24 09:28:20 -0400 |
---|---|---|
committer | Matthew Kosarek <mattkae@protonmail.com> | 2021-06-24 09:28:20 -0400 |
commit | 322df8c2a1aa32210102b2924b44be6e20cdf8ae (patch) | |
tree | f980a6d772559499fdda410c5956ca5569aa43e9 /2d/rigidbody/rigidbody_1 | |
parent | 953c41b0ec054997cdbc77bf6953187a22d9bd12 (diff) |
Decently working demo for rigidbody 1
Diffstat (limited to '2d/rigidbody/rigidbody_1')
-rwxr-xr-x | 2d/rigidbody/rigidbody_1/dist/output.wasm | bin | 48588 -> 48801 bytes | |||
-rw-r--r-- | 2d/rigidbody/rigidbody_1/main.cpp | 11 | ||||
-rw-r--r-- | 2d/rigidbody/rigidbody_1/snippet3.cpp | 10 |
3 files changed, 12 insertions, 9 deletions
diff --git a/2d/rigidbody/rigidbody_1/dist/output.wasm b/2d/rigidbody/rigidbody_1/dist/output.wasm Binary files differindex 5ceed51..f6625a9 100755 --- a/2d/rigidbody/rigidbody_1/dist/output.wasm +++ b/2d/rigidbody/rigidbody_1/dist/output.wasm diff --git a/2d/rigidbody/rigidbody_1/main.cpp b/2d/rigidbody/rigidbody_1/main.cpp index b3ed359..88c322c 100644 --- a/2d/rigidbody/rigidbody_1/main.cpp +++ b/2d/rigidbody/rigidbody_1/main.cpp @@ -60,14 +60,15 @@ struct Rigidbody { } 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 @@ -207,7 +208,7 @@ int main() { void load() { renderer.load(&context); - rectangle.load(&renderer, Vector4 { 55.f, 235.f, 35.f, 255.f }, 32.f, 32.f); + rectangle.load(&renderer, Vector4 { 55.f, 235.f, 35.f, 255.f }, 100.f, 75.f); rectangle.body.position = Vector2 { context.width / 3.f, context.height / 3.f }; rectangle.body.velocity = Vector2 { 100.f, 250.f }; @@ -300,8 +301,8 @@ EM_BOOL onMouseMove(int eventType, const EmscriptenMouseEvent *mouseEvent, void return true; } - pointer.force.x = static_cast<float32>(mouseEvent->movementX) * 2000.f; - pointer.force.y = static_cast<float32>(-mouseEvent->movementY) * 2000.f; + pointer.force.x = static_cast<float32>(mouseEvent->movementX) * 100000.f; + pointer.force.y = static_cast<float32>(-mouseEvent->movementY) * 100000.f; pointer.body.position.x = static_cast<float32>(mouseEvent->targetX); pointer.body.position.y = static_cast<float32>(600.f - mouseEvent->targetY); diff --git a/2d/rigidbody/rigidbody_1/snippet3.cpp b/2d/rigidbody/rigidbody_1/snippet3.cpp index a43c884..4727a9a 100644 --- a/2d/rigidbody/rigidbody_1/snippet3.cpp +++ b/2d/rigidbody/rigidbody_1/snippet3.cpp @@ -17,24 +17,26 @@ struct Rigidbody { 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 |