PDA

View Full Version : Not opengl related but can somebody help???



Fastian
05-21-2001, 01:56 AM
How can I find the angle between 2 vectors in 3D space. I mean if i am trying to find the angle between up vector (0,1,0) and a vector (1,1,0) using the formula
@ = cos^-1 (a dot b)/ |a|.|b|
then suppose the angle comes to be around 45 but the angle between (0,1,0) and (-1,1,0) also comes to be 45 while it should be -45. So does anybody know of a way through which I can get actual angle between up vector and another vector or is there a way to identify that the 45 angle in the second case is actually -45??? waiting for your reply.... Please help.....!

Fastian

kistompika
05-21-2001, 09:08 AM
The problem is that cos(x)=cos(-x).

Your problem can be easily solved, but my method is not too efficient(it's just what popped in my mind right now, if I find a more simple one, I'll let you know).

Let the two vectors be u and v. We want to find the counterclockwise angle between u, and v in that order. Simply to speak we want to see if v is "below" u or "above" it.
We calculate w=u x v. Then w and u specify a plane, its normal is n=w x u. If v points "down" from the plane then s=(v dot n) is negative. Otherwise its positive. You can now simply calculate the angle :

angle=sign(s)*cos^-1((u dot v)/(|u|*|v|))

It's possible that I mixed up u and v, or w and u in their cross product. Please try each possibility.

ET3D
05-21-2001, 12:32 PM
There is no correct "angle between vectors". The angle is always up to a sign. That's because you can look at the vectors from two different directions, and get opposite (or complementary) angles.

What kistompika suggested is a good start, but I'll modify it a bit. Calculate w = u x v, then multiply the resulting vector by the normal to the plane you're interested in comparing angles on (for example, your view direction if your screen is the plane). Then check the sign. I always mix my signs, so I won't tell you which is which - check it yourself http://www.opengl.org/discussion_boards/ubb/wink.gif

Maybe there's a simpler way, but that's the way I do it (well, actually, I first project both vectors to the plane, then measure the angle, but both versions can be useful for different things).

[This message has been edited by ET3D (edited 05-21-2001).]

Randolph
05-21-2001, 12:48 PM
If you always know, what angle you need, you should use methods defined above. But I am not sure, that for any vectors in 3d space you know for sure, which angle you need from "this" side or from "opposite" one. If you just need angle and dont want to make any operations with it, take it absolute value...