This is a discussion on Angle between vectors within the Game Programming forums, part of the General Programming Boards category; Originally Posted by Geometrian If you normalize the two vectors, (length = 1), then dot them, you'll get a right ...
yeah, I am using cartesian coordinate..except this one is reversed..
the baffling thing is..except I swap the values around...
I think I can continue working from now on..thanks very much Elysia..missed you on this forum
Thanks Geometrian
You ended that sentence with a preposition...Bastard!
What is all this talk of a right triangle? If you have two coordinates you can form a right triangle quite easily but you do not need to for this problem.
This is C++. Note that C# does not have const and Vector3 should be a struct or value type instead of a reference type.Code:// Returns a scalar that is the arccosine of the angle between the vectors // v1 and v2 must be unit vectors if the angle is needed - IE: normalized vectors // For simple sign comparison v1 and v2 do not need to be normalized float Dot(const Vector3 &v1,const Vector3 &v2) { return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z); }
If you only need the angle between two vectors you simply normalize the vectors, dot them, and take the arccosine of the result to get the angle. If you only need to test the sign of the result (actually more useful than the angle) then the vectors do not need to be normalized as this does not affect the result.
The dot product is probably the most important operation in computer graphics and is used all over the place because it's geometric meaning is extremely important and applicable to nearly every operation and test you need to perform in graphics and games.
It depends. Generally speaking, raw datas' parameters should be normalized (i.e., your object data should have normalized geometry). When rendering, some things can mess this up--e.g. scaling, linear or nonlinear, (although the normal matrix (transpose of the inverse model*view matrix) counters this).
However, in some cases, you will need to explicitly normalize your vectors. For example, in a fragment program, normals are linearly interpolated from the vertex program, changing their lengths. Often, the first thing one does in a fragment program is normalize such parameters (also commonly applies to tangents and binormals).
Ian