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 +++++++-----
2d/rigidbody/rigidbody_1.html.content | 2 +-
2d/rigidbody/rigidbody_1/dist/output.wasm | Bin 48588 -> 48801 bytes
2d/rigidbody/rigidbody_1/main.cpp | 11 ++++++-----
2d/rigidbody/rigidbody_1/snippet3.cpp | 10 ++++++----
5 files changed, 20 insertions(+), 15 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 int32 NUM_IMPULSES = 4;
void update(float32 deltaTimeSeconds) {
applyGravity(deltaTimeSeconds);
-
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;
i.isDead = true;
}
+
+
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);
@@ -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.
diff --git a/2d/rigidbody/rigidbody_1.html.content b/2d/rigidbody/rigidbody_1.html.content
index d61acaf..6e7d46b 100644
--- a/2d/rigidbody/rigidbody_1.html.content
+++ b/2d/rigidbody/rigidbody_1.html.content
@@ -88,7 +88,7 @@
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.
diff --git a/2d/rigidbody/rigidbody_1/dist/output.wasm b/2d/rigidbody/rigidbody_1/dist/output.wasm
index 5ceed51..f6625a9 100755
Binary files a/2d/rigidbody/rigidbody_1/dist/output.wasm and b/2d/rigidbody/rigidbody_1/dist/output.wasm differ
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(mouseEvent->movementX) * 2000.f;
- pointer.force.y = static_cast(-mouseEvent->movementY) * 2000.f;
+ pointer.force.x = static_cast(mouseEvent->movementX) * 100000.f;
+ pointer.force.y = static_cast(-mouseEvent->movementY) * 100000.f;
pointer.body.position.x = static_cast(mouseEvent->targetX);
pointer.body.position.y = static_cast(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
--
cgit v1.2.1