Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not clear what polygon shapes are allowed #68

Closed
whitespacecode opened this issue May 12, 2023 · 1 comment
Closed

Not clear what polygon shapes are allowed #68

whitespacecode opened this issue May 12, 2023 · 1 comment

Comments

@whitespacecode
Copy link

whitespacecode commented May 12, 2023

Okay maybe i'm doing something wrong but i'm building 2 polygon's and i want to detect wether the second shape is not colliding anymore with the first shape.

I found the collision would treat the first polygon using it bounds shape instead of the real shape. So it still treats the first polygon as a box shape instead of the L shape i'm building.

Setup for the first polygon is a shape

new Polygon(new Vector(), [
        new Vector(185,0),
        new Vector(185,160),
        new Vector(0,160),
        new Vector(0,270),
        new Vector(500,270),
        new Vector(500,0),
        new Vector(185,0),
]);

Setup for the second polygon, that can move around

new Polygon(new Vector(mouseposition.x, mouseposition.y), [
    new Vector(-object.width, -object.height),
    new Vector( object.width, -object.height),
    new Vector( object.width,  object.height),
    new Vector(-object.width,  object.height)
]);

Testing the collision

function isWithinBounds(objectPolygon, roomPolygon) {
    return testPolygonPolygon(objectPolygon, roomPolygon)
}

Visual representation what is happening. The object is clearly 'outside' the other polygon but it's still detecting collision. I'm using Pixijs to draw the polygon's i get using 'drawPolygon' and passing the polygon.points.
debug

@whitespacecode
Copy link
Author

Found my mistake.

Collisions uses the Separating Axis Theorem (SAT) for its narrow-phase collision tests. One caveat to SAT is that it only works properly on convex bodies.

So knowing what the limitations where i used a different library to decompose my Polygon into different convex bodies. Then looping the polygons and test for collision

const decomposedPolygons = decomposePolygon(roomPolygon.points);

for (let i = 0; i < decomposedPolygons.length; i++) {
      if (testPolygonPolygon(objectPolygon, decomposedPolygons[i])) {
          return false; // Collision detected, object is in bounds
      }
}

I used https://github.com/schteppe/poly-decomp.js to convert my polygon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant