summaryrefslogtreecommitdiff
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
parent953c41b0ec054997cdbc77bf6953187a22d9bd12 (diff)
Decently working demo for rigidbody 1
-rw-r--r--2d/rigidbody/rigidbody_1.html12
-rw-r--r--2d/rigidbody/rigidbody_1.html.content2
-rwxr-xr-x2d/rigidbody/rigidbody_1/dist/output.wasmbin48588 -> 48801 bytes
-rw-r--r--2d/rigidbody/rigidbody_1/main.cpp11
-rw-r--r--2d/rigidbody/rigidbody_1/snippet3.cpp10
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 <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>
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 @@
<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>
diff --git a/2d/rigidbody/rigidbody_1/dist/output.wasm b/2d/rigidbody/rigidbody_1/dist/output.wasm
index 5ceed51..f6625a9 100755
--- a/2d/rigidbody/rigidbody_1/dist/output.wasm
+++ b/2d/rigidbody/rigidbody_1/dist/output.wasm
Binary files 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<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