Hi,
I am triangulating convex shapes, so for each shape I have a set of triangles, So I have to attach all the triangle shapes to a single body. Now I am having these 2 problems to make it work:
1. To create a body: cpBodyNew(mass, cpMomentForPoly(mass, num, verts, cpvzero));
- What should be the verts? should it be a set of all the vertices of the shape or a set of vertices of a single triangle, if single triangle then which one? Also, what should be the position of body?
2. To create all triangle shapes related to the body, I am getting revered winding exception when doing: cpPolyShapeNew(body, 3, verts, cpvzero), How should I create verts array at run time so that the winding problem will not happen.
Please help.
Triangulating Convex shapes
-
- Posts: 9
- Joined: Sat Feb 19, 2011 6:02 pm
- Contact:
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Triangulating Convex shapes
One thing to keep in mind is that each shape has an overhead. Combine them into convex polygons first if you can for better performance. This also reduces problems with having "cracks" in your shapes.
The position of the body is always the center of gravity. The shapes are attached relative to that. So the origin of your vertex data should be where you want the center of gravity to be. Add the moment of inertias of all your triangles together to get the final moment of inertia.
The winding error is because all shapes need to have the same winding (counterclockwise or clockwise depending on which way you consider the y-axis to point). The simple fix is to call cpPolyValidate(verts, count) and reverse the order if it returns false.
The position of the body is always the center of gravity. The shapes are attached relative to that. So the origin of your vertex data should be where you want the center of gravity to be. Add the moment of inertias of all your triangles together to get the final moment of inertia.
The winding error is because all shapes need to have the same winding (counterclockwise or clockwise depending on which way you consider the y-axis to point). The simple fix is to call cpPolyValidate(verts, count) and reverse the order if it returns false.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 9
- Joined: Sat Feb 19, 2011 6:02 pm
- Contact:
Re: Triangulating Convex shapes
Thanks Slembcke, I will work to combine triangles to make convex shapes,
Also, What would be the size of verts array for the body? cpBodyNew(mass, cpMomentForPoly(mass, num, verts, cpvzero));
Also, What would be the size of verts array for the body? cpBodyNew(mass, cpMomentForPoly(mass, num, verts, cpvzero));
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Triangulating Convex shapes
You calculate the moment of each convex polygon separately and add them all together.
Actually, I don't know if the algorithm used in cpMomentForPoly() is specific to convex polygons. I've never actually checked.
Actually, I don't know if the algorithm used in cpMomentForPoly() is specific to convex polygons. I've never actually checked.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 9
- Joined: Sat Feb 19, 2011 6:02 pm
- Contact:
Re: Triangulating Convex shapes
It worked, Thanks Slembcke
Who is online
Users browsing this forum: No registered users and 10 guests