diff options
author | Matthew Kosarek <mattkae@protonmail.com> | 2021-06-27 15:24:07 -0400 |
---|---|---|
committer | Matthew Kosarek <mattkae@protonmail.com> | 2021-06-27 15:24:07 -0400 |
commit | 52d43a63d02e973f28aa30b768eddc042a4f155b (patch) | |
tree | 852de9a02ff9c6298c5b41dcc466786ed8af3be6 /2d/_collisions/polygon_polygon/snippet1.cpp | |
parent | 28d0d84638ba1ba45696f3fa86cb18e694f280a5 (diff) |
Some minor updates to SAT explanation, and made icons work everywhere
Diffstat (limited to '2d/_collisions/polygon_polygon/snippet1.cpp')
-rw-r--r-- | 2d/_collisions/polygon_polygon/snippet1.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/2d/_collisions/polygon_polygon/snippet1.cpp b/2d/_collisions/polygon_polygon/snippet1.cpp new file mode 100644 index 0000000..3277cf9 --- /dev/null +++ b/2d/_collisions/polygon_polygon/snippet1.cpp @@ -0,0 +1,52 @@ + +Vector2 getProjection(Vector2* vertices, int numVertices, Vector2 axis) { + // Find the min and max vertex projections + float32 min = axis.dot(vertices[0]); + float32 max = min; + + for (int v = 1; v < numVertices; v++) { + float32 d = axis.dot(vertices[v]); + + if (d < min) { + min = d; + } else if (d > max) { + max = d; + } + } + + return Vector2 { min, max }; +} + +bool projectionsOverlap(Vector2 first, Vector2 second) { + return first.x <= second.y && second.x <= first.y; +} + +bool doIntersect(ConvexPolygon* first, ConvexPolygon* second) { + IntersectionResult ir; + + // Check agaisnt the edges of the first polygon + for (int i = 0; i < first->numVertices; i++) { + Vector2 normal = first->edges[i].normal; + + Vector2 firstProj = getProjection(first->transformedVertices, first->numVertices, normal); + Vector2 secondProj = getProjection(second->transformedVertices, second->numVertices, normal); + + if (!projectionsOverlap(firstProj, secondProj)) { + return false; + } + } + + // Check against the edges of the second polygon + for (int i = 0; i < second->numVertices; i++) { + Vector2 normal = second->edges[i].normal; + + Vector2 firstProj = getProjection(first->transformedVertices, first->numVertices, normal); + Vector2 secondProj = getProjection(second->transformedVertices, second->numVertices, normal); + + if (!projectionsOverlap(firstProj, secondProj)) { + return false; + } + } + + return true; +} |