Creating a polygon "container" out of line segments
Posted: Thu Jul 11, 2013 3:07 pm
I'm trying to give the appearance that one body is "inside" another body by using collision layers and creating polygonal bounds out of line segments to contain the second body. However, the interior body won't stay inside the bounds 100% of the time. It's clearly interacting with the line segments, as it bounces off them and CPDebugDraw indicates that collisions are occurring. Sometimes, parts of the inside body will stick out of the bounds, and then the entire body will "pop" out of the boundaries. Here's the code I'm using to generate the boundaries (an irregular octagon) using vertices from VertexHelper:
Could it be something with the mass of the body? I wasn't completely sure what to do with that value, because the body isn't solid. After messing with the values, it seems like increasing the container's mass and segment radius and decreasing the body to be contained's mass helps, but doesn't completely fix the problem. Any help would be much appreciated!
Code: Select all
//Create "container" to hold things that are inside the bowl
int num = 8;
CGPoint verts[] = {
cpv(-11.5f, 33.2f),
cpv(9.9f, 31.9f),
cpv(33.1f, 8.1f),
cpv(33.2f, -8.5f),
cpv(9.9f, -34.2f),
cpv(-17.2f, -28.3f),
cpv(-34.2f, -1.4f),
cpv(-29.8f, 18.3f)
};
_containerBody = [ChipmunkBody bodyWithMass:15.0f andMoment:cpMomentForPoly(15.0f, num, verts, cpvzero)];
for (int i = 0; i < 8; i++){
cpVect point1 = verts[i];
cpVect point2 = verts[(i + 1) % 8];
ChipmunkShape *segmentShape = [ChipmunkSegmentShape segmentWithBody:_containerBody from:point1 to:point2 radius:10.0f];
[segmentShape setElasticity:0.5f];
[segmentShape setFriction:0.0f];
[segmentShape setLayers:10];
[_space addShape:segmentShape];
}
[_space addBody:_containerBody];