summaryrefslogtreecommitdiff
path: root/2d/rigidbody/rigidbody_1/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to '2d/rigidbody/rigidbody_1/main.cpp')
-rw-r--r--2d/rigidbody/rigidbody_1/main.cpp73
1 files changed, 55 insertions, 18 deletions
diff --git a/2d/rigidbody/rigidbody_1/main.cpp b/2d/rigidbody/rigidbody_1/main.cpp
index 321e3e5..b3ed359 100644
--- a/2d/rigidbody/rigidbody_1/main.cpp
+++ b/2d/rigidbody/rigidbody_1/main.cpp
@@ -11,42 +11,76 @@
#include <cmath>
#include <cfloat>
-const float32 MAX_VELOCITY = 200.f;
+struct Impulse {
+ Vector2 force = { 0, 0 };
+ float32 timeOfApplicationSeconds = 0.25f;
+ float32 timeAppliedSeconds = 0.f;
+ bool isDead = false;
+};
+
+const int32 NUM_IMPULSES = 4;
struct Rigidbody {
- Vector2 force = { 0, 0 };
+ int32 numImpulses = 0;
+ Impulse activeImpulses[NUM_IMPULSES];
Vector2 velocity = { 0, 0 };
Vector2 position = { 0, 0 };
float32 mass = 1.f;
void reset() {
- force = { 0, 0 };
+ numImpulses = 0;
velocity = { 0, 0 };
}
- void applyForce(Vector2 f) {
- force += 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, -50.f } * deltaTimeSeconds);
+ velocity += (Vector2 { 0.f, -9.8f } * deltaTimeSeconds);
}
void update(float32 deltaTimeSeconds) {
applyGravity(deltaTimeSeconds);
-
- Vector2 acceleration = force / mass;
- velocity += (acceleration * deltaTimeSeconds);
- if (ABS(velocity.x) > MAX_VELOCITY) {
- velocity.x = SIGN(velocity.x) * MAX_VELOCITY;
- }
- if (ABS(velocity.y) > MAX_VELOCITY) {
- velocity.y = SIGN(velocity.y) * MAX_VELOCITY;
+ 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; // Do the remainder of the time
+ i.isDead = true;
+ }
+
+ float32 impulseDtSeconds = nextTimeAppliedSeconds - i.timeAppliedSeconds;
+ force += i.force * impulseDtSeconds;
+
+
+ i.timeAppliedSeconds = nextTimeAppliedSeconds;
}
+ Vector2 acceleration = force / mass;
+ velocity += (acceleration * impulseDtSeconds);
position += (velocity * deltaTimeSeconds);
- force = Vector2 { 0.f, 0.f };
+
+ // 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++) {
+ activeImpulses[j - 1] = activeImpulses[j];
+ }
+
+ idx = idx - 1;
+ numImpulses--;
+ }
+ }
}
};
@@ -106,6 +140,7 @@ struct Rectangle {
struct Circle {
OrthographicShape shape;
Rigidbody body;
+ Vector2 force;
float32 radius = 5.f;
@@ -204,7 +239,9 @@ void update(float32 deltaTimeSeconds, void* userData) {
if (isPointInRectangle(pointer.body.position, rectangle)) {
if (!isIntersectingPointer) {
isIntersectingPointer = true;
- rectangle.body.force += pointer.body.force;
+ Impulse i;
+ i.force = pointer.force;
+ rectangle.body.applyImpulse(i);
}
} else if (isIntersectingPointer) {
isIntersectingPointer = false;
@@ -263,8 +300,8 @@ EM_BOOL onMouseMove(int eventType, const EmscriptenMouseEvent *mouseEvent, void
return true;
}
- pointer.body.force.x = static_cast<float32>(mouseEvent->movementX) * 1000.f;
- pointer.body.force.y = static_cast<float32>(-mouseEvent->movementY) * 1000.f;
+ pointer.force.x = static_cast<float32>(mouseEvent->movementX) * 2000.f;
+ pointer.force.y = static_cast<float32>(-mouseEvent->movementY) * 2000.f;
pointer.body.position.x = static_cast<float32>(mouseEvent->targetX);
pointer.body.position.y = static_cast<float32>(600.f - mouseEvent->targetY);