Jump to content
  • Advertisement
Sign in to follow this  
Little Coding Fox

Making a character always turned at me

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

Hey everyone! I'm making a simple 3D demo but i need one character (not the player) to keep turned at me all of the time (sorry for my english!). I am doing this project with OpenGL, and i use a vector class (GhostVector3f). What i want to do is like this: //Draw Scene glRotatef(angle,0,1,0);//angle = angle so the character is turned at me //Draw the character //end draw scene Can anyone please help me finding the angle based on the xyz of the vector class? thank you!

Share this post


Link to post
Share on other sites
Advertisement
You can easily find the angle between two vectors using the inner product.

Suppose we have vectors A and B. The inner product is defined like so:

A.x * B.x + A.y * B.y + A.z * B.z

We also need to find the length of the two vectors (if they are prenormalised, this turns out to be 1). The length of a vector is:

sqrt(A . A) where . signifies the inner product as mentioned above, this is actually Pythagoras. This is usually signified using |A|.

We can now find the angle between the two vectors by using:

acos x = (A.B) / (|A||B|)

I read somewhere here that there's a more reliable technique than using acos, but I have forgotten it, so perhaps you should search for it.

Basically, find the angle between the characters forward vector and the vector between you and the character, then rotate the character by that amount.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!