diff options
author | Matthew Kosarek <mattkae@protonmail.com> | 2021-06-24 13:49:23 -0400 |
---|---|---|
committer | Matthew Kosarek <mattkae@protonmail.com> | 2021-06-24 13:49:23 -0400 |
commit | 410a072c3862481f729c293402ddd49c5ae98769 (patch) | |
tree | 6fbd4b31b355cd0228bd790415762b446dcd4ab3 /2d/rigidbody/rigidbody_2 | |
parent | 0f1275d7ba1a7e3ad838423c15d78e23c960f80e (diff) |
(mkosarek) Decent enough discussion of 2d rotational forces
Diffstat (limited to '2d/rigidbody/rigidbody_2')
-rw-r--r-- | 2d/rigidbody/rigidbody_2/main.cpp | 7 | ||||
-rw-r--r-- | 2d/rigidbody/rigidbody_2/snippet1.cpp | 12 | ||||
-rw-r--r-- | 2d/rigidbody/rigidbody_2/snippet2.cpp | 8 | ||||
-rw-r--r-- | 2d/rigidbody/rigidbody_2/snippet3.cpp | 88 | ||||
-rw-r--r-- | 2d/rigidbody/rigidbody_2/tinysnippet.cpp | 4 |
5 files changed, 116 insertions, 3 deletions
diff --git a/2d/rigidbody/rigidbody_2/main.cpp b/2d/rigidbody/rigidbody_2/main.cpp index bad6949..123cf7e 100644 --- a/2d/rigidbody/rigidbody_2/main.cpp +++ b/2d/rigidbody/rigidbody_2/main.cpp @@ -63,13 +63,15 @@ struct Rigidbody { float32 nextTimeAppliedSeconds = i.timeAppliedSeconds + deltaTimeSeconds; if (nextTimeAppliedSeconds >= i.timeOfApplicationSeconds) { - nextTimeAppliedSeconds = i.timeOfApplicationSeconds; // Do the remainder of the time + nextTimeAppliedSeconds = i.timeOfApplicationSeconds; i.isDead = true; } float32 impulseDtSeconds = nextTimeAppliedSeconds - i.timeAppliedSeconds; Vector2 forceToApply = i.force * (impulseDtSeconds / i.timeOfApplicationSeconds); force += forceToApply * impulseDtSeconds; + + // New! Increment the torque for each force torque += i.pointOfApplication.getPerp().dot(forceToApply); i.timeAppliedSeconds = nextTimeAppliedSeconds; @@ -79,12 +81,11 @@ struct Rigidbody { velocity += (acceleration * deltaTimeSeconds); position += (velocity * deltaTimeSeconds); - // New: Update the rotational velocity as well + // New! Update the rotational velocity as well float32 rotationalAcceleration = torque / momentOfInertia; rotationalVelocity += (rotationalAcceleration * deltaTimeSeconds); rotation += (rotationalVelocity * deltaTimeSeconds); - // Cleanup any impulses that have expired in the mean time for (int32 idx = 0; idx < numImpulses; idx++) { if (activeImpulses[idx].isDead) { for (int j = idx + 1; j < numImpulses; j++) { diff --git a/2d/rigidbody/rigidbody_2/snippet1.cpp b/2d/rigidbody/rigidbody_2/snippet1.cpp new file mode 100644 index 0000000..fbc7bf1 --- /dev/null +++ b/2d/rigidbody/rigidbody_2/snippet1.cpp @@ -0,0 +1,12 @@ + +struct Rigidbody { + int32 numImpulses = 0; + Impulse activeImpulses[NUM_IMPULSES]; + Vector2 velocity = { 0, 0 }; + Vector2 position = { 0, 0 }; + float32 mass = 1.f; + float32 rotationalVelocity = 0.f; + float32 rotation = 0.f; + float32 momentOfInertia = 1.f; + ... +} diff --git a/2d/rigidbody/rigidbody_2/snippet2.cpp b/2d/rigidbody/rigidbody_2/snippet2.cpp new file mode 100644 index 0000000..bd0cdb2 --- /dev/null +++ b/2d/rigidbody/rigidbody_2/snippet2.cpp @@ -0,0 +1,8 @@ + +struct Impulse { + Vector2 force = { 0, 0 }; + Vector2 pointOfApplication = { 0, 0 }; + float32 timeOfApplicationSeconds = 0.25f; + float32 timeAppliedSeconds = 0.f; + bool isDead = false; +}; diff --git a/2d/rigidbody/rigidbody_2/snippet3.cpp b/2d/rigidbody/rigidbody_2/snippet3.cpp new file mode 100644 index 0000000..7b7130c --- /dev/null +++ b/2d/rigidbody/rigidbody_2/snippet3.cpp @@ -0,0 +1,88 @@ + +struct Impulse { + Vector2 force = { 0, 0 }; + Vector2 pointOfApplication = { 0, 0 }; + float32 timeOfApplicationSeconds = 0.25f; + float32 timeAppliedSeconds = 0.f; + bool isDead = false; +}; + +const int32 NUM_IMPULSES = 4; + +struct Rigidbody { + int32 numImpulses = 0; + Impulse activeImpulses[NUM_IMPULSES]; + Vector2 velocity = { 0, 0 }; + Vector2 position = { 0, 0 }; + float32 mass = 1.f; + + float32 rotationalVelocity = 0.f; + float32 rotation = 0.f; + float32 momentOfInertia = 1.f; + + void reset() { + numImpulses = 0; + velocity = { 0, 0 }; + rotationalVelocity = 0.f; + rotation = 0.f; + } + + void applyImpulse(Impulse i) { + if (numImpulses > NUM_IMPULSES) { + printf("Unable to apply impulse. Buffer full.\n"); + return; + } + + activeImpulses[numImpulses] = i; + numImpulses++; + } + + void applyGravity(float32 deltaTimeSeconds) { + velocity += (Vector2 { 0.f, -9.8f } * deltaTimeSeconds); + } + + void update(float32 deltaTimeSeconds) { + applyGravity(deltaTimeSeconds); + + Vector2 force; + float32 torque = 0.f; + for (int32 idx = 0; idx < numImpulses; idx++) { + Impulse& i = activeImpulses[idx]; + + float32 nextTimeAppliedSeconds = i.timeAppliedSeconds + deltaTimeSeconds; + if (nextTimeAppliedSeconds >= i.timeOfApplicationSeconds) { + nextTimeAppliedSeconds = i.timeOfApplicationSeconds; + i.isDead = true; + } + + float32 impulseDtSeconds = nextTimeAppliedSeconds - i.timeAppliedSeconds; + Vector2 forceToApply = i.force * (impulseDtSeconds / i.timeOfApplicationSeconds); + force += forceToApply * impulseDtSeconds; + + // New! Increment the torque for each force + torque += i.pointOfApplication.getPerp().dot(forceToApply); + + i.timeAppliedSeconds = nextTimeAppliedSeconds; + } + + Vector2 acceleration = force / mass; + velocity += (acceleration * deltaTimeSeconds); + position += (velocity * deltaTimeSeconds); + + // New! Update the rotational velocity as well + float32 rotationalAcceleration = torque / momentOfInertia; + rotationalVelocity += (rotationalAcceleration * deltaTimeSeconds); + rotation += (rotationalVelocity * deltaTimeSeconds); + + for (int32 idx = 0; idx < numImpulses; idx++) { + if (activeImpulses[idx].isDead) { + for (int j = idx + 1; j < numImpulses; j++) { + activeImpulses[j - 1] = activeImpulses[j]; + } + + idx = idx - 1; + numImpulses--; + } + } + } +}; diff --git a/2d/rigidbody/rigidbody_2/tinysnippet.cpp b/2d/rigidbody/rigidbody_2/tinysnippet.cpp new file mode 100644 index 0000000..2f8fbdd --- /dev/null +++ b/2d/rigidbody/rigidbody_2/tinysnippet.cpp @@ -0,0 +1,4 @@ + +Vector2 getPerp() { + return { y, -x }; +} |