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

## Recommended Posts

Can someone tell me how to compute the angle between two 3D vectors, v1 and v2 where the angle is taken to be counterclockwise about v3 (v3 is assumed to be perpendicular to v1 and v2). Thanks.

##### Share on other sites
Dot product works.

v1•v2 = |v1||v2| cos(angle)

|v| = magnitude

##### Share on other sites
Dot product obviously doesn't work since it is commutative (for vectors with real entries), so clockwise/anticlockwise information is lost.

You need to do a cross product for that,

aXb = |a||b|sin(theta) _n_

where _n_ is the vector perpendicular to a and b (can't remember if its left or right handed though). You should be able to solve the above equation for theta pretty easily.

##### Share on other sites
Thanks, i'll give these a whirl.

##### Share on other sites
I am confused by this formula...

aXb = |a||b|sin(theta) _n_

First, I thought you could not divide a vector by another vector. And if you can, do you divide one vector's x by the other's x, and so on?

Second, if "a cross product b" (aXb) gives you a perpendicular vector and you are dividing by another perpendiculalar vector _n_, won't the formula just be...

sin(theta) = 1 / (|a||b|)

##### Share on other sites
Divide aXb by |a||b|, call this vector r (say).

r = sin(theta) _n_

_n_ is the axis you want to rotate around (normalised) - that's an INPUT to this equation.
Solve for any of the coordinates of the resultant vector to get the answer.

##### Share on other sites
I am still confused by this...

Solve for any of the coordinates of the resultant vector to get the answer.

How can you do...

sin(theta) = someVector / _n_;

##### Share on other sites
angle between A and B, looking from C:

vec3 cross=CrossProduct(A,B);//cross is perpendicular to A and B
double dot=DotProduct(A,B);
double angle=atan2(length(cross),dot);
if(DotProduct(cross,C)<0) angle=-angle;

note that
2: it may have opposite sign to what you want(depends to hand of system). You may need to change "<" to ">"

##### Share on other sites
That's amazing. It works perfectly. Thank you so much.

• 18
• 29
• 11
• 21
• 16