Rotating body towards a point

I'm fairly new to box2d, so any help would be appreciated. I'm using the iPhone port (along with the cocos2d library). What I'm looking to achieve is for my player characters box2d body to rotate towards the location of the last touched point.

Here's the code I'm currently working with (where pBody is the player Body and location is a CGPoint containing the x and y location of the last known player touch):

Code:
   CGPoint location = [touch locationInView: [touch view]];
   location = [[CCDirector sharedDirector] convertToGL: location];
         
   //CCSprite *actor = (CCSprite*)pBody->GetUserData();
         
   b2Vec2 pos = pBody->GetPosition();
   float xpos = pos.x * PTM_RATIO;
   float ypos = pos.y * PTM_RATIO;

   pBody->SetTransform(pos, atan2f(location.y - ypos, location.x - xpos));


While the code compiles, I'm getting errant rotation. Any advice would be greatly appreciated!
You shouldn't use SetTransform if later you want to manage collision. SetTransform teleports the body.
Instead you should set the body velocity or use forces.
Do you call SetTransform another time of the same frame ?
Otherwise your body will never move since you set his position to his current position.

You should create your own tool to understand why it doesn't work.
For example you could draw points, lines, arrows to check if your maths are ok.

Also you maths seems wrong. What you pass to atan2f is not normalized.
noon wrote:
What you pass to atan2f is not normalized.
The input to atan2() doesn't need to be normalized. (Assuming a typical implementation of atan2(), at least.)
Jesse wrote:
noon wrote:
What you pass to atan2f is not normalized.
The input to atan2() doesn't need to be normalized. (Assuming a typical implementation of atan2(), at least.)


Yep, you are right.

Original thread: https://box2d.org/forum/viewtopic.php?t=5689