I wanted to create a little iPad game to test all the features of Chipmunk. But I am facing a problem which i cant solve.
I just made a method that lets me draw new bodies (boxes) that are affected by gravity. It works really fine, but only for small boxes. If I make a bigger box (more than about 200pixel) these boxes start to overlap with the ground and sometimes even fall through it.
The shapes work fine, the boxes always collide in the right moment, but the big boxes never fall asleep. They just vibrate into the ground.
(there is a static shape that starts 50px above the bottom)
It has nothing to do with the mass, as all the bodies have the same mass.
Here is the Method that creates the boxes:
Code: Select all
-(void) newObjectFrom:(CGPoint)start toPoint:(CGPoint)end onLayer:(CCLayer *)layer {
numberBodies ++;
CGPoint size;
size.x = start.x - end.x;
size.y = start.y - end.y;
CGPoint center;
center.x = start.x - size.x/2;
center.y = start.y - size.y/2;
cpFloat mass=1;
sprite[numberBodies] = [CCSprite spriteWithFile:@"wood.png" rect:CGRectMake(0, 0, abs(size.x), abs(size.y))];
sprite[numberBodies].position = center;
[layer addChild: sprite[numberBodies]];
body[numberBodies] = cpBodyNew(mass, 10);
cpBodySetPos( body[numberBodies], center );
cpSpaceAddBody(space, body[numberBodies]);
cpShape* shape = cpBoxShapeNew(body[numberBodies], abs(size.x), abs(size.y));
cpShapeSetElasticity( shape, 0.5 );
cpShapeSetFriction( shape, 0.5 );
cpSpaceAddShape(space, shape);
}
Thats how i create my chipmunk space.
Code: Select all
-(id) init {
if (self = [super init])
{
cpInitChipmunk();
space = cpSpaceNew();
cpSpaceSetGravity(space, cpv(0, -500) );
cpSpaceSetIterations(space, 20);
cpSpaceSetCollisionSlop(space, 0);
cpSpaceUseSpatialHash(space, 1000, 5);
cpShape *ground = cpSegmentShapeNew(space->staticBody , cpv(0,0), cpv(1024,0), 50);
cpShapeSetElasticity(ground, 0.1);
cpShapeSetFriction( ground, 0.4);
cpSpaceAddStaticShape(space, ground);
numberBodies = -1;
}
return self;
}
Code: Select all
-(void) update:(ccTime) delta
{
int steps = 3;
CGFloat dt = [[CCDirector sharedDirector] animationInterval]/(CGFloat)steps;
for(int i=0; i<steps; i++){
[space updatePhysics:dt];
}
[space updateSprites];
}
}
-(void) updatePhysics: (CGFloat) dt {
cpSpaceStep(space, dt);
}
-(void) updateSprites {
for (int i=0; i <= numberBodies; i++) {
if (sprite[i]) {
sprite[i].position = body[i]->p;
sprite[i].rotation = CC_RADIANS_TO_DEGREES(-1 * body[i]->a);
}
}
}