Page 1 of 1

Objects sometimes falling through floor

Posted: Mon Nov 08, 2010 7:44 am
by Workshed
Hi guys,

I've got a bit of an issue, i got Chipmunk working nicely but if I set the mass of my bodies to a reasonable amount (so that items fall with gravity fast enough to look realistic) some of them will fall through the floor I have defined...

Any pointers would be hugely appreciated! Here's some code:

Code: Select all

	cpInitChipmunk();  
	space = cpSpaceNew();  
	space->gravity = cpv(0, -200);  

	cpVect object_bounds[] = { cpv(0.0, 0.0), cpv(44.0, 0.0), cpv(44.0, -20.0), cpv(-44.0, -20.0) };

	for (PhysicsSprite* physics_obj in physicsSprites) // I'm looping though 10 of these
	{	
		cpBody *ballBody = cpBodyNew(20.0, 1000); 
		ballBody->p = cpv(physics_obj.x, physics_obj.y); // Positions are from x=628 to x=258 and all are at y=300
		cpSpaceAddBody(space, ballBody);
		cpShape* objectShape = cpPolyShapeNew(ballBody, 4, object_bounds, cpv(0.0, 0.0));
		objectShape->e = 0.5; // Elasticity 
		objectShape->u = 0.8; // Friction  
		objectShape->data = physics_obj;
		objectShape->collision_type = 1;
		cpSpaceAddShape(space, objectShape);
	}
	
	// Create floor's body and set it's position  
	cpBody *altFloorBody = cpBodyNew(INFINITY, INFINITY);
	
	altFloorBody->p = cpv(0.0, 10.0);
	
	cpShape* altFloorShape = cpSegmentShapeNew(altFloorBody, cpv(0.0, 0.0), cpv(1124.0, 0.0), 0.0);
	altFloorShape->e = 0.0; altFloorShape->u = 0.15; altFloorShape->collision_type = 0;  
	altFloorShape->data = floor; 
	cpSpaceAddStaticShape(space, altFloorShape);  

Re: Objects sometimes falling through floor

Posted: Mon Nov 08, 2010 9:48 am
by gerrit
I just tackled this by bumping up the iterations and using a polygon shape with some depth to it as my ground (instead of a segment shape like all of the examples use).

Re: Objects sometimes falling through floor

Posted: Mon Nov 08, 2010 9:50 am
by slembcke
Chipmunk does not support swept collisions. This means that if your objects are small, moving fast, and you are using a large timestep, they may be able to pass through each other without generating collisions.

The solution is to do the opposite, make your objects larger or your ground thicker, cap their velocity so they don't move as fast, and/or use a smaller timestep.

Re: Objects sometimes falling through floor

Posted: Tue Nov 09, 2010 4:20 am
by Workshed
Thanks for that guys, I'll have a play with it :)
It's reassuring to know that i've not done something stupid in the setup :D