#### Archived

This topic is now archived and is closed to further replies.

# Rotate Vector from Direction Vector

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

## Recommended Posts

I'm a week or so old in the 3D math department, so feel free to laugh at my attempts and understanding of this. How do I use a normalized direction vector to create a rotation vector that will make an object face that direction? I have two point vectors respresenting the positions of two objects. One of the objects happen to be an eye (as in a human eye, not Camera Eye). I want to make the eye rotate to "look" at the other position. I know how to calculate the normalized direction vector between the eye and the point. And if I use the direction vector as the rotation values, it almost works. The only problem is that the eye will stop at around 90 degree angles; they won't rotate in a complete circle. Also, I had to use -Y, X, Z of the direction vector for the rotation. That's Rotate(x,y,z) = Dir(-y,x,z). But I'm sure that's just because of the starting points and initial rotations. Is this close to what I need? Creating a rotate-vector from a direction vector will come in handy quite a lot, so any help will be put to great use. EDIT: Umm, I was wrong. The eye stops at around 45 degrees, not 90. [edited by - Jiia on May 29, 2004 9:21:12 PM]

##### Share on other sites
This guy also feels my pain

##### Share on other sites
You''ll need two vectors - one that represents the direction you want to face, and what that represents the direction you are facing currently. Normalized vectors both would be ideal. You take the cross product between these two vectors to determine the rotation vector, and the dot product to determine the cosine of the angle between then (assuming normalized vectors). If your eye turns the wrong direction, but the correct angle amount, then switch the cross product operation.

Now, how you''d go about rotating around this axis is a different story

##### Share on other sites
It sounds great, but my initial facing direction is 0,0,0. If I cross the direction I want to face with 0,0,0, that results in 0,0,0, right? The eye mesh is actually looking towards exact-negative-Z. But that is with zero rotation.

I''m probably totally screwing it up.

This does exactly what I need, but this is total trig, right?

Rotation.y = atan2f(DirVector.x, DirVector.z);
Rotation.x = DirVector.y;
Rotation.z = 0.0f;

x and z determine the y rotation angle. X just uses the Y direction. And of course the eye can''t rotate on the Z axis. Damn, that would have to hurt

Anyone see where I''m totally blotching this?

##### Share on other sites
Well I have been slaving over this one

http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm

is a good link. From what I understand. Take the 2 vectors describing the two points and get the dot product of them. then multiply the arccos to get the new axis.

Theres more to iy which I''m still reading but this describes a lot of it.

do a google on dot angles vectors matrix and you''ll find page after page on it.

##### Share on other sites
These relate to the yaw pitch and roll by the way. A cross product is a slower way to reach the same result.

Use yaw pitch and roll. I saw in those pages explaining it, but Andre LaMothes book goes into detail on moving objects in space so I just studied his code.

##### Share on other sites
Eck! I can''t even seem to find this arccos function. Perhaps I''m not including the right header. It''s not in math.h? And Dot Products return a float. How is a single angle going to help? I know: I just licked the icing. Have to go back to college for a few years before I can rotate to face a direction

It''s so easy to find the direction vector from a rotation vector. Err, at least it''s easy to find it with some D3DX matrix math. Just matrix 3,1 - 3,2 - 3,3 after applying a rotation to it. Seems like some reverse engineering could have me creating rotation vectors from direction vectors in no time.

In the meantime, I''m going outside to scream. Then maybe get some aggression out on the Vice City police force. Then perhaps I can be a normal guy and try again.

This stuff is so easy.

##### Share on other sites
yeah the up, look, and right vectors along with the position are simple.

I think the arccos function is the acos function in c++. Nobody has told me either.

##### Share on other sites
Thanks for that. acos is definitely the arc cosine.

quote:
Original post by nick5454
From what I understand. Take the 2 vectors describing the two points and get the dot product of them. then multiply the arccos to get the new axis.

I''m still not quite sure why a new axis is needed..? Shouldn''t the object rotate around it''s own axes?

In any case, I''m sticking with my atan2 for now. If I spend 3 days trying to rotate an eye, my game will never get finished.

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633696
• Total Posts
3013394
×