using EvilEngine.Formats.Maps; using EvilEngine.Host.Movement; namespace EvilEngine.Host.Tests; internal sealed class HullBoxWorld : IHullWorld { public const double Hair = 1e-4; private const double Touch = 1e-7; private readonly List<(MapVector Normal, double Distance)[]> _brushes = []; public HullHit Trace(HullBox hull, MapVector start, MapVector end) { var best = double.MaxValue; var normal = MapVector.Zero; foreach (var brush in _brushes) { var hit = TraceBrush(brush, hull, start, end); if (hit is null) continue; if (hit.Value.StartSolid) return new HullHit(0, start, MapVector.Zero, true); if (hit.Value.Fraction < best) { best = hit.Value.Fraction; normal = hit.Value.Normal; } } if (best == double.MaxValue) return new HullHit(1, end, MapVector.Zero, false); return new HullHit(best, Lerp(start, end, best), normal, false); } public HullBoxWorld Box(MapVector min, MapVector max) { _brushes.Add( [ (new MapVector(1, 0, 0), max.X), (new MapVector(-1, 0, 0), -min.X), (new MapVector(0, 1, 0), max.Y), (new MapVector(0, -1, 0), -min.Y), (new MapVector(0, 0, 1), max.Z), (new MapVector(0, 0, -1), -min.Z) ]); return this; } public HullBoxWorld Ramp(double minX, double maxX, double lowZ, double highZ, double baseY, double degrees) { var slope = Math.Tan(degrees * Math.PI / 180); var top = baseY + (lowZ - highZ) * slope; var scale = Math.Sqrt(1 + slope * slope); _brushes.Add( [ (new MapVector(1, 0, 0), maxX), (new MapVector(-1, 0, 0), -minX), (new MapVector(0, 1, 0), top), (new MapVector(0, -1, 0), -baseY), (new MapVector(0, 0, 1), lowZ), (new MapVector(0, 0, -1), -highZ), (new MapVector(0, 1 / scale, slope / scale), (baseY + lowZ * slope) / scale) ]); return this; } private static (double Fraction, MapVector Normal, bool StartSolid)? TraceBrush( (MapVector Normal, double Distance)[] planes, HullBox hull, MapVector start, MapVector end) { var enter = -1.0; var exit = 1.0; var outside = false; var normal = MapVector.Zero; var gap = 0.0; var closing = 0.0; foreach (var (n, distance) in planes) { var reach = Pick(n.X, hull.Min.X, hull.Max.X) + Pick(n.Y, hull.Min.Y, hull.Max.Y) + Pick(n.Z, hull.Min.Z, hull.Max.Z); var limit = distance - reach; var ds = Dot(n, start) - limit; var de = Dot(n, end) - limit; if (ds > 0 && de > 0) return null; if (ds >= -Touch) { outside = true; if (de >= -Touch) continue; var from = Math.Max(ds, 0); var t = from / (from - de); if (t > enter) { enter = t; normal = n; gap = from; closing = from - de; } } else if (de > 0) { exit = Math.Min(exit, ds / (ds - de)); } } if (!outside) return (0, MapVector.Zero, true); if (enter < 0 || enter > exit) return null; return (Math.Max(0, (gap - Hair) / closing), normal, false); } private static double Pick(double n, double min, double max) { return n < 0 ? n * max : n * min; } private static double Dot(MapVector a, MapVector b) { return a.X * b.X + a.Y * b.Y + a.Z * b.Z; } private static MapVector Lerp(MapVector a, MapVector b, double t) { return new MapVector(a.X + (b.X - a.X) * t, a.Y + (b.Y - a.Y) * t, a.Z + (b.Z - a.Z) * t); } }