Commits

André Schnabel committed e298ccc

added ray point distance to mathutils

Comments (0)

Files changed (3)

Math/Intersector.cs

 using System.Collections.Generic;
+using System.Diagnostics;
 using Kosu.Utilities;
 
 namespace Kosu.Math {
 		/// Direction of the ray.
 		/// </param>
 		public Ray(Vector3 position, Vector3 direction) {
+			Debug.Assert(direction.LengthSquared - 1.0f < 0.00001f, "Ray direction must be normalized!");
 			Position = position;
 			Direction = direction;
 		}
 			get { return (float) System.Math.Sqrt(X*X + Y*Y + Z*Z); }
 		}
 
+		public float LengthSquared {
+			get { return X*X + Y*Y + Z*Z; }
+		}
+
 		/// <summary>
 		/// Rot the vector around the given axis with angle alpha (in radians, ccw).
 		/// </summary>

Utilities/MathUtils.cs

 		}
 
 		// TODO: Write tests for ConvertToBase functions.
-
 		/// <summary>
 		/// Convert a base-10 (decimal) integer value to an array of base-radix (param) digits.
 		/// </summary>
 			return (float) System.Math.Tan(alpha);
 		}
 		#endregion
+
+		public static float RayPointDistance(Ray ray, Vector3 point) {
+			return (float) System.Math.Sqrt(RayPointDistanceSquared(ray, point));
+		}
+
+		public static float RayPointDistanceSquared(Ray ray, Vector3 point) {
+			Vector3 w = point - ray.Position;
+			float projectedDist = w*ray.Direction;
+			return w.LengthSquared - projectedDist*projectedDist;
+		}
 	}
 }