I am using CollisionPolygon2D
to implement collision detection for polygons with 3-16 vertices. It works for polygons with 3-8 vertices, but additional vertices (9+) don't seem to register at all.
This is how I implement collision detection for a nonagon in C#:
var unit = 256f / 96f;
_collisionShape2D = _node.CreateComponent<CollisionPolygon2D>();
_collisionShape2D.As<CollisionPolygon2D>().VertexCount = 9;
_collisionShape2D.As<CollisionPolygon2D>().SetVertex(0, new Vector2(-0.3515625f, -1.000000f) / 2 * unit);
_collisionShape2D.As<CollisionPolygon2D>().SetVertex(1, new Vector2(+0.3515625f, -1.000000f) / 2 * unit);
_collisionShape2D.As<CollisionPolygon2D>().SetVertex(2, new Vector2(+0.8593750f, -0.515625f) / 2 * unit);
_collisionShape2D.As<CollisionPolygon2D>().SetVertex(3, new Vector2(+1.0000000f, +0.140625f) / 2 * unit);
_collisionShape2D.As<CollisionPolygon2D>().SetVertex(4, new Vector2(+0.6250000f, +0.750000f) / 2 * unit);
_collisionShape2D.As<CollisionPolygon2D>().SetVertex(5, new Vector2(0000000000f, +1.000000f) / 2 * unit);
_collisionShape2D.As<CollisionPolygon2D>().SetVertex(6, new Vector2(-0.6250000f, +0.750000f) / 2 * unit);
_collisionShape2D.As<CollisionPolygon2D>().SetVertex(7, new Vector2(-1.0000000f, +0.140625f) / 2 * unit);
_collisionShape2D.As<CollisionPolygon2D>().SetVertex(8, new Vector2(-0.8593750f, -0.515625f) / 2 * unit);
Note, the nonagon is a Sprite2D
with a width and height of exactly 256 pixels. In my project, 1 world unit equals 96 pixels; hence, why I multiply by unit
.
If this were an octagon, it would work as expected; however, that ninth vertex doesn't seem to register. What actually happens is the eight vertex connects with the first vertex and the ninth is ignored entirely. The same applies to polygons with any number of additional vertices (decagon, hendecagon, and so forth). This leads me to believe CollisionPolygon2D
simply does not work for polygons with 9+ vertices.
According to the official documentation:
Polygon shapes are solid convex polygons. A polygon is convex when all line segments connecting two points in the interior do not cross any edge of the polygon. A polygon must have 3 or more vertices.
Polygons vertices winding doesn't matter.
The nonagon in question is convex as far as I am aware. This is what it looks like:
Courtesy of Wikipedia.
As you can see, none of the angles exceed 180, indicating it is indeed convex. The following demonstrates the collision produced in my example:
I tried replacing CollisionPolygon2D
with CollisionChain2D
using the exact same vertices to see if that would work instead, but I get really weird collision behavior (as if the size is either very small or just nonexistent; for instance, the object does not rotate as you'd expect when it falls and hits other objects).
For craps and giggles, I decided to add a tenth vertex, which equals the first, thinking maybe I needed to "close" the shape; that theory didn't play out well either...
I did get CollisionChain2D
to work for some concave polygons with <=6 vertices, but it doesn't appear to work at all for convex polygons with any number of vertices.
What am I missing here?