Unity: parallel vectors and cross product, how to compare vectors
- by Heisenbug
I read this post explaining a method to understand if the angle between 2 given vectors and the normal to the plane described by them, is clockwise or anticlockwise:
public static AngleDir GetAngleDirection(Vector3 beginDir, Vector3 endDir, Vector3 upDir)
{
Vector3 cross = Vector3.Cross(beginDir, endDir);
float dot = Vector3.Dot(cross, upDir);
if (dot > 0.0f)
return AngleDir.CLOCK;
else if (dot < 0.0f)
return AngleDir.ANTICLOCK;
return AngleDir.PARALLEL;
}
After having used it a little bit, I think it's wrong.
If I supply the same vector as input (beginDir equal to endDir), the cross product is zero, but the dot product is a little bit more than zero.
I think that to fix that I can simply check if the cross product is zero, means that the 2 vectors are parallel, but my code doesn't work.
I tried the following solution:
Vector3 cross = Vector3.Cross(beginDir, endDir);
if (cross == Vector.zero)
return AngleDir.PARALLEL;
And it doesn't work because comparison between Vector.zero and cross is always different from zero (even if cross is actually [0.0f, 0.0f, 0.0f]).
I tried also this:
Vector3 cross = Vector3.Cross(beginDir, endDir);
if (cross.magnitude == 0.0f)
return AngleDir.PARALLEL;
it also fails because magnitude is slightly more than zero.
So my question is: given 2 Vector3 in Unity, how to compare them?
I need the elegant equivalent version of this:
if (beginDir.x == endDir.x && beginDir.y == endDir.y && beginDir.z == endDir.z)
return true;