0.2.0-dev Download ZIP
131 lines ·4.1 KB Raw
| 1 | using EvilEngine.Formats.Maps; |
| 2 | using EvilEngine.Host.Movement; |
| 3 | |
| 4 | namespace EvilEngine.Host.Tests; |
| 5 | |
| 6 | internal sealed class HullBoxWorld : IHullWorld |
| 7 | { |
| 8 | public const double Hair = 1e-4; |
| 9 | private const double Touch = 1e-7; |
| 10 | |
| 11 | private readonly List<(MapVector Normal, double Distance)[]> _brushes = []; |
| 12 | |
| 13 | public HullHit Trace(HullBox hull, MapVector start, MapVector end) |
| 14 | { |
| 15 | var best = double.MaxValue; |
| 16 | var normal = MapVector.Zero; |
| 17 | foreach (var brush in _brushes) |
| 18 | { |
| 19 | var hit = TraceBrush(brush, hull, start, end); |
| 20 | if (hit is null) |
| 21 | continue; |
| 22 | if (hit.Value.StartSolid) |
| 23 | return new HullHit(0, start, MapVector.Zero, true); |
| 24 | if (hit.Value.Fraction < best) |
| 25 | { |
| 26 | best = hit.Value.Fraction; |
| 27 | normal = hit.Value.Normal; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | if (best == double.MaxValue) |
| 32 | return new HullHit(1, end, MapVector.Zero, false); |
| 33 | return new HullHit(best, Lerp(start, end, best), normal, false); |
| 34 | } |
| 35 | |
| 36 | public HullBoxWorld Box(MapVector min, MapVector max) |
| 37 | { |
| 38 | _brushes.Add( |
| 39 | [ |
| 40 | (new MapVector(1, 0, 0), max.X), |
| 41 | (new MapVector(-1, 0, 0), -min.X), |
| 42 | (new MapVector(0, 1, 0), max.Y), |
| 43 | (new MapVector(0, -1, 0), -min.Y), |
| 44 | (new MapVector(0, 0, 1), max.Z), |
| 45 | (new MapVector(0, 0, -1), -min.Z) |
| 46 | ]); |
| 47 | return this; |
| 48 | } |
| 49 | |
| 50 | public HullBoxWorld Ramp(double minX, double maxX, double lowZ, double highZ, double baseY, double degrees) |
| 51 | { |
| 52 | var slope = Math.Tan(degrees * Math.PI / 180); |
| 53 | var top = baseY + (lowZ - highZ) * slope; |
| 54 | var scale = Math.Sqrt(1 + slope * slope); |
| 55 | _brushes.Add( |
| 56 | [ |
| 57 | (new MapVector(1, 0, 0), maxX), |
| 58 | (new MapVector(-1, 0, 0), -minX), |
| 59 | (new MapVector(0, 1, 0), top), |
| 60 | (new MapVector(0, -1, 0), -baseY), |
| 61 | (new MapVector(0, 0, 1), lowZ), |
| 62 | (new MapVector(0, 0, -1), -highZ), |
| 63 | (new MapVector(0, 1 / scale, slope / scale), (baseY + lowZ * slope) / scale) |
| 64 | ]); |
| 65 | return this; |
| 66 | } |
| 67 | |
| 68 | private static (double Fraction, MapVector Normal, bool StartSolid)? TraceBrush( |
| 69 | (MapVector Normal, double Distance)[] planes, |
| 70 | HullBox hull, MapVector start, MapVector end) |
| 71 | { |
| 72 | var enter = -1.0; |
| 73 | var exit = 1.0; |
| 74 | var outside = false; |
| 75 | var normal = MapVector.Zero; |
| 76 | var gap = 0.0; |
| 77 | var closing = 0.0; |
| 78 | |
| 79 | foreach (var (n, distance) in planes) |
| 80 | { |
| 81 | var reach = Pick(n.X, hull.Min.X, hull.Max.X) + Pick(n.Y, hull.Min.Y, hull.Max.Y) + |
| 82 | Pick(n.Z, hull.Min.Z, hull.Max.Z); |
| 83 | var limit = distance - reach; |
| 84 | var ds = Dot(n, start) - limit; |
| 85 | var de = Dot(n, end) - limit; |
| 86 | |
| 87 | if (ds > 0 && de > 0) |
| 88 | return null; |
| 89 | if (ds >= -Touch) |
| 90 | { |
| 91 | outside = true; |
| 92 | if (de >= -Touch) |
| 93 | continue; |
| 94 | var from = Math.Max(ds, 0); |
| 95 | var t = from / (from - de); |
| 96 | if (t > enter) |
| 97 | { |
| 98 | enter = t; |
| 99 | normal = n; |
| 100 | gap = from; |
| 101 | closing = from - de; |
| 102 | } |
| 103 | } |
| 104 | else if (de > 0) |
| 105 | { |
| 106 | exit = Math.Min(exit, ds / (ds - de)); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (!outside) |
| 111 | return (0, MapVector.Zero, true); |
| 112 | if (enter < 0 || enter > exit) |
| 113 | return null; |
| 114 | return (Math.Max(0, (gap - Hair) / closing), normal, false); |
| 115 | } |
| 116 | |
| 117 | private static double Pick(double n, double min, double max) |
| 118 | { |
| 119 | return n < 0 ? n * max : n * min; |
| 120 | } |
| 121 | |
| 122 | private static double Dot(MapVector a, MapVector b) |
| 123 | { |
| 124 | return a.X * b.X + a.Y * b.Y + a.Z * b.Z; |
| 125 | } |
| 126 | |
| 127 | private static MapVector Lerp(MapVector a, MapVector b, double t) |
| 128 | { |
| 129 | return new MapVector(a.X + (b.X - a.X) * t, a.Y + (b.Y - a.Y) * t, a.Z + (b.Z - a.Z) * t); |
| 130 | } |
| 131 | } |