detect-collisions: Concave polygons not working as expected

I am having some trouble with concave polygons. The collisions were not being detected with my concave polygon, so I did a little bit of testing with a different concave polygon. It should be noted that I am checking the polygon collider against a circle collider with a radius of 1.

This is the concave polygon I tested with:

[
    { x: -13.25, y: -7.51 },
    { x: -13.25, y: 7.51 },
    { x: 0.77, y: 7.51 },
    { x: -3, y: 0.41 },
    { x: 0.77, y: -7.51 }
]

which creates a polygon that looks like this:

image

the collisions work as expected on the right side, but on the left edge it does not. My character is able to pass through the left edge.

removing the concavity creates a perfectly functional collider

[
    { x: -13.25, y: -7.51 },
    { x: -13.25, y: 7.51 },
    { x: 0.77, y: 7.51 },
    // { x: -3, y: 0.41 },
    { x: 0.77, y: -7.51 }
]

my original collider, which did not work at all, was this:

[
    { x: -11.25, y: -6.76 },
    { x: -12.5, y: -6.76 },
    { x: -12.5, y: 6.75 },
    { x: -3.1, y: 6.75 },
    { x: -3.1, y: 0.41 },
    { x: -2.35, y: 0.41 },
    { x: -2.35, y: 6.75 },
    { x: 0.77, y: 6.75 },
    { x: 0.77, y: 7.5 },
    { x: -13.25, y: 7.5 },
    { x: -13.25, y: -7.51 },
    { x: -11.25, y: -7.51 }
]

I don’t know if this is related, but the length of body.convexPolygons and the length of body.getConvex() do not match. With the former being over double the length of the latter.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 25 (23 by maintainers)

Most upvoted comments

Alright, I fixed it

You need to do this.response.clear(); in the reduces

Should I create a pull request?

Yes, this is a known issue. It is because aInB for concave polygons just uses the bounding box. At the moment, this is the best solution we have.

You can check out an image I made displaying this pitfall in #34

The theory was that having something is better than having nothing (there previously was no aInB for the polygons)

Ah alright, thanks for the clarification!

This appears to still be broken in some instances. Here’s an example scenario where both lines when colliding with a concave polygon should have aInB set to false, but it’s set to true: https://codesandbox.io/s/nifty-buck-t5knk0

Yes, this is a known issue. It is because aInB for polygons just uses the bounding box. At the moment, this is the best solution we have.

You can check out an image I made displaying this pitfall in #34

Played around with it, found no problems.

try v6.3.13 @Codezilluh best regards

Ok, I’ll try it out right now. Sounds solid.

I merged your MR it was great thank you!

I also saw your commend about wrong aInB and bInA for non convex and I’ve added 1 test for the case and

assured that even outside that if (collisionVector) { block, your util function is used for non convex polygons

in the cases where there is no concave polygon involded those overwrites wont be involved

still theres that response clear so in near future I’ll try to add some more tests.

cheers!

can you check v6.3.10 ?

Works flawlessly! (as far as I can tell, and I am using pretty complex polygons)

I am happy

Thank you for finding all those bugs and finding a solution

do you have any suggestions as how to solve aInB and bInA for concave polygons ?

I’ll have to look into how it is handled for normal polygons. It’s tough because simply looping and comparing the polygons will not work for most cases. Maybe something fancy could be done with the points? It may end up being better just to use the AABB for that. I am not sure if aInB or bInA would even be useful for concave polygons that are not the same, as the collision system should be able to take care of it (haven’t actually tried). I am currently just using aInB to see if I need to nudge the object in a random direction.

I was also thinking about simple aInB and bInA with AABB it’s still better than nothing and currently we have close to nothing (in case of aInB or bInA) if one is concave I’m also a bit sorry I kind of took you the opportunity to contribute so how about you open a merge request with this change if you feel you want to…

best regards

Cool, now it appears the polygons are being generated properly.

Yes I fixed the convexPolygons array length to be === this.getConvex().length

also fixed collision resolution between circles and concaves

There might be a bug with actual collision resolution (the overlaps), I haven’t look into it much yet. This is the issue I am facing now: when heading into perpendicular walls (haven’t tested other angles), you can pass through one of the walls.

I propably have to add all vectors from all polygons, will look into it soon maybe at the weekend or today depending on how complex it is