Cutting and Splitting Polygons

Nibbie
Posts: 2
Joined: 2011.09
Post: #1
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?
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
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-Phy...ancy.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.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2011.09
Post: #3
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?
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #4
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.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Combining then splitting angular and linear velocity reubert 4 4,711 Sep 5, 2006 12:47 AM
Last Post: reubert
  Collision Detection with concave-Polygons mutle 2 5,899 Jun 30, 2003 08:31 AM
Last Post: codemattic