Hi everybody !
I am facing a problem using chipmunk and iPhone accelerometer. Indeed, I defined a ball shape and body :
ballBody = cpBodyNew(100.0, INFINITY);
ballBody->p = cpv(160, 240);
cpSpaceAddBody(space, ballBody);
cpShape *ballShape = cpCircleShapeNew(ballBody, 20.0, cpvzero);
ballShape->e = 0.5;
ballShape->u = 0.8;
ballShape->collision_type = 1;
I want my ball to be able to move all around the iPhone screen and not more. For this, I defined walls all around the screen (apart from the top of the screen) :
cpBody *floorBody = cpBodyNew(INFINITY, INFINITY);
cpBody *wallBodyR = cpBodyNew(INFINITY, INFINITY);
cpBody *wallBodyL = cpBodyNew(INFINITY, INFINITY);
floorBody->p = cpv(160, 0);
wallBodyR->p = cpv(320, 240);
wallBodyL->p = cpv(0, 240);
cpShape *floorShape = cpSegmentShapeNew(floorBody, cpv(-160.0, 0.0), cpv(320.0, 0.0), 0);
cpShape *wallShapeR = cpSegmentShapeNew(wallBodyR, cpv(0.0, -240.0), cpv(0.0, 240.0), 0);
cpShape *wallShapeL = cpSegmentShapeNew(wallBodyL, cpv(0.0, -240.0), cpv(0.0, 240.0), 0);
floorShape->e = 0.5; floorShape->u = 0.1; floorShape->collision_type = 0;
wallShapeR->e = 0.5; wallShapeR->u = 0.1; wallShapeR->collision_type = 0;
wallShapeL->e = 0.5; wallShapeL->u = 0.1; wallShapeL->collision_type = 0;
cpSpaceAddStaticShape(space, floorShape);
cpSpaceAddStaticShape(space, wallShapeR);
cpSpaceAddStaticShape(space, wallShapeL);
I use those methods to make my ball move :
- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
accellX = acceleration.x * 10;
accellY = acceleration.y * 10;
}
-(void) step: (ccTime) dt{
cpSpaceStep(space, dt);
float newX = ballBody->p.x + accellX;
float newY = ballBody->p.y + accellY;
// move the sprite body to the new x position
ballBody->p = cpv(newX, newY);
// call our function for each shape
cpSpaceHashEach(space->activeShapes, &updateShape, nil);
}
void updateShape(void *ptr, void* unused)
{
cpShape *shape = (cpShape *)ptr; // the shape
CCSprite *sprite = shape->data; // the sprite
// set the sprite position to the shapes body position
[sprite setPosition: shape->body->p];
}
My problem is that my ball go through the walls when moving with the accelerometer. It clearly slow down when hurting a wall but it doesn't stop. If I give my space a gravity, the ball correctly stop and bounce when it hurts the floor (bottom wall), but don't when it hurts the left or right wall.
Any suggestions ?
PS: forgive my english, it's not my mother tongue.