Skip to main content
GameDev.net gamedev.net
🔒 Locked

Yaw angle

Started by newbie_programmer Aug 31, 2005 at 4:10 PM 5 replies 1.4k views
Original Post
newbie_programmer
newbie_programmer
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
CombatWombat
CombatWombat
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.
jjd
jjd
Quote:
Original post by CombatWombat
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.



Do you mean "arc cosine"?


-Josh
Zakwayda
Zakwayda
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.
newbie_programmer
newbie_programmer
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.
Zakwayda
Zakwayda
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));
newbie_programmer
newbie_programmer
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**

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.