Jump to content
• ### What is your GameDev Story?

• Advertisement

# Yaw angle

This topic is 4887 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 everybody, i am a newbie game programmer. My question might seem silly to you all. Any help you provide will be greatly appreciated. i need to find the angle between a NPC charcter and the vector from him to the player so i can rotate him towards the player. this rotation is to be done in about y axis. I have the world position of both the the models and their rotations. Also, if anybody could suggest resources to learn basic game physics and artificial intelligence from (esp 3D) i'd be gratefull. thank you all in advance

#### Share this post

##### Share on other sites
Advertisement
You can use the cos of the dot product of the two vectors for this.

Make sure they are both have a length of 1 first, however, or you'll get funny results.

http://freespace.virgin.net/hugo.elias/routines/r_dot.htm
^ Explains the dot product if you are not familiar with it, and shows how to get the angle between the vectors from it.

#### Share this post

##### Share on other sites
Quote:
 Original post by CombatWombatYou can use the cos of the dot product of the two vectors for this.Make sure they are both have a length of 1 first, however, or you'll get funny results.

Do you mean "arc cosine"?

-Josh

#### Share this post

##### Share on other sites
Another method that is in some ways superior to the acos() method is to use atan2(). Presumably your character has a forward vector that reflects its current rotation. You can then find the signed angle to a target like this:

Vector3 diff = target-position;
dot = diff.x*forward.x+diff.z*forward.z;
perpdot = diff.z*forward.x-diff.x*forward.z;
yaw_delta = atan2(perpdot,dot);
// *Convert from radians to degrees if necessary*

You may have to swap and/or negate the arguments to atan2(), depending on which direction you consider to be forward when no rotation has been applied.

#### Share this post

##### Share on other sites
Thank you all for such fast response.
I had come across these methods when i was trying to find a solution. My actual problem is that i dont know how to get the forward vector. :(
sorry for not stating my exact problem.

#### Share this post

##### Share on other sites
Quote:
 My actual problem is that i dont know how to get the forward vector. :(
Since this is essentially a 2d problem, the forward vector is simply constructed from sin(yaw) and cos(yaw). For example, if you want +z to be forward when yaw = 0, you might construct the forward vector like this:

// *convert to radians if necessary*

forward.set(sin(yaw), 0.0f, cos(yaw));

#### Share this post

##### Share on other sites
Thank you all for helping me out.
especially CombatWombat,Josh and jyk. This should solve me problem.

**proud to be part of such a helpfull community**

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• ### Popular Now

• 9
• 13
• 9
• 9
• 15
• Advertisement
• ### Forum Statistics

• Total Topics
634078
• Total Posts
3015363
×

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