Advertisement

Rotation problem

Started by May 15, 2008 11:03 AM
2 comments, last by rxa 16 years, 5 months ago
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
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);
Advertisement
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).

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?

This topic is closed to new replies.

Advertisement