diff --git a/Blarg.GameFramework/Math/IntersectionTester.cs b/Blarg.GameFramework/Math/IntersectionTester.cs index 188df3e..f051221 100644 --- a/Blarg.GameFramework/Math/IntersectionTester.cs +++ b/Blarg.GameFramework/Math/IntersectionTester.cs @@ -307,12 +307,9 @@ namespace Blarg.GameFramework { bool foundCollision = false; - Vector3 p1; - Vector3 p2; - Vector3 p3; - 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); + Vector3 p1 = v1 / packet.EllipsoidRadius; + Vector3 p2 = v2 / packet.EllipsoidRadius; + Vector3 p3 = v3 / packet.EllipsoidRadius; var trianglePlane = new Plane(ref p1, ref p2, ref p3); diff --git a/Blarg.GameFramework/Math/Matrix3x3.cs b/Blarg.GameFramework/Math/Matrix3x3.cs index 33e10cb..5bd1ca4 100644 --- a/Blarg.GameFramework/Math/Matrix3x3.cs +++ b/Blarg.GameFramework/Math/Matrix3x3.cs @@ -32,11 +32,11 @@ namespace Blarg.GameFramework { return M11 * M22 * M33 + - M12 * M23 * M31 + - M13 * M21 * M32 - - M11 * M23 * M32 - - M12 * M21 * M33 - - M13 * M22 * M31; + M12 * M23 * M31 + + M13 * M21 * M32 - + M11 * M23 * M32 - + M12 * M21 * M33 - + M13 * M22 * M31; } } diff --git a/Blarg.GameFramework/Math/Matrix4x4.cs b/Blarg.GameFramework/Math/Matrix4x4.cs index a398e14..bb779f2 100644 --- a/Blarg.GameFramework/Math/Matrix4x4.cs +++ b/Blarg.GameFramework/Math/Matrix4x4.cs @@ -46,17 +46,17 @@ namespace Blarg.GameFramework { return (M11 * M22 - M21 * M12) * - (M33 * M44 - M43 * M34) - - (M11 * M32 - M31 * M12) * - (M23 * M44 - M43 * M24) + - (M11 * M42 - M41 * M12) * - (M23 * M34 - M33 * M24) + - (M21 * M32 - M31 * M22) * - (M13 * M44 - M43 * M14) - - (M21 * M42 - M41 * M22) * - (M13 * M34 - M33 * M14) + - (M31 * M42 - M41 * M32) * - (M13 * M24 - M23 * M14); + (M33 * M44 - M43 * M34) - + (M11 * M32 - M31 * M12) * + (M23 * M44 - M43 * M24) + + (M11 * M42 - M41 * M12) * + (M23 * M34 - M33 * M24) + + (M21 * M32 - M31 * M22) * + (M13 * M44 - M43 * M14) - + (M21 * M42 - M41 * M22) * + (M13 * M34 - M33 * M14) + + (M31 * M42 - M41 * M32) * + (M13 * M24 - M23 * M14); } } @@ -313,7 +313,7 @@ namespace Blarg.GameFramework if (forwardLengthSquared < 0.0001f) forward = -cameraForward; else - forward = forward * (1.0f / ((float)Math.Sqrt(forwardLengthSquared))); + forward = forward * MathHelpers.FastInverseSqrt(forwardLengthSquared); Vector3 left = Vector3.Normalize(Vector3.Cross(cameraUp, forward)); Vector3 up = Vector3.Cross(forward, left); @@ -353,7 +353,7 @@ namespace Blarg.GameFramework if (lengthSquared < 0.0001f) temp = -cameraForward; else - temp = temp * (1.0f / ((float)Math.Sqrt(lengthSquared))); + temp = temp * MathHelpers.FastInverseSqrt(lengthSquared); Vector3 up = axis; Vector3 forward; diff --git a/Blarg.GameFramework/Math/Plane.cs b/Blarg.GameFramework/Math/Plane.cs index d0ac9f9..891831f 100644 --- a/Blarg.GameFramework/Math/Plane.cs +++ b/Blarg.GameFramework/Math/Plane.cs @@ -45,7 +45,7 @@ namespace Blarg.GameFramework Vector3 e3 = b - a; Vector3 e1 = c - b; Vector3 crossed = Vector3.Cross(e3, e1); - float scaleFactor = 1.0f / crossed.Length; + float scaleFactor = crossed.InverseLength; Normal = crossed * scaleFactor; @@ -107,16 +107,11 @@ namespace Blarg.GameFramework public static void Normalize(ref Plane plane, out Plane result) { - float length = (float)Math.Sqrt( - (plane.Normal.X * plane.Normal.X) + - (plane.Normal.Y * plane.Normal.Y) + - (plane.Normal.Z * plane.Normal.Z) - ); - - 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; + float inverseLength = plane.Normal.InverseLength; + result.Normal.X = plane.Normal.X * inverseLength; + result.Normal.Y = plane.Normal.Y * inverseLength; + result.Normal.Z = plane.Normal.Z * inverseLength; + result.D = plane.D * inverseLength; } public static bool operator ==(Plane left, Plane right) diff --git a/Blarg.GameFramework/Math/Quaternion.cs b/Blarg.GameFramework/Math/Quaternion.cs index 437dc5d..1a48311 100644 --- a/Blarg.GameFramework/Math/Quaternion.cs +++ b/Blarg.GameFramework/Math/Quaternion.cs @@ -30,12 +30,17 @@ namespace Blarg.GameFramework { return (W * W) + - (X * X) + - (Y * Y) + - (Z * Z); + (X * X) + + (Y * Y) + + (Z * Z); } } + public float InverseLength + { + get { return MathHelpers.FastInverseSqrt(LengthSquared); } + } + public Vector3 Vector { get { return new Vector3(X, Y, Z); } @@ -267,7 +272,7 @@ namespace Blarg.GameFramework 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.Y = q.Y * inverseLength; result.Z = q.Z * inverseLength; diff --git a/Blarg.GameFramework/Math/Vector2.cs b/Blarg.GameFramework/Math/Vector2.cs index 694a822..c89c51e 100644 --- a/Blarg.GameFramework/Math/Vector2.cs +++ b/Blarg.GameFramework/Math/Vector2.cs @@ -31,10 +31,15 @@ namespace Blarg.GameFramework { return (X * X) + - (Y * Y); + (Y * Y); } } + public float InverseLength + { + get { return MathHelpers.FastInverseSqrt(LengthSquared); } + } + public Vector2(float x, float y) { X = x; @@ -113,7 +118,7 @@ namespace Blarg.GameFramework { return ((b.X - a.X) * (b.X - a.X)) + - ((b.Y - a.Y) * (b.Y - a.Y)); + ((b.Y - a.Y) * (b.Y - a.Y)); } public static float Dot(Vector2 a, Vector2 b) @@ -125,7 +130,7 @@ namespace Blarg.GameFramework { return (a.X * b.X) + - (a.Y * b.Y); + (a.Y * b.Y); } public static Vector2 Lerp(Vector2 a, Vector2 b, float interpolation) @@ -150,7 +155,7 @@ namespace Blarg.GameFramework 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.Y = v.Y * inverseLength; } diff --git a/Blarg.GameFramework/Math/Vector3.cs b/Blarg.GameFramework/Math/Vector3.cs index 54aa593..53bab12 100644 --- a/Blarg.GameFramework/Math/Vector3.cs +++ b/Blarg.GameFramework/Math/Vector3.cs @@ -40,11 +40,16 @@ namespace Blarg.GameFramework { return (X * X) + - (Y * Y) + - (Z * Z); + (Y * Y) + + (Z * Z); } } + public float InverseLength + { + get { return MathHelpers.FastInverseSqrt(LengthSquared); } + } + public Vector3(float x, float y, float z) { X = x; @@ -144,8 +149,8 @@ namespace Blarg.GameFramework { return ((b.X - a.X) * (b.X - a.X)) + - ((b.Y - a.Y) * (b.Y - a.Y)) + - ((b.Z - a.Z) * (b.Z - a.Z)); + ((b.Y - a.Y) * (b.Y - a.Y)) + + ((b.Z - a.Z) * (b.Z - a.Z)); } public static float Dot(Vector3 a, Vector3 b) @@ -157,8 +162,8 @@ namespace Blarg.GameFramework { return (a.X * b.X) + - (a.Y * b.Y) + - (a.Z * b.Z); + (a.Y * b.Y) + + (a.Z * b.Z); } public static Vector3 Lerp(Vector3 a, Vector3 b, float interpolation) @@ -184,7 +189,7 @@ namespace Blarg.GameFramework 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.Y = v.Y * inverseLength; result.Z = v.Z * inverseLength; diff --git a/Blarg.GameFramework/Math/Vector4.cs b/Blarg.GameFramework/Math/Vector4.cs index 9bfb708..d6ee3f5 100644 --- a/Blarg.GameFramework/Math/Vector4.cs +++ b/Blarg.GameFramework/Math/Vector4.cs @@ -33,12 +33,17 @@ namespace Blarg.GameFramework { return (X * X) + - (Y * Y) + - (Z * Z) + - (W * W); + (Y * Y) + + (Z * Z) + + (W * W); } } + public float InverseLength + { + get { return MathHelpers.FastInverseSqrt(LengthSquared); } + } + public Vector4(float x, float y, float z, float w) { X = x; @@ -105,9 +110,9 @@ namespace Blarg.GameFramework { return ((b.X - a.X) * (b.X - a.X)) + - ((b.Y - a.Y) * (b.Y - a.Y)) + - ((b.Z - a.Z) * (b.Z - a.Z)) + - ((b.W - a.W) * (b.W - a.W)); + ((b.Y - a.Y) * (b.Y - a.Y)) + + ((b.Z - a.Z) * (b.Z - a.Z)) + + ((b.W - a.W) * (b.W - a.W)); } public static float Dot(Vector4 a, Vector4 b) @@ -119,9 +124,9 @@ namespace Blarg.GameFramework { return (a.X * b.X) + - (a.Y * b.Y) + - (a.Z * b.Z) + - (a.W * b.W); + (a.Y * b.Y) + + (a.Z * b.Z) + + (a.W * b.W); } public static Vector4 Lerp(Vector4 a, Vector4 b, float interpolation) @@ -148,7 +153,7 @@ namespace Blarg.GameFramework 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.Y = v.Y * inverseLength; result.Z = v.Z * inverseLength;