tags:

views:

1112

answers:

1

Hi,

I am facing one problem. I have done some coding to rotate cpSegmentShapeNew but its not working . Have a look on the following code,

    //**creating shape
testBody = cpBodyNew(INFINITY, INFINITY);
cpShape* testShape = cpSegmentShapeNew(testBody, cpv(230, 82), cpv(193, 46), 0.0f);
testShape->e = 0.0;
testShape->u = 0.0;
testShape->data = flipper;
testShape->collision_type = 2;
cpSpaceAddStaticShape(space, testShape);

//Body moving when user touch
-(BOOL) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//event that starts when a finger touchs the screen
UITouch *touch = [touches anyObject];
CGPoint tmpLoc = [touch locationInView: [touch view]];
CGPoint location = [[Director sharedDirector] convertCoordinate:tmpLoc];

ball.position = location;
ballBody->p = location;
[flipper runAction:[RotateTo actionWithDuration:0.1f angle:60]];

cpBodySetAngle(testBody, 60);

cpvrotate(testBody->rot, cpv(100000,0));

return kEventHandled;
}

Please anyone tell me that where i am wrong.

Thanks.

A: 

Greetings,

The problem is that you are rotating both objects (sprite + body) through code.

What you need is rotate one, and let the other object know it's happened so it can do it too.

For example, if you move the body, then the method that updates the sprites should look like that:

void updateShapes(void* ptr, void* unused)
{
 cpShape* shape = (cpShape*)ptr;
 Sprite* sprite = shape->data;
 if(sprite)
 {
  cpBody* body = shape->body;
  [sprite setPosition:cpv(body->p.x, body->p.y)];
  [sprite setRotation: (float) CC_RADIANS_TO_DEGREES( -body->a )];
 }
}

The last line of code updates the rotation. That's the line you are missing.

I hope that helps you or someone else in the future.

Good luck cocos2d mate !

Yohann T.

Yohann T.