Skip to content

Commit

Permalink
Merge pull request #223 from Frassle/angle
Browse files Browse the repository at this point in the history
[Math] Fix NaN issue in CalculateAngle
  • Loading branch information
Frassle committed Mar 16, 2015
2 parents 3efb4e3 + 75961e6 commit 282f1d1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
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

0 comments on commit 282f1d1

Please sign in to comment.