views:

38

answers:

0

I'm attempting to rotate a box2d body that's tied to a cocos2d sprite via box2d's GetUserData() in my iPhone application. Specifically, I'm attempting to grab the latest touch location and rotate my box2d body in that direction.

I'm fairly inexperienced when it comes to box2d, so any advice would be appreciated. Below is a quick stab at how I imagine I'd manipulate the players box2d body. I'd like clarification on:

1) If this is the correct way of doing things. 2) How I'd calculate the angle between the player and the last touch location in order to rotate my player in that direction.

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    b2Body *pBody = self.playerBody;

    if(pBody != NULL) {
        for(UITouch *touch in touches) {
            CGPoint location = [touch locationInView: [touch view]];
            location = [[CCDirector sharedDirector] convertToGL: location];

            CCSprite *myActor = (CCSprite*)pBody->GetUserData();
            pBody->SetTransform(pBody->GetPosition(), angleToRotateByInRadians);
        }
    }
}