Page 1 of 1

Creating a polygon "container" out of line segments

Posted: Thu Jul 11, 2013 3:07 pm
by eyerobot
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:

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];

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!

Re: Creating a polygon "container" out of line segments

Posted: Sat Jul 13, 2013 2:33 pm
by eyerobot
Okay, duh. I realized late last night that I didn't have to make a separate body and update its position based on the body of the larger object (which was what was causing the problems, the container kept being placed on top of the object inside it, causing it to pop out), I could just add the segments to the main body itself, just on a different layer.

Rookie mistake, sorry everyone!

Re: Creating a polygon "container" out of line segments

Posted: Thu Feb 27, 2014 7:32 am
by Projet_Peip
Hi everyone, I'm trying to do something more or less similar using pymunk. I'm trying to build a static empty circle using the polygon function. However whatever I tied, it keeps failing.
I am a huge newbie, just started using pymunk. Could anyone help me ?

Re: Creating a polygon "container" out of line segments

Posted: Thu Feb 27, 2014 12:20 pm
by slembcke
Need more details. What is the name of the polygon function, and how is it failing? Is there an error message?

Re: Creating a polygon "container" out of line segments

Posted: Thu Mar 27, 2014 7:07 am
by Projet_Peip
Thank you for your reply. I have advanced in this project, here is what I have managed to do using pymunk:
I've created a body in space, an empty circle made of polygons. I used for to create enough verts. The problem is that now, I wish to draw this circle (made out of polygons) using points = shape.get_vertices. Everything's fine except that it only takes the points from the first iteration of my for function (which I used to create my verts)
What am I missing ? Must I create a loop to draw the polygon as well ?

Re: Creating a polygon "container" out of line segments

Posted: Fri May 09, 2014 1:05 am
by viblo
Projet_Peip wrote:Thank you for your reply. I have advanced in this project, here is what I have managed to do using pymunk:
I've created a body in space, an empty circle made of polygons. I used for to create enough verts. The problem is that now, I wish to draw this circle (made out of polygons) using points = shape.get_vertices. Everything's fine except that it only takes the points from the first iteration of my for function (which I used to create my verts)
What am I missing ? Must I create a loop to draw the polygon as well ?


Take a look at the debug draw methods for how to draw the vertices. But shape.get_vertices() should give you the vertices at the time you call the method, and not how they where in the beginnning.