Collision only occuring on a single side of a shape

Official forum for the Chipmunk2D Physics Library.
Post Reply
panzeriti
Posts: 3
Joined: Tue Oct 05, 2010 7:14 pm
Contact:

Collision only occuring on a single side of a shape

Post by panzeriti »

I am seeing an issue where I create a cpshape and collisions will only occur on a single side of the shape. If I go through the shape, then a collision will occur when i come out the other side.

I am developing on the iPhone.
Mark
Posts: 6
Joined: Thu Aug 05, 2010 1:52 am
Contact:

Re: Collision only occuring on a single side of a shape

Post by Mark »

Hi,

I've seen on these forums more than once that this kind of thing can happen if you get the winding of the vertices wrong. Make sure they are defined in a clockwise order.

-Mark
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Collision only occuring on a single side of a shape

Post by slembcke »

My best guess is also that you either made a concave or inside out polygon.

If you compile the lib in debug mode, it has an assertion to check for winding and concavity (and many other issues). I would highly recommend running at least in the simulator using debug mode. If you use the iphone_static script to compile Chipmunk, it builds the simulator lib as debug and the iPhone lib as optimized by default.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
panzeriti
Posts: 3
Joined: Tue Oct 05, 2010 7:14 pm
Contact:

Re: Collision only occuring on a single side of a shape

Post by panzeriti »

I seem to be drawing the vertices in a clockwise order and I am not receiving any debugger errors.

Here is my code :

Code: Select all

// Create our rear wall body and set it's position
	cpBody *rearWallBody = cpBodyNew(INFINITY, INFINITY);
	rearWallBody->p = cpv(-40, 0);	// x, y from bottom left/right
	
	// drawing as an offset from the defined origin
	cpVect verts2[] = { cpv(0.0, 0.0), cpv(0.0, 4000.0), cpv(39.0, 4000.0), cpv(39.0, 0.0) };
	
	// Create the shape					//body //num vertices //offset from defined origin
	cpShape *rearWallShape = cpPolyShapeNew(rearWallBody, 4, verts2, cpvzero);
	rearWallShape->e = 0.5; rearWallShape->u = 0.15;
	cpSpaceAddStaticShape(space, rearWallShape);
User avatar
pat
Posts: 53
Joined: Tue Jul 20, 2010 7:55 pm
Contact:

Re: Collision only occuring on a single side of a shape

Post by pat »

a couple of things that stick out to me are the shape is

- The shape is really large, I'm not sure if this is a issue though. I'm not sure exactly how poly shapes collide, but from working with bsp if you have really long edges sometimes the plane math will allow things to pass through.

- You may want to try making it thicker. Depending on the velocity of things and how small your step interval is things will miss collisions at high velocities since chipmunk does not employ sweep testing. So i mean alternatively try turning your step interval down to like 1/60 or less.

+ If your using the newest chipmunk with sleeping bodys in cpPolyShapeNew you should not make a new body but instead on call it like this:

Code: Select all

cpShape *rearWallShape = cpPolyShapeNew(&space->staticBody, 4, verts2, cpv(-40,0));
Unless you really want it to be rogue, Having it rogue will not allow sleeping. And you could, (i'm not sure if should) use cpSpaceAddShape at this point which will basically end up calling cpSpaceAddStaticShape internally once seeing its a shape owned by the spaces staticBody.
Thinking about this more, Maybe your supposed to just pass null for the body.

+ You might want to try using a segment shape instead,

Code: Select all

cpShape *rearWallShape = cpSegmentShapeNew(&space->staticBody, cpv(-20.5,0), cpv(-20.5, 4000.0), 39.0);
(which will have rounded caps that go -39 to +4039 on Y, segments dont collide with eachother however, but collide with everything else)

And you are doing things correctly as far as winding clockwise. I think one or more of the above listed things with a - could fix this but things with a + might be worth trying or doing.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Collision only occuring on a single side of a shape

Post by slembcke »

Yes, in Chipmunk 5.3 and up the space has a static body that you can use instead of creating a "rogue" body. Required if you want objects to be able to sleep, though should otherwise be functionally equivalent to using your own body. Unless it's a bug, this shouldn't be causing the problem. Fast moving objects are another possibility as Pat pointed out, I hadn't thought of that.

@Pat You can use NULL instead of &space->staticBody if you want. It's an undocumented convenience feature at the moment. It hasn't been well tested that all functions that expect a body handle being passed NULL gracefully. Also, when you add a joint or shape with a NULL body pointer, the space actually overwrites the pointer to the static body over it. A couple people have expressed that this might be confusing/unexpected behavior.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
pat
Posts: 53
Joined: Tue Jul 20, 2010 7:55 pm
Contact:

Re: Collision only occuring on a single side of a shape

Post by pat »

slembcke wrote:@Pat You can use NULL instead of &space->staticBody if you want. It's an undocumented convenience feature at the moment. It hasn't been well tested that all functions that expect a body handle being passed NULL gracefully. Also, when you add a joint or shape with a NULL body pointer, the space actually overwrites the pointer to the static body over it. A couple people have expressed that this might be confusing/unexpected behavior.
Yea, it's a bit confusing just that the option to do either is there. Maybe just having a macro like below would help that confusion a bit. It is a bit more readable this way, but about as functional as the polite please definition in the blog hah.

Code: Select all

#define CP_STATIC_BODY 0
or could do like for each shape new/init function

Code: Select all

#define cpSegmentShapeNewStatic(a, b, radius) cpSegmentShapeNew(0, a, b, radius)
Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests