diff --git a/LibBSP/Source/Extensions/PlaneExtensions.cs b/LibBSP/Source/Extensions/PlaneExtensions.cs index 7e03180..1aebdc3 100644 --- a/LibBSP/Source/Extensions/PlaneExtensions.cs +++ b/LibBSP/Source/Extensions/PlaneExtensions.cs @@ -227,6 +227,57 @@ namespace LibBSP { return points; } + /// + /// Gets the signed distance from this to a given point. + /// + /// This . + /// Point to get the distance to. + /// Signed distance from this to the given point. + /// Unity uses the plane equation "Ax + By + Cz + D = 0" while Quake-based engines + /// use "Ax + By + Cz = D". The distance equation needs to be evaluated differently from + /// Unity's default implementation to properly apply to planes read from BSPs. +#if UNITY + public static float GetBSPDistanceToPoint(this Plane p, Vector3 to) { +#else + public static double GetBSPDistanceToPoint(this Plane p, Vector3 to) { +#endif +#if UNITY + float normLength = Mathf.Pow(p.normal.x, 2) + Mathf.Pow(p.normal.y, 2) + Mathf.Pow(p.normal.z, 2); + if (Mathf.Abs(normLength - 1.00f) > 0.01) { + normLength = Mathf.Sqrt(normLength); + } + return (p.normal.x * to.x + p.normal.y * to.y + p.normal.z * to.z - p.distance) / normLength; +#else + return p.GetDistanceToPoint(to); +#endif + } + + /// + /// Is on the positive side of this ? + /// + /// This . + /// Point to get the side for. + /// true if is on the positive side of this . + /// Unity uses the plane equation "Ax + By + Cz + D = 0" while Quake-based engines + /// use "Ax + By + Cz = D". The distance equation needs to be evaluated differently from + /// Unity's default implementation to properly apply to planes read from BSPs. + public static bool GetBSPSide(this Plane p, Vector3 v) { + return p.GetBSPDistanceToPoint(v) > 0; + } + + /// + /// Determines whether the given is contained in this . + /// + /// Point. + /// true if the is contained in this . + /// Unity uses the plane equation "Ax + By + Cz + D = 0" while Quake-based engines + /// use "Ax + By + Cz = D". The distance equation needs to be evaluated differently from + /// Unity's default implementation to properly apply to planes read from BSPs. + public static bool BSPContains(this Plane p, Vector3 v) { + var distanceTo = p.GetBSPDistanceToPoint(v); + return distanceTo < 0.001 && distanceTo > -0.001; + } + /// /// Factory method to parse a byte array into a List of objects. /// diff --git a/LibBSP/Source/Structs/Common/Plane.cs b/LibBSP/Source/Structs/Common/Plane.cs index 3ee7301..327cdd6 100644 --- a/LibBSP/Source/Structs/Common/Plane.cs +++ b/LibBSP/Source/Structs/Common/Plane.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; namespace LibBSP { /// - /// Holds the data for a plane in 3D space in Hessian Normal Form. + /// Holds the data for a plane in 3D space in Hesse Normal Form. /// [Serializable] public struct Plane : IEquatable { diff --git a/LibBSP/Source/Util/BSPReader.cs b/LibBSP/Source/Util/BSPReader.cs index 3c834cd..58ce277 100644 --- a/LibBSP/Source/Util/BSPReader.cs +++ b/LibBSP/Source/Util/BSPReader.cs @@ -41,7 +41,7 @@ namespace LibBSP { /// The numerical index of this lump. /// The type of BSP to interpret the file as. /// A object containing information about the lump. - /// "" is less than zero, or greater than the number of lumps allowed by "". + /// "" is less than zero, or greater than the number of lumps allowed by "". public LumpInfo GetLumpInfo(int index, MapType version) { if (index < 0 || index >= BSP.GetNumLumps(version)) { throw new IndexOutOfRangeException();