From 94e3f8516ca45353f49af39c4349b12aa118f287 Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Wed, 30 Jun 2021 19:21:42 -0400 Subject: Small optimization for polygonal intersections --- 2d/_collisions/polygon_polygon/dist/output.wasm | Bin 57631 -> 57769 bytes 2d/_collisions/polygon_polygon/main.cpp | 31 +++++++++++++++++------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/2d/_collisions/polygon_polygon/dist/output.wasm b/2d/_collisions/polygon_polygon/dist/output.wasm index 5b2ba39..d865038 100755 Binary files a/2d/_collisions/polygon_polygon/dist/output.wasm and b/2d/_collisions/polygon_polygon/dist/output.wasm differ diff --git a/2d/_collisions/polygon_polygon/main.cpp b/2d/_collisions/polygon_polygon/main.cpp index 8648e94..ff01e52 100644 --- a/2d/_collisions/polygon_polygon/main.cpp +++ b/2d/_collisions/polygon_polygon/main.cpp @@ -115,8 +115,7 @@ struct ConvexPolygon { Rigidbody previousBody; Vector4 color; int32 numVertices = 3; - float32 width = 0.f; - float32 height = 0.f; + float32 radius = 0.f; Vector2* originalVertices; Vector2* transformedVertices; @@ -138,14 +137,14 @@ struct ConvexPolygon { int32 indexPosition = vidx * 3; float32 firstAngle = angleIncrements * vidx; - shaderVertices[indexPosition].position = { cosf(firstAngle) * width, sinf(firstAngle) * height }; + shaderVertices[indexPosition].position = { cosf(firstAngle) * radius, sinf(firstAngle) * radius }; originalVertices[vidx] = shaderVertices[indexPosition].position; shaderVertices[indexPosition + 1].position = { 0.f, 0.f }; float32 secondAngle = angleIncrements * (vidx + 1); - shaderVertices[indexPosition + 2].position = { cosf(secondAngle) * width, sinf(secondAngle) * height }; + shaderVertices[indexPosition + 2].position = { cosf(secondAngle) * radius, sinf(secondAngle) * radius }; // Apply some global stylings for (int subIdx = 0; subIdx < 3; subIdx++) { @@ -159,7 +158,7 @@ struct ConvexPolygon { edges = new Edge[numVertices]; // This will be filled in later when we are doing our SAT calculation. // Calculate moment of inertia - body.momentOfInertia = (PI * (width * height * body.mass)) / 4.f; + body.momentOfInertia = (PI * (radius * radius * body.mass)) / 4.f; } void update(float32 dtSeconds) { @@ -167,7 +166,9 @@ struct ConvexPolygon { body.update(dtSeconds); shape.model = Mat4x4().translateByVec2(body.position).rotate2D(body.rotation); + } + void calculateTransformedVertices() { // Populate the current position of our edges. Note that this might be slow depending // on how many edges your shaped have. for (int vidx = 0; vidx < numVertices; vidx++) { @@ -177,7 +178,7 @@ struct ConvexPolygon { Vector2 end = shape.model * originalVertices[vidx == numVertices - 1 ? 0 : vidx + 1]; edges[vidx] = { (end - start).getPerp(), start, end }; } - } + } void restorePreviousBody() { body = previousBody; @@ -244,8 +245,7 @@ void load() { polygons[index].body.mass = 8.f; } - polygons[index].width = (polygons[index].body.mass / 2.f) * 20.f; - polygons[index].height = (polygons[index].body.mass / 2.f) * 20.f; + polygons[index].radius = (polygons[index].body.mass / 2.f) * 20.f; polygons[index].numVertices = (index + 1) * 3; polygons[index].color = Vector4 { @@ -383,17 +383,30 @@ void resolveCollision(Rigidbody* first, Rigidbody* second, IntersectionResult* i second->rotationalVelocity = second->rotationalVelocity - secondPerp.dot(collisionNormal * impulseMagnitude) / second->momentOfInertia; } +bool circleHitBoxesIntersect(ConvexPolygon* first, ConvexPolygon* second) { + return (first->body.position - second->body.position).length() <= (first->radius + second->radius); +} + void update(float32 deltaTimeSeconds, void* userData) { // Update for (int p = 0; p < 4; p++) { polygons[p].update(deltaTimeSeconds); } - // Check collisions with other rectangles + // Collision detection for (int i = 0; i < 4; i++) { ConvexPolygon* first = &polygons[i]; for (int j = i + 1; j < 4; j++) { ConvexPolygon* second = &polygons[j]; + + if (!circleHitBoxesIntersect(first, second)) { + continue; + } + + printf("Might intersect\n"); + + first->calculateTransformedVertices(); + second->calculateTransformedVertices(); IntersectionResult ir = getIntersection(first, second); if (!ir.intersect) { -- cgit v1.2.1