Jump to content
• Advertisement

# calculating the angular rotation from a key press

This topic is 4742 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

##### Share on other sites
Advertisement
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

##### 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

##### Share on other sites

• Advertisement

### Announcements

• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5
• Advertisement

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633682
• Total Posts
3013306
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!