diff options
Diffstat (limited to 'frontend/2d/_collisions/rectangle_rectangle/main.cpp')
-rw-r--r-- | frontend/2d/_collisions/rectangle_rectangle/main.cpp | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/frontend/2d/_collisions/rectangle_rectangle/main.cpp b/frontend/2d/_collisions/rectangle_rectangle/main.cpp new file mode 100644 index 0000000..eda76d4 --- /dev/null +++ b/frontend/2d/_collisions/rectangle_rectangle/main.cpp @@ -0,0 +1,249 @@ +#include "../../../shared_cpp/OrthographicRenderer.h" +#include "../../../shared_cpp/types.h" +#include "../../../shared_cpp/WebglContext.h" +#include "../../../shared_cpp/mathlib.h" +#include "../../../shared_cpp/MainLoop.h" +#include <cstdio> +#include <cmath> +#include <emscripten/html5.h> +#include <unistd.h> +#include <pthread.h> +#include <cmath> + +struct Rigidbody { + Vector2 force = { 0, 0 }; + Vector2 velocity = { 0, 0 }; + Vector2 position = { 0, 0 }; + float32 rotationalVelocity = 0.f; + float32 rotation = 0.f; + float32 mass = 1.f; + float32 cofOfRestition = 1.f; + float32 momentOfInertia = 0.f; + + void reset() { + force = { 0, 0 }; + velocity = { 0, 0 }; + rotationalVelocity = 0.f; + rotation = 0.f; + } + + void applyForce(Vector2 f) { + force += f; + } + + void applyGravity(float32 deltaTimeSeconds) { + velocity += (Vector2 { 0.f, -96.f } * deltaTimeSeconds); + } + + void update(float32 deltaTimeSeconds) { + applyGravity(deltaTimeSeconds); + + Vector2 acceleration = force / mass; + velocity += (acceleration * deltaTimeSeconds); + position += (velocity * deltaTimeSeconds); + force = Vector2 { 0.f, 0.f }; + + rotation += (rotationalVelocity * deltaTimeSeconds); + } + + void setMomentOfInertia(float32 moi) { + momentOfInertia = moi; + } +}; + +struct IntersectionResult { + bool intersect = false; + Vector2 collisionNormal; + Vector2 relativeVelocity; + Vector2 firstPointOfApplication; + Vector2 secondPointOfApplication; +}; + +struct Rectangle { + OrthographicShape shape; + Rigidbody body; + Vector4 color; + float32 width = 0.f; + float32 height = 0.f; + + void load(OrthographicRenderer* renderer, Vector4 inColor, float32 inWidth, float32 inHeight) { + color = inColor.toNormalizedColor(); + width = inWidth;; + height = inHeight; + + float32 halfWidth = width / 2.f; + float32 halfHeight = height / 2.f; + + OrthographicVertex vertices[6]; + vertices[0].position = Vector2 { -halfWidth, -halfHeight }; + vertices[1].position = Vector2 { -halfWidth, halfHeight }; + vertices[2].position = Vector2 { halfWidth, halfHeight }; + vertices[3].position = Vector2 { -halfWidth, -halfHeight }; + vertices[4].position = Vector2 { halfWidth, -halfHeight }; + vertices[5].position = Vector2 { halfWidth, halfHeight }; + + for (int32 i = 0; i < 6; i++) { + vertices[i].color = color; + } + + shape.load(vertices, 6, renderer); + body.reset(); + body.momentOfInertia = (1.f / 12.f) * body.mass * (width + height * height * height); + } + + void update(float32 dtSeconds) { + body.update(dtSeconds); + shape.model = Mat4x4().translateByVec2(body.position).rotate2D(body.rotation); + } + + void render(OrthographicRenderer* renderer) { + shape.render(renderer); + } + + void unload() { + shape.unload(); + } +}; + + +EM_BOOL onPlayClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData); +EM_BOOL onStopClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData); + +void load(); +void update(float32 time, void* userData); +void unload(); + +WebglContext context; +OrthographicRenderer renderer; +MainLoop mainLoop; +Rectangle rectangleList[4]; + +int main() { + context.init("#gl_canvas"); + emscripten_set_click_callback("#gl_canvas_play", NULL, false, onPlayClicked); + emscripten_set_click_callback("#gl_canvas_stop", NULL, false, onStopClicked); + return 0; +} + +void load() { + renderer.load(&context); + + rectangleList[0].load(&renderer, Vector4 { 135.f, 135.f, 35.f, 255.f }, 24.f, 32.f); + rectangleList[0].body.position = Vector2 { context.width / 3.f, context.height / 3.f }; + rectangleList[0].body.velocity = Vector2 { 100.f, 250.f }; + rectangleList[0].body.rotation = 0.2f; + + rectangleList[1].load(&renderer, Vector4 { 35.f, 135.f, 135.f, 255.f }, 64.f, 96.f); + rectangleList[1].body.position = Vector2 { context.width / 3.f, context.height * (2.f / 3.f) }; + rectangleList[1].body.rotation = 1.3f; + + rectangleList[2].load(&renderer, Vector4 { 135.f, 35.f, 135.f, 255.f }, 64.f, 32.f); + rectangleList[2].body.position = Vector2 { context.width * (2.f / 3.f), context.height / 3.f }; + rectangleList[2].body.velocity = Vector2 { -100.f, 250.f }; + rectangleList[2].body.rotation = -0.5f; + + rectangleList[3].load(&renderer, Vector4 { 35.f, 135.f, 35.f, 255.f }, 8.f, 16.f); + rectangleList[3].body.position = Vector2 { context.width * (2.f / 3.f), context.height * (2.f / 3.f) }; + rectangleList[3].body.rotation = -3.23f; + + mainLoop.run(update); +} + + +const float32 EPSILON = 1.f; +IntersectionResult getIntersection(Rectangle* first, Rectangle* second) { + IntersectionResult ir; + + return ir; +} + +void resolveCollision(Rigidbody* first, Rigidbody* second, IntersectionResult* ir) { + Vector2 relativeVelocity = ir->relativeVelocity; + Vector2 collisionNormal = ir->collisionNormal; + Vector2 firstPerp = ir->firstPointOfApplication.getPerp(); + Vector2 secondPerp = ir->secondPointOfApplication.getPerp(); + float32 firstPerpNorm = firstPerp.dot(collisionNormal); + float32 sndPerpNorm = secondPerp.dot(collisionNormal); + + float32 cofOfRestition = (first->cofOfRestition + second->cofOfRestition) / 2.f; + float32 numerator = (relativeVelocity * (-1 * (1.f + cofOfRestition))).dot(collisionNormal); + float32 linearDenomPart = collisionNormal.dot(collisionNormal * (1.f / first->mass + 1.f / second->mass)); + float32 rotationalDenomPart = (firstPerpNorm * firstPerpNorm) / first->momentOfInertia + (sndPerpNorm * sndPerpNorm) / second->momentOfInertia; + + float32 impulseMagnitude = numerator / (linearDenomPart);// + rotationalDenomPart); + first->velocity = first->velocity + (collisionNormal * (impulseMagnitude / first->mass)); + second->velocity = second->velocity - (collisionNormal * (impulseMagnitude / second->mass)); + first->rotationalVelocity = first->rotationalVelocity + firstPerp.dot(collisionNormal * impulseMagnitude) / first->momentOfInertia; + second->rotationalVelocity = second->rotationalVelocity - secondPerp.dot(collisionNormal * impulseMagnitude) / second->momentOfInertia; +} + +void update(float32 deltaTimeSeconds, void* userData) { + // Update + for (int r = 0; r < 4; r++) { + rectangleList[r].update(deltaTimeSeconds); + } + + /*for (int32 segmentIndex = 0; segmentIndex < 4; segmentIndex++) { + IntersectionResult ir = getIntersection(&rectangle, &segmentList[segmentIndex]); + if (!ir.intersect) { + continue; + } + + // Handle collison here + IntersectionResult irCopy = ir; + float32 copyDt = deltaTimeSeconds; + + do { + ir = irCopy; + rectangle = rectCopy; + copyDt = copyDt /= 2.f; + + rectangle.update(copyDt); + irCopy = getIntersection(&rectangle, &segmentList[segmentIndex]); + + if (copyDt <= 0.f) { + printf("Error: Should not be happening.\n"); + break; + } + + } while (irCopy.intersect); + + printf("Found intersection at timestamp: %f\n", copyDt); + + resolveCollision(&rectangle.body, &segmentList[segmentIndex].body, &ir); + float32 frameTimeRemaining = deltaTimeSeconds - copyDt; + + update(frameTimeRemaining, userData); + return; + }*/ + + // Renderer + renderer.render(); + for (int r = 0; r < 4; r++) { + rectangleList[r].render(&renderer); + } +} + +void unload() { + mainLoop.stop(); + renderer.unload(); + for (int r = 0; r < 4; r++) { + rectangleList[r].unload(); + } +} + +// +// Interactions with DOM handled below +// +EM_BOOL onPlayClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData) { + printf("Play clicked\n"); + + load(); + return true; +} + +EM_BOOL onStopClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData) { + printf("Stop clicked\n"); + unload(); + return true; +} |