# Making a character always turned at me

This topic is 4977 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633325
• Total Posts
3011372
• ### Who's Online (See full list)

There are no registered users currently online

×