# Rotation problem

## Recommended Posts

Hi, I'm using the following code to get the angle a character needs to rotate to face another character. Why does it not work? Rotation = (int)(Math.Atan(SM.GetDistance(this, SM.ClosestEnemy()) / (X - SM.ClosestEnemy().X)) * 180 / Math.PI); Thanks

##### Share on other sites
It looks like you are taking total distance over x distance. Since you are using atan you probably want y distance over x distance. This is going to give you a reference angle rather than the full angle so you might consider using atan2 if the language you are using has it(and it probably does). That would give you the angle from the positive x axis through your character to the closest enemy. To get the angle needed to turn you'll need to subtract your current facing angle from this value. You might try something like:

Rotation = (int)(Math.Atan2(Y - SM.ClosestEnemy().Y,X - SM.ClosestEnemy().X) * 180 / Math.PI - currentfacingangleindegrees);

##### Share on other sites
The more usual way were to use the dot-product and the cross-product of the normalized view and difference vectors, or perhaps their projections. Then the dot-product gives you the cosine of the angle, and the cross-product tells you whether you should rotate left or right (i.e. on which side the lesser angle, namely those of the dot-product, is being located).

##### Share on other sites

Quote:
 The more usual way were to use the dot-product and the cross-product of the normalized view and difference vectors, or perhaps their projections. Then the dot-product gives you the cosine of the angle, and the cross-product tells you whether you should rotate left or right (i.e. on which side the lesser angle, namely those of the dot-product, is being located).

May I have some examples for using dot-production and cross-production for this. I know how to work with atan(), but i think that using basic vector operations for this is much more faster?

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627737
• Total Posts
2978873

• 10
• 10
• 21
• 14
• 12