minor code cleanups, and use fast inverse square root in a few places

This commit is contained in:
Gered 2013-08-24 22:12:58 -04:00
parent d008ce4875
commit 060b1c7ab2
8 changed files with 72 additions and 60 deletions

View file

@ -307,12 +307,9 @@ namespace Blarg.GameFramework
{ {
bool foundCollision = false; bool foundCollision = false;
Vector3 p1; Vector3 p1 = v1 / packet.EllipsoidRadius;
Vector3 p2; Vector3 p2 = v2 / packet.EllipsoidRadius;
Vector3 p3; Vector3 p3 = v3 / packet.EllipsoidRadius;
Vector3.Divide(ref v1, ref packet.EllipsoidRadius, out p1);
Vector3.Divide(ref v2, ref packet.EllipsoidRadius, out p2);
Vector3.Divide(ref v3, ref packet.EllipsoidRadius, out p3);
var trianglePlane = new Plane(ref p1, ref p2, ref p3); var trianglePlane = new Plane(ref p1, ref p2, ref p3);

View file

@ -313,7 +313,7 @@ namespace Blarg.GameFramework
if (forwardLengthSquared < 0.0001f) if (forwardLengthSquared < 0.0001f)
forward = -cameraForward; forward = -cameraForward;
else else
forward = forward * (1.0f / ((float)Math.Sqrt(forwardLengthSquared))); forward = forward * MathHelpers.FastInverseSqrt(forwardLengthSquared);
Vector3 left = Vector3.Normalize(Vector3.Cross(cameraUp, forward)); Vector3 left = Vector3.Normalize(Vector3.Cross(cameraUp, forward));
Vector3 up = Vector3.Cross(forward, left); Vector3 up = Vector3.Cross(forward, left);
@ -353,7 +353,7 @@ namespace Blarg.GameFramework
if (lengthSquared < 0.0001f) if (lengthSquared < 0.0001f)
temp = -cameraForward; temp = -cameraForward;
else else
temp = temp * (1.0f / ((float)Math.Sqrt(lengthSquared))); temp = temp * MathHelpers.FastInverseSqrt(lengthSquared);
Vector3 up = axis; Vector3 up = axis;
Vector3 forward; Vector3 forward;

View file

@ -45,7 +45,7 @@ namespace Blarg.GameFramework
Vector3 e3 = b - a; Vector3 e3 = b - a;
Vector3 e1 = c - b; Vector3 e1 = c - b;
Vector3 crossed = Vector3.Cross(e3, e1); Vector3 crossed = Vector3.Cross(e3, e1);
float scaleFactor = 1.0f / crossed.Length; float scaleFactor = crossed.InverseLength;
Normal = crossed * scaleFactor; Normal = crossed * scaleFactor;
@ -107,16 +107,11 @@ namespace Blarg.GameFramework
public static void Normalize(ref Plane plane, out Plane result) public static void Normalize(ref Plane plane, out Plane result)
{ {
float length = (float)Math.Sqrt( float inverseLength = plane.Normal.InverseLength;
(plane.Normal.X * plane.Normal.X) + result.Normal.X = plane.Normal.X * inverseLength;
(plane.Normal.Y * plane.Normal.Y) + result.Normal.Y = plane.Normal.Y * inverseLength;
(plane.Normal.Z * plane.Normal.Z) result.Normal.Z = plane.Normal.Z * inverseLength;
); result.D = plane.D * inverseLength;
result.Normal.X = plane.Normal.X / length;
result.Normal.Y = plane.Normal.Y / length;
result.Normal.Z = plane.Normal.Z / length;
result.D = plane.D / length;
} }
public static bool operator ==(Plane left, Plane right) public static bool operator ==(Plane left, Plane right)

View file

@ -36,6 +36,11 @@ namespace Blarg.GameFramework
} }
} }
public float InverseLength
{
get { return MathHelpers.FastInverseSqrt(LengthSquared); }
}
public Vector3 Vector public Vector3 Vector
{ {
get { return new Vector3(X, Y, Z); } get { return new Vector3(X, Y, Z); }
@ -267,7 +272,7 @@ namespace Blarg.GameFramework
public static void Normalize(ref Quaternion q, out Quaternion result) public static void Normalize(ref Quaternion q, out Quaternion result)
{ {
float inverseLength = 1.0f / q.Length; float inverseLength = q.InverseLength;
result.X = q.X * inverseLength; result.X = q.X * inverseLength;
result.Y = q.Y * inverseLength; result.Y = q.Y * inverseLength;
result.Z = q.Z * inverseLength; result.Z = q.Z * inverseLength;

View file

@ -35,6 +35,11 @@ namespace Blarg.GameFramework
} }
} }
public float InverseLength
{
get { return MathHelpers.FastInverseSqrt(LengthSquared); }
}
public Vector2(float x, float y) public Vector2(float x, float y)
{ {
X = x; X = x;
@ -150,7 +155,7 @@ namespace Blarg.GameFramework
public static void Normalize(ref Vector2 v, out Vector2 result) public static void Normalize(ref Vector2 v, out Vector2 result)
{ {
float inverseLength = 1.0f / v.Length; float inverseLength = v.InverseLength;
result.X = v.X * inverseLength; result.X = v.X * inverseLength;
result.Y = v.Y * inverseLength; result.Y = v.Y * inverseLength;
} }

View file

@ -45,6 +45,11 @@ namespace Blarg.GameFramework
} }
} }
public float InverseLength
{
get { return MathHelpers.FastInverseSqrt(LengthSquared); }
}
public Vector3(float x, float y, float z) public Vector3(float x, float y, float z)
{ {
X = x; X = x;
@ -184,7 +189,7 @@ namespace Blarg.GameFramework
public static void Normalize(ref Vector3 v, out Vector3 result) public static void Normalize(ref Vector3 v, out Vector3 result)
{ {
float inverseLength = 1.0f / v.Length; float inverseLength = v.InverseLength;
result.X = v.X * inverseLength; result.X = v.X * inverseLength;
result.Y = v.Y * inverseLength; result.Y = v.Y * inverseLength;
result.Z = v.Z * inverseLength; result.Z = v.Z * inverseLength;

View file

@ -39,6 +39,11 @@ namespace Blarg.GameFramework
} }
} }
public float InverseLength
{
get { return MathHelpers.FastInverseSqrt(LengthSquared); }
}
public Vector4(float x, float y, float z, float w) public Vector4(float x, float y, float z, float w)
{ {
X = x; X = x;
@ -148,7 +153,7 @@ namespace Blarg.GameFramework
public static void Normalize(ref Vector4 v, out Vector4 result) public static void Normalize(ref Vector4 v, out Vector4 result)
{ {
float inverseLength = 1.0f / v.Length; float inverseLength = v.InverseLength;
result.X = v.X * inverseLength; result.X = v.X * inverseLength;
result.Y = v.Y * inverseLength; result.Y = v.Y * inverseLength;
result.Z = v.Z * inverseLength; result.Z = v.Z * inverseLength;