yes that works for returning the angle between the two vectors in radians.
Printable View
yes that works for returning the angle between the two vectors in radians.
Err... That still doesn't work. Your function still doesn't work. You need to divide by the magnitude of v regardless of what temp is. Also, unless the vector is one of the unit vectors (<0, 0, 1>, etc), it si unlikely that it will ever be exactly of magnitude 1, so checking for that seems to be a bit pointless.
*edit*
This is what I'd use:
Code:float angleBetween(const Vector3D &v) const {
float temp1 = length(), temp2 = v.length();
if(temp1 == 0.0 || temp2 == 0) // Alternately, if(temp1 * temp2 == 0.0)
return 0.0;
else
return acosf(dotProduct(&v) / (temp1 * temp2))
}
Are you kidding? Any normalized vector has a length of 1. They are VERY common.
Do you know what normalizing a vector does, or how it is used?
Why do you assume v is normalized?Quote:
Originally posted by Lurker
Are you kidding? Any normalized vector has a length of 1. They are VERY common.
Do you know what normalizing a vector does, or how it is used?
Why do you assume it never will be?Quote:
Originally posted by Zach L.
Why do you assume v is normalized?
I don't. But you never checked for that in the code. Also, normalized vectors with magnitude 1 are rare when represented on a machine with finite memory. Normalized vectors with magnitude 1.00000000026, etc. are not rare.
Normalized vectors are DEFINED as having a length of 1. They CAN'T have 1.000000000000000000000000000000000000000000000000 000000000000000000000000025501 as a length.
that's a good point zach. i would just normalize them first, checking for divide by zero, then just take the dotproduct.
Yes. Mathematically, they are defined that way. Unfortunately, you cannot represent numbers such as the square root of any non-perfect square with infinite precision, so on a computer, they are more likely to have a magnitude of 1.0000000026 than 1.
lurker, you will rarely get a length of 1 when normalizing :) it's jsut really freaking close to being 1, but unless you make a new vector (0,0,1.0f) you will get what zach said.
I see what you mean, I didn't think about that. :D Thanks.
just normalize it, realize it's pretty close to being 1, then take the dotproduct.
Yeah, unless you have an 'approximately-equal-to' operator, I don't think it is worth checking for the magnitude being equal to 1, but that might be too inefficient anyways... I'm sure Silvercord has a better idea of whether or not it is worth it.
So how's this?
Thanks :D .Code:float angleBetween(const Vector3D &v) const {
normalize();
float temp = length();
if(temp == 0.0f) {
return;
}
acosf(dotProduct(v))
}
looks better