Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Math] Fix NaN issue in CalculateAngle #223

Merged
merged 1 commit into from Mar 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions Source/OpenTK/Math/Vector3.cs
Expand Up @@ -1205,7 +1205,9 @@ public static void TransformPerspective(ref Vector3 vec, ref Matrix4 mat, out Ve
/// <remarks>Note that the returned angle is never bigger than the constant Pi.</remarks>
public static float CalculateAngle(Vector3 first, Vector3 second)
{
return (float)System.Math.Acos((Vector3.Dot(first, second)) / (first.Length * second.Length));
float result;
CalculateAngle(ref first, ref second, out result);
return result;
}

/// <summary>Calculates the angle (in radians) between two vectors.</summary>
Expand All @@ -1217,7 +1219,7 @@ public static void CalculateAngle(ref Vector3 first, ref Vector3 second, out flo
{
float temp;
Vector3.Dot(ref first, ref second, out temp);
result = (float)System.Math.Acos(temp / (first.Length * second.Length));
result = (float)System.Math.Acos(MathHelper.Clamp(temp / (first.Length * second.Length), -1.0, 1.0));
}

#endregion
Expand Down
6 changes: 4 additions & 2 deletions Source/OpenTK/Math/Vector3d.cs
Expand Up @@ -1203,7 +1203,9 @@ public static void TransformPerspective(ref Vector3d vec, ref Matrix4d mat, out
/// <remarks>Note that the returned angle is never bigger than the constant Pi.</remarks>
public static double CalculateAngle(Vector3d first, Vector3d second)
{
return System.Math.Acos((Vector3d.Dot(first, second)) / (first.Length * second.Length));
double result;
CalculateAngle(ref first, ref second, out result);
return result;
}

/// <summary>Calculates the angle (in radians) between two vectors.</summary>
Expand All @@ -1215,7 +1217,7 @@ public static void CalculateAngle(ref Vector3d first, ref Vector3d second, out d
{
double temp;
Vector3d.Dot(ref first, ref second, out temp);
result = System.Math.Acos(temp / (first.Length * second.Length));
result = System.Math.Acos(MathHelper.Clamp(temp / (first.Length * second.Length), -1.0, 1.0));
}

#endregion
Expand Down