iDevGames Forums
Cutting and Splitting Polygons - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Cutting and Splitting Polygons (/thread-9328.html)



Cutting and Splitting Polygons - Maedi - Sep 12, 2011 06:30 AM

In OpenGL, Quartz or Cocos2d is it possible to create a 2d polygon and then programatically intersect this shape with a line at any angle, creating 2 new shapes from the cut line?

For example: a 4 sided rectangle has line going through the shape at any angle from one side to another. This line is then used to split the polygon into 2 new shapes.

What kind of frameworks or technologies should I look into for doing things like this?


RE: Cutting and Splitting Polygons - Skorche - Sep 12, 2011 10:47 AM

If you are exclusively cutting convex polygons it's trivial to do. I have a snippet of code here that clips a polygon by a horizontal line, but it would be an easy modification to make it work with an arbitrary axis:
https://github.com/slembcke/Chipmunk-Physics/blob/master/Demo/Buoyancy.c#L58

For concave polygons it's little trickier as you can end up with more than 2 pieces, but the concept is basically the same.


RE: Cutting and Splitting Polygons - Maedi - Sep 13, 2011 06:32 AM

Thanks for the link. So I should use a framework like Cocos2d to draw the polygons and then use Chipmunk to do the physics side?

In my use case the polygons are 2d shapes in a 3d environment, will the new Cocos3d allow you to access Chipmunk's 2d physics engine?


RE: Cutting and Splitting Polygons - Skorche - Sep 13, 2011 07:40 AM

I wouldn't use Cocos2D unless you are drawing sprites, it's sort of a pain to do your own GL drawing in Cocos2D. I'd certainly recommend Chipmunk for physics of course. Rasp (but there is also Box2D)

I can't speak for Cocos3D having never tried it. I can't imagine there would be anything preventing you from using the libraries together if you wanted to.