Sign in to follow this  
newbie_programmer

Yaw angle

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


Link to post
Share on other sites
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


Link to post
Share on other sites
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

Share this post


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


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


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


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