public static float DistanceSquared(AABB aabb, Vector2f point) { float sqDist = 0f; for (int i = 0; i < 2; i++) { // For each axis count any excess distance outside box contents float v = point[i]; if (v < aabb.Min[i]) sqDist += (aabb.Min[i] - v) * (aabb.Min[i] - v); if (v > aabb.Max[i]) sqDist += (v - aabb.Max[i]) * (v - aabb.Max[i]); } return sqDist; }
public bool Intersects(AABB other) { if (// Max < other.Min this.Max.X < other.Min.X || this.Max.Y < other.Min.Y || this.Max.Z < other.Min.Z || // Min > other.Max this.Min.X > other.Max.X || this.Min.Y > other.Max.Y || this.Min.Z > other.Max.Z) { return false; } return true; // Intersects if above fails }
public static float Distance(AABB aabb, Vector3f point) { return (float)Math.Sqrt(DistanceSquared(aabb, point)); }