Rotate towards position?

What’s the formula to set a body’s rotation value so that this body will face a certain point on the screen? (In my game, I have a hunter sprite which will hunt a player, so it will look at that player.)

I’m currently dabbling around with the following, I’ve no idea if that’s in any way even close :slight_smile:

local angle = math.atan2(myBody.x - targetX, myBody.y - targetY) local angleDegrees = angle \* (180 / math.pi) myBody.rotation = math.floor(angleDegrees) [import]uid: 10284 topic_id: 4856 reply_id: 304856[/import]

This is the function I use… just scroll down to the 2nd one. I think the second object should be the player, but maybe the order doesn’t matter…

http://developer.anscamobile.com/forum/2010/11/17/math-helper-functions-distancebetween-and-anglebetween

It will return the angle between, in which case you just set your hunter to that rotation with "object.rotation = "

Let me know if this doesn’t work,
Brent
[import]uid: 9747 topic_id: 4856 reply_id: 15591[/import]

Works great!! Thanks! (I subtract 90 degrees from the result but that might be due to how my graphics are orientated in the first place.) [import]uid: 10284 topic_id: 4856 reply_id: 15697[/import]