Creating a polygon "container" out of line segments

Official forum for the Chipmunk2D Physics Library.
Post Reply
eyerobot
Posts: 2
Joined: Mon Jul 08, 2013 6:37 pm
Contact:

Creating a polygon "container" out of line segments

Post 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!
eyerobot
Posts: 2
Joined: Mon Jul 08, 2013 6:37 pm
Contact:

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

Post 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!
Projet_Peip
Posts: 4
Joined: Thu Feb 20, 2014 5:00 am
Contact:

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

Post 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 ?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

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

Post by slembcke »

Need more details. What is the name of the polygon function, and how is it failing? Is there an error message?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Projet_Peip
Posts: 4
Joined: Thu Feb 20, 2014 5:00 am
Contact:

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

Post 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 ?
viblo
Posts: 206
Joined: Tue Aug 21, 2007 3:12 pm
Contact:

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

Post 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.
http://www.pymunk.org - A python library built on top of Chipmunk to let you easily get cool 2d physics in your python game/app
Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests