Angle between Vectors
I have a function to calculate the geometrical angle between two vectors :
double AngleBetweenVectors(CVector3 Vector1, CVector3 Vector2)
{
// Get the dot product of the vectors
float dotProduct = Dot(Vector1, Vector2);
// Get the product of both of the vectors magnitudes
float vectorsMagnitude = Magnitude(Vector1) * Magnitude(Vector2) ;
// Get the angle in radians between the 2 vectors
double angle = acos( dotProduct / vectorsMagnitude );
// Return the angle in radians
return( angle );
}
but I want to calculate the angle *directed*, I don''t know if it''s the good word in English ( "angle orienté" en Français =) but I want to have an angle according to the direction of the vectors therefore to have negative angles.
How can I do that ??
Thanks.
In 3-space there is no way to specify such a direction only given two vectors. You need a third "reference" vector with wich to compute the directions against. It works like this, consider being at the reference vector and looking at the two vectors in question. If the rotation is a couter-clockwise one from v1 to v2, then the angle is positive. If it is clockwise from v1 to v2, then the angle is negative.
This can be computed like so, (assuming that ref is not in the plane of v1, v2)
dot = (v1 x v2) dotP ref
if (dot < 0)
clockwise
if (dot > 0)
counter clockwise
if (dot == 0)
ref in plane of v1 v2, no singed angle exists
(NOTE: I haven''t doublechecked those signs, they may be reversed. Note also that they will be reversed between RH and LH coordinate systems.)
How does this work? We are looking at the normal created by the cross product of the two vectors, and seeing if it is pointing toward or away from the reference. Since (v1 x v2) = -(v2 x v1), a rotation in the oppisite direction from a given rotation will always give a different sign for the dot product with ref.
Josh
This can be computed like so, (assuming that ref is not in the plane of v1, v2)
dot = (v1 x v2) dotP ref
if (dot < 0)
clockwise
if (dot > 0)
counter clockwise
if (dot == 0)
ref in plane of v1 v2, no singed angle exists
(NOTE: I haven''t doublechecked those signs, they may be reversed. Note also that they will be reversed between RH and LH coordinate systems.)
How does this work? We are looking at the normal created by the cross product of the two vectors, and seeing if it is pointing toward or away from the reference. Since (v1 x v2) = -(v2 x v1), a rotation in the oppisite direction from a given rotation will always give a different sign for the dot product with ref.
Josh
I have a question for a similar topic:
Can I do the same to get the angle between 2 vectors on a plane? I need the yaw and pitch between 2 vectors.
Can I do the same to get the angle between 2 vectors on a plane? I need the yaw and pitch between 2 vectors.
quote:
Original post by Cybertron
I have a question for a similar topic:
Can I do the same to get the angle between 2 vectors on a plane? I need the yaw and pitch between 2 vectors.
Cybertron,
Please post a separate thread. I see your question leading to a series of posts. Its quite confusing to start a new thread in the middle of an existing one.
You''ll want to ask for clarification on what yaw and pitch actually are, for example. (Hint, you can''t get both a yaw and pitch given only two vectors. Plus, what you can get depends on how you choose to define yaw and pitch.)
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement