Sign in to follow this  

calculating the angular rotation from a key press

This topic is 4375 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi guys, cant seem to get my head around this....say your looking top down at your character, and he's facing up the screen. Now, say that you press the down arrow, in that case I would want my character to rotate around 180 degrees to now face down. How can I work out the angle I need him to rotate to face any direction?? can't seem to work it out, and it should be fairly simple in my mind......ahhh...any help appreciated.

Share this post


Link to post
Share on other sites
Quote:

How can I work out the angle I need him to rotate to face any direction?


You can take the dot product of the character's "look at" vector and the "desired direction" vector. If both vectors are unit, then this is the cosine of the angle they form *on their plane*. So, use the aproppriate inverse trigonometric function (acos) to fine the angle. It will be returned in the range (-pi/2,pi/2)

Share this post


Link to post
Share on other sites
Let's treat its facing as a vector iin R2, with the up vector nott included.

The initial facing vector is F = <xi,zi>
The facing that you want to turn to is G = <xf,zf>.

The scalar product is ||F|| * ||G|| * cos a,
where ||F|| is the length of F (should be 1)
||G|| is the length of G (should be 1)
and a is the angle between them.

So, cos a = xixf + zizf
or, a = acos(xixf + zizf)

Share this post


Link to post
Share on other sites

This topic is 4375 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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