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:

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)
Alright, I fixed it
You need to do
this.response.clear();in thereducesShould I create a pull request?
Ah alright, thanks for the clarification!
Yes, this is a known issue. It is because
aInBfor 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.
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 polygonsin 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!
I am happy
Thank you for finding all those bugs and finding a solution
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
Yes I fixed the convexPolygons array length to be === this.getConvex().length
also fixed collision resolution between circles and concaves
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