@Acki: Given any two arbitrary vectors, they lie in a plane. The code that Auradrummer wrote calculates this angle.
@Auradrummer: Actually, although not completely self-evident, this functionality is possible with the current implementation of
vector3d. It's great that you're studying these new topics though and contributing what you are learning! I just want to give you a little bit of constructive criticism because I know of you, from the past, and I know you are truly trying.
Firstly, Acki is correct, you should use the template parameter
T; secondly, in its current state, your code is hard to read and the style is incoherent. That's an easy fix. For example, here is code that does the same thing, but is a bit more readable:
Code: Select all
//! Get planar angle between vectors
T getAngleBetween (const vector3d<T>& vec2)
{
const T dotProduct = X * vec2.X + Y * vec2.Y + Z * vec2.Z;
const T length1 = sqrt (X * X + Y * Y + Z * Z);
const T length2 = sqrt (vec2.X * vec2.X + vec2.Y * vec2.Y + vec2.Z * vec2.Z);
const T cosAngle = dotProduct / (length1 * length2);
return acos (cosAngle);
}
Now with that exercise out of the way, it's time to talk about how Irrlicht already provides the capability for you to do this. You could either normalize the vectors before hand and then calculate the dot product [1], or calculate the dot product and then divide by the sum of the lengths like your code does [2].
[1]: A note must be made that
vector3d<T>::normalze does modify the vector.
Code: Select all
vec1.normalize();
vec2.normalize();
const f32 angle = vec1.dotProduct( vec2 );
[2]: Equivalent to what you wrote above
Code: Select all
const f32 dp = vec1.dotProduct( vec2 );
const f32 angle = dp / (vec1.getLength() + vec2.getLength());
So the reason you probably don't see that code inside of
vector3d is because the implementation in code is trivial. You could of course keep the modification for yourself in your own copy of Irrlicht, but I don't think the developers plan on putting the code into the class.
At any rate, great topic Auradrummer. Keep up the hard work.