diff options
author | Matthew Kosarek <mattkae@protonmail.com> | 2021-07-06 20:24:05 -0400 |
---|---|---|
committer | Matthew Kosarek <mattkae@protonmail.com> | 2021-07-06 20:24:05 -0400 |
commit | a56ccb7392f9f7d0178c17fcef0bf60a1e7de705 (patch) | |
tree | 1eccc9ca942e0ad3fe8a53437ff16853637d8b73 /2d/_collisions | |
parent | a5bc39679d7f78c6cc263d64355f93d6b80754da (diff) |
(mkosarek) Done enough with SAT collisions for now
Diffstat (limited to '2d/_collisions')
-rw-r--r-- | 2d/_collisions/polygon_polygon.html | 5 | ||||
-rw-r--r-- | 2d/_collisions/polygon_polygon.html.content | 3 | ||||
-rwxr-xr-x | 2d/_collisions/polygon_polygon/dist/output.wasm | bin | 57746 -> 58079 bytes | |||
-rw-r--r-- | 2d/_collisions/polygon_polygon/main.cpp | 23 | ||||
-rw-r--r-- | 2d/_collisions/polygon_polygon/snippet2.cpp | 2 |
5 files changed, 32 insertions, 1 deletions
diff --git a/2d/_collisions/polygon_polygon.html b/2d/_collisions/polygon_polygon.html index 9826ba4..ace2c17 100644 --- a/2d/_collisions/polygon_polygon.html +++ b/2d/_collisions/polygon_polygon.html @@ -312,6 +312,8 @@ <span class="code_comment">// the collision later.</span> ProjectionResult getProjection(Vector2* vertices, <span class="code_keyword">int</span> numVertices, <span class="code_keyword">Vector2</span> axis) { ProjectionResult pr; + pr.minVertex = vertices[0]; + pr.maxVertex = vertices[0]; <span class="code_keyword">float32</span> min = axis.dot(vertices[0]); <span class="code_keyword">float32</span> max = min; @@ -404,6 +406,9 @@ IntersectionResult getIntersection(ConvexPolygon* first, ConvexPolygon* second) <h2 id='live_example'> Live Example of Intersection Detection </h2> + <p> + A live example of Separating Axis Theorem running with full collision detection. Click <button id='reverse_gravity'>this button</button>to reverse gravity on the simualtion. + </p> <div class="opengl_canvas_container"> <canvas id="gl_canvas" width="800" height="600"></canvas> <button id="gl_canvas_play" class="play_button"> diff --git a/2d/_collisions/polygon_polygon.html.content b/2d/_collisions/polygon_polygon.html.content index f2a9253..f2e6bfb 100644 --- a/2d/_collisions/polygon_polygon.html.content +++ b/2d/_collisions/polygon_polygon.html.content @@ -186,6 +186,9 @@ <h2 id='live_example'> Live Example of Intersection Detection </h2> + <p> + A live example of Separating Axis Theorem running with full collision detection. Click <button id='reverse_gravity'>this button</button>to reverse gravity on the simualtion. + </p> <div class="opengl_canvas_container"> <canvas id="gl_canvas" width="800" height="600"></canvas> <button id="gl_canvas_play" class="play_button"> diff --git a/2d/_collisions/polygon_polygon/dist/output.wasm b/2d/_collisions/polygon_polygon/dist/output.wasm Binary files differindex f4ef9bb..a047d19 100755 --- a/2d/_collisions/polygon_polygon/dist/output.wasm +++ b/2d/_collisions/polygon_polygon/dist/output.wasm diff --git a/2d/_collisions/polygon_polygon/main.cpp b/2d/_collisions/polygon_polygon/main.cpp index e85cc25..620edaa 100644 --- a/2d/_collisions/polygon_polygon/main.cpp +++ b/2d/_collisions/polygon_polygon/main.cpp @@ -11,6 +11,8 @@ #include <cmath> #include <cfloat> +float32 gravityDirection = -1; + struct Impulse { Vector2 force = { 0, 0 }; float32 timeOfApplicationSeconds = 0.25f; @@ -52,7 +54,7 @@ struct Rigidbody { } void applyGravity(float32 deltaTimeSeconds) { - velocity += (Vector2 { 0.f, -9.8f } * deltaTimeSeconds); + velocity += (Vector2 { 0.f, gravityDirection * 100.f } * deltaTimeSeconds); } void update(float32 deltaTimeSeconds) { @@ -204,6 +206,7 @@ struct ConvexPolygon { EM_BOOL onPlayClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData); EM_BOOL onStopClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData); +EM_BOOL onGravityReversed(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData);; void load(); void update(float32 time, void* userData); @@ -218,6 +221,7 @@ 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); + emscripten_set_click_callback("#reverse_gravity", NULL, false, onGravityReversed); return 0; } @@ -269,12 +273,19 @@ struct SATResult { struct ProjectionResult { Vector2 minVertex; + Vector2 minAdjacent1; + Vector2 minAdjacent2; + Vector2 maxVertex; + Vector2 maxAdjacent1; + Vector2 maxAdjacent2; Vector2 projection; }; ProjectionResult getProjection(Vector2* vertices, int numVertices, Vector2 axis) { ProjectionResult pr; + pr.minVertex = vertices[0]; + pr.maxVertex = vertices[0]; float32 min = axis.dot(vertices[0]); float32 max = min; @@ -334,6 +345,10 @@ bool runSatForShapesEdges(SATResult* result, ConvexPolygon* first, ConvexPolygon } else { result->overlapPoint = secondProj.maxVertex; } + + // Check if the normal from one of the edges of the overlap point is nearly perpendicular + // to the edge that you have intersected with. If so, let's call the point of intersection + // the middle of the edge. } } @@ -497,3 +512,9 @@ EM_BOOL onStopClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, voi unload(); return true; } + +EM_BOOL onGravityReversed(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData) { + printf("Reversing gravity\n"); + gravityDirection = -gravityDirection; + return true; +} diff --git a/2d/_collisions/polygon_polygon/snippet2.cpp b/2d/_collisions/polygon_polygon/snippet2.cpp index a279f65..c182060 100644 --- a/2d/_collisions/polygon_polygon/snippet2.cpp +++ b/2d/_collisions/polygon_polygon/snippet2.cpp @@ -29,6 +29,8 @@ struct ProjectionResult { // the collision later. ProjectionResult getProjection(Vector2* vertices, int numVertices, Vector2 axis) { ProjectionResult pr; + pr.minVertex = vertices[0]; + pr.maxVertex = vertices[0]; float32 min = axis.dot(vertices[0]); float32 max = min; |