From 52d43a63d02e973f28aa30b768eddc042a4f155b Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Sun, 27 Jun 2021 15:24:07 -0400 Subject: Some minor updates to SAT explanation, and made icons work everywhere --- 2d/_collisions/polygon_polygon.html | 60 ++++++++++++++++++++++-- 2d/_collisions/polygon_polygon.html.content | 6 ++- 2d/_collisions/polygon_polygon/dist/output.wasm | Bin 57485 -> 57700 bytes 2d/_collisions/polygon_polygon/main.cpp | 27 +++++------ 2d/_collisions/polygon_polygon/snippet1.cpp | 52 ++++++++++++++++++++ 2d/_collisions/rectangle_line.html | 2 +- 2d/_collisions/rectangle_rectangle.html | 2 +- 7 files changed, 127 insertions(+), 22 deletions(-) create mode 100644 2d/_collisions/polygon_polygon/snippet1.cpp (limited to '2d/_collisions') diff --git a/2d/_collisions/polygon_polygon.html b/2d/_collisions/polygon_polygon.html index 5d360af..4c4698a 100644 --- a/2d/_collisions/polygon_polygon.html +++ b/2d/_collisions/polygon_polygon.html @@ -4,7 +4,7 @@ Physics for Games - + @@ -159,13 +159,65 @@
  • If the projections overlap for each edge of both shapes, the shapes are intersecting. Return true.
  • - And that is all there is to finding the intersection between two convex polygons. -

    + And that is all there is to finding the intersection between two convex polygons. Here is the code snippet that does this if you are interested: + +
    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;
    +}
    +

    SAT Collision Resolution

    - Now that we know our objects have intersecting, we want to be able to send them tumbling away from each other to simulate a collision. To do this, we will need to find the following things: + Now that we know our objects have intersected, we want to be able to send them tumbling away from each other to simulate a collision. To do this, we will need to find the following things:

    SAT Collision Resolution

    - Now that we know our objects have intersecting, we want to be able to send them tumbling away from each other to simulate a collision. To do this, we will need to find the following things: + Now that we know our objects have intersected, we want to be able to send them tumbling away from each other to simulate a collision. To do this, we will need to find the following things: