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

Calculating angle between 2 vectors in 3D

Started by furrymaster Sep 5, 2010 at 6:26 AM 5 replies 950+ views
Original Post
furrymaster
furrymaster
Hi, im looking for advice how to get value of angle (in x,y,z space) which can rotate one vector(3D) to another.
For example:

I have vector(or arrow it doesn
t meter) 1st and 2nd and i would like to get angle with which i can rotate vector 1st into vector 2nd.

I need this calculations for getting rotate cane model for deferred lighting geometry render.
If I helped you rate me
KulSeran
KulSeran
I suspect you don't actually want that answer though, since you mentioned deferred lightning. You probably just want the matrix that would transform your model into the correct space for the rendering. This is just going to be some combination of inverted and multiplied matricies for your model, camera, and light depending on what space you are coming from and what one you want to go to (world? camera? screen?).

But, the direct answer is that you can get the angle from:
acos( dot( vec1, vec2 ) )
And then rotate it about the axis:
normalize( cross( vec1, vec2 ) )

furrymaster
furrymaster
But which axis?
If I helped you rate me
Ezbez
Ezbez
Quote:
Original post by KulSeran
acos( dot( vec1, vec2 ) )


This only works if vec1 and vec2 are unit vectors. More generally, you have:

vec1 dot vec2 = |vec1| * |vec2| * cos(angle)

where |vec| denotes the length of the vector.

Edit: The axis to rotate around is given by the last equation in KulSeran's post.
furrymaster
furrymaster
Em, i dont understand. I m getting only 1 angle from acos( dot( vec1, vec2 ) ) and
how can i use it for example in glRotatef()?
If I helped you rate me
SiCrane
SiCrane
The result of a cross product of two vectors is a vector perpendicular to both vectors. When normalized, that can be used as the axis of rotation for a rotation from one vector orientation to the other.
furrymaster
furrymaster
Thx, now i understand:) So i m going back to my code
If I helped you rate me

Topic Locked

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

Sign in to reply to this topic.