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


angle between vectors

Recommended Posts

Hello, I am learning 3d programming, and I am a little confused about something. I am attempting to use the billboard technique to render some lights. I can find the vector that points from the light to the camera position, but I am unsure of how to find the proper rotation angles to rotate the triangle to face the camera. I found many articles that talk about using the dot product to find the angle between two vectors, but what I am unsure of is how to translate the single float into a set of x,y,z rotations. Any tips or examples on how to do this would rock thanks

Share this post

Link to post
Share on other sites
I would find the axis of rotation by taking the cross-product of the vectors, and then use the dot-product to find the degree of rotation. The only thing you have to be careful of is the sign on the axis. This way, you can perform the proper rotation using a single matrix instead of using a concatenation of 3 euler rotations.

Share this post

Link to post
Share on other sites
Ok, I'm dense I still am not having any luck with this. I'm going to go through the steps I am doing to try and come up with the answer, maybe somebody can show me where I am messing up. I'm still pretty iffy with matrices, so I'm still trying what I figured was the 'easy' way.

I have a camera located at the origin:

camera_pos = 0,0,0

I have a light:

light_pos = 10,10,0

The light is represented by a triangle with a normal facing forward

light_vec = 0,0,1

My understanding of how to get the direction vector is to subtract the light position from the target position:

direction = camera_pos - light_pos = -10,-10,0

Then I get the length of the direction vector:

direction_length = direction.length() = 14.142136

Then I normalize my direction vector:

direction_normal = -0.70710677,-0.70710677,0

Now I get the dot product of the direction normal and the triangle normal

dotproduct = = 0

Then I get the angle by with acos on the dot product

angle = acos( dotproduct ) = 1.5707964

Now I get the cross product of the direction vector and and triangle normal

crossproduct = direction_normal.cross( light_vec ) = -0.70710677,-0.70710677,0

Then I multiply the crossproduct by the angle

new_angle = crossproduct * angle = -1.1107208, -1.1107208, 0

Now I thought that this was supposed to be rotation angles, so I rotate my triangle normal

new_normal = light_vec.Rotate( new_angle ) = -0.89601892, 0.39784661, 0.19715005

Now supposedly this normal should be the same as my original direction vector normal, and I would be able to travel direction_length units along this vector and end up back at my camera position, but this is obviously not going to be the case as the vector is not really even close to my original direction vector.

If I make my light position use only one axis this algorithm works, for instance, if my light is at 0, 10, 0 or 10,0,0 or 0,0,10 I get the correct values, but anything that uses more than one dimension comes out wrong.

Any ideas where I am making my major malfunction would be very helpful


[edited by - roaddog23 on January 5, 2004 3:57:50 PM]

[edited by - roaddog23 on January 5, 2004 3:58:57 PM]

Share this post

Link to post
Share on other sites
heres a quick way to do spherical billboards but it undoes all the previous operations

note: this is the OpenGL way to do it(i use this)

float modelview[16];

// get the current modelview matrix
glGetFloatv(GL_MODELVIEW_MATRIX , modelview);

// remove rotations
for( i=0; i<3; i++ )
for( j=0; j<3; j++ )
modelview[i*4 + j] = 0.0F;
if ( i == j ) modelview[i*4 + j] = 1.0F;
// set the modelview with no rotations

Share this post

Link to post
Share on other sites
I got it working, finally. It was the whole axis/angle thing that I didn''t understand. I was trying to multiply by the product rather than using it as an axis of rotation.


Share this post

Link to post
Share on other sites