Hey there everyone, I've tried for many hours to find some code to partition a concave polygon into multiple convex ones, so it can be used as a physics body in cocos2d. Well, I've spend the afternoon making my own, and I'm putting it up for free for everyone to enjoy
Github link: https://github.com/clickstan/polygonPartition
The code uses a super fast underlying C++ polygon partitioning code base, and gives the minimum number of needed polygons. And I've written a nice helper class to work with it easily.
I have a tilemap in Tiled where I made a custom polygon (for physics collisions), it looks like this:
With the normal CCPhysics methods, they only accept convex polygons, so this wouldn't work... But now it does!
I want to make a physics body with this polygon, so I simply read in the polygon from the file, make an array of points, and call:
//init points array
CGPoint *groundPolyPoints=(CGPoint *)malloc(100*sizeof(CGPoint));
//fill the polygon points array from file, or any way you like
//create physics shape from concave polygon!
tileMap.physicsBody=[PolygonHelper physicsBodyFromConcavePolygonPoints:groundPolyPoints NumPoints:pointsCount CornerRadius:1];
Here is how it looks (using CCDrawNode to draw the polygons):
debug draw on: (you can see how the polygons got split up):
Hopefully this helps anyone who had trouble making a physics body from a big polygon. Works great with prkit (https://github.com/kenmadsen/cocos2d-PRKit) (for adding physics body to PRKit texture polygon!)