Sign in to follow this  
dries123

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this