I'm experiencing a problem: i've created a body an i want to rotate it when user rotates the device.
Body creation code:
Code: Select all
space = cpSpaceNew();
cpSpaceResizeStaticHash(space, 400.0f, 40);
cpSpaceResizeActiveHash(space, 100, 600);
space->gravity = ccp(0, -100);
space->elasticIterations = space->iterations;
CGFloat x = screenSize.width / 2;
CGFloat y = screenSize.height / 2;
#define ratio 32
staticBody = cpBodyNew(INFINITY, INFINITY);
cpShape *shape;
shape = cpSegmentShapeNew(staticBody, ccp(x + -3.4*ratio, y + 3.4*ratio), ccp(x + 3.4*ratio, y + 3.4*ratio), 2.f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);
shape = cpSegmentShapeNew(staticBody, ccp(x + 3.4*ratio, y + 3.4*ratio), ccp(x + 3.4*ratio, y + -3.4*ratio), 2.f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);
shape = cpSegmentShapeNew(staticBody, ccp(x + 3.4*ratio, y + -3.4*ratio), ccp(x + 1.2*ratio, y + -3.4*ratio), 2.f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);
shape = cpSegmentShapeNew(staticBody, ccp(x + 1.2*ratio, y + -3.4*ratio), ccp(x + 1.2*ratio, y + 1.07*ratio), 2.f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);
shape = cpSegmentShapeNew(staticBody, ccp(x + 1.2*ratio, y + 1.07*ratio), ccp(x + -3.3*ratio, y + 1.07*ratio), 2.f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);
shape = cpSegmentShapeNew(staticBody, ccp(x + -3.3*ratio, y + 1.07*ratio), ccp(x + -3.4*ratio, y + 1.15*ratio), 2.f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);
shape = cpSegmentShapeNew(staticBody, ccp(x + -3.4*ratio, y + 1.15*ratio), ccp(x + -3.4*ratio, y + 3.4*ratio), 2.f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);
Code: Select all
-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
rollingX = (acceleration.x * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
rollingY = (acceleration.y * kFilteringFactor) + (rollingY * (1.0 - kFilteringFactor));
rollingZ = (acceleration.z * kFilteringFactor) + (rollingZ * (1.0 - kFilteringFactor));
float accelX = acceleration.x - rollingX;
float accelY = acceleration.y - rollingY;
float accelZ = acceleration.z - rollingZ;
CGFloat angleAcceleration = atan2(accelY, -accelX);
cpBodySetAngVel(staticBody, angleAcceleration);
}
Looking forward for your advice!