Code: Select all
cpBody* finishLineBody = cpBodyNew(INFINITY, INFINITY);
finishLineBody->p = position; // created the position and passed it in to this method
cpShape* fLineShape = cpPolyShapeNew(finishLineBody, 8, verts, cpvzero); // verts already created
fLineShape->e = 0.3f;
fLineShape->u = 0.7f;
CCSprite *finishLineSprite = [CCSprite spriteWithFile:@"finish_line.png"];
finishLineSprite.position = finishLineBody->p;
fLineShape->data = (__bridge void*)finishLineSprite;
// The rotation code
fLineShape->body->a = CC_DEGREES_TO_RADIANS(90.0);
finishLineSprite.rotation = CC_RADIANS_TO_DEGREES(fLineShape->body->a) * -1;
cpSpaceAddStaticShape(self.space, fLineShape);
[self addChild:finishLineSprite z:0];
The sprite is rotated 90º as desired, but the shape doesn't get rotated so my collisions don't match the sprite.
My real question is this: Where, when and how does a shape attached to a body get rotated? I can see from the debugger output that the tVerts array usually gets updated after a rotation takes place (but I can't find where).
I have another static shape/body/sprite object that I manually rotate when a touch happens and it DOES get rotated correctly. I'm using the same way of rotating that object and it works there. That object's tVerts have been updated properly when I break in the debug draw code.
When looking at the tVerts for the finishLineShape/body above in the debug draw code, I notice that the tVerts haven't been updated to reflect the fLineShape->body->a = CC_DEGREES_TO_RADIANS(90.0); call made before adding the shape to the space.
My update method updates the sprite.position, body->a and sprite.rotation as normal:
-(void) update:(ccTime)delta
{
float timeStep = 0.03f;
cpSpaceStep(self.space, timeStep);
// call forEachShape C method to update sprite positions
cpSpaceEachShape(self.space, &forEachShape, nil);
}
static void forEachShape(cpShape* shape, void* data)
{
CCSprite* sprite = (__bridge CCSprite*)shape->data;
if (sprite != nil)
{
cpBody* body = shape->body;
sprite.position = body->p;
sprite.rotation = CC_RADIANS_TO_DEGREES(body->a) * -1;
}
}
I'd like to understand the detailed mechanism of rotation of a static shape created with a body and associated with a sprite (also, an understanding of dynamic (non-static) shape rotation).
I assume that a shape created with a body is locked to that body position and rotation angle and so gets rotated when the body gets rotated - is this correct?
Thanks