Skip to content
0.2.0-dev Download ZIP
205 lines ·8.5 KB Raw
1using System.Numerics;
2using EvilEngine.Api.Physics;
3using EvilEngine.Formats.Collision;
4using EvilEngine.Formats.Maps;
5using EvilEngine.Formats.Records;
6using EvilEngine.Host.Liquids;
7using EvilEngine.Host.Physics;
8
9namespace EvilEngine.Host.Tests;
10
11public sealed class FloatingTests
12{
13 private const double Tick = 0.015;
14 private static readonly MapVector Gravity = new(0, -15.24, 0);
15
16 private static readonly Vector3[] Cube =
17 [
18 new(-1, -1, -1), new(1, -1, -1), new(-1, 1, -1), new(1, 1, -1),
19 new(-1, -1, 1), new(1, -1, 1), new(-1, 1, 1), new(1, 1, 1)
20 ];
21
22 private static BodyShape Box(double size)
23 {
24 var half = (float)(size / 2);
25 return BodyShape.Of(new CollisionContent([
26 new Collider(new ColliderShape.Box(new Vector3(2 * half)), ["solid"])
27 ]));
28 }
29
30 private static LiquidSpace Sea(double height, Liquid? liquid = null, MapVector? flow = null)
31 {
32 var space = new LiquidSpace(_ => null, _ => { });
33 space.Add(new LiquidBody(null, liquid ?? new Liquid { Base = EngineLiquids.For("water") },
34 new LiquidShape.Level(height)) { Flow = flow });
35 return space;
36 }
37
38 private static (MapVector Position, MapVector Velocity, Floated? Last) Drop(LiquidSpace sea, BodyShape shape,
39 double mass, MapVector start, double seconds, Orientation? orientation = null)
40 {
41 var (position, velocity, spin) = (start, MapVector.Zero, MapVector.Zero);
42 Floated? last = null;
43 for (var tick = 0; tick < seconds / Tick; tick++)
44 {
45 var body = new FloatingBody(shape, mass, position, orientation ?? Orientation.Identity, velocity, spin);
46 last = Floating.Float(sea, body, Gravity, Tick);
47 if (last is not null)
48 (velocity, spin) = (last.Velocity, last.Spin);
49 var push = last?.Buoyancy ?? MapVector.Zero;
50 velocity = new MapVector(velocity.X + (Gravity.X + push.X / mass) * Tick,
51 velocity.Y + (Gravity.Y + push.Y / mass) * Tick, velocity.Z + (Gravity.Z + push.Z / mass) * Tick);
52 position = new MapVector(position.X + velocity.X * Tick, position.Y + velocity.Y * Tick,
53 position.Z + velocity.Z * Tick);
54 }
55
56 return (position, velocity, last);
57 }
58
59 [Fact]
60 public void MeasuresEveryShapesVolume()
61 {
62 Assert.Equal(8, Box(2).Volume, 9);
63 Assert.Equal(8, BodyShape.Of(new CollisionContent([
64 new Collider(new ColliderShape.Hull(Cube), ["solid"])
65 ])).Volume, 9);
66 var sphere = BodyShape.Of(new CollisionContent([new Collider(new ColliderShape.Sphere(0.5f), ["solid"])]));
67 Assert.Equal(4.0 / 3 * Math.PI * 0.125, sphere.Volume, 9);
68 Assert.InRange(sphere.Parts[0].MeshVolume / sphere.Volume, 0.8, 1);
69 var capsule =
70 BodyShape.Of(new CollisionContent([new Collider(new ColliderShape.Capsule(1, 0.25f), ["solid"])]));
71 Assert.Equal(Math.PI * 0.0625 * (1 + 1.0 / 3), capsule.Volume, 9);
72 var cylinder =
73 BodyShape.Of(new CollisionContent([new Collider(new ColliderShape.Cylinder(2, 0.5f), ["solid"])]));
74 Assert.Equal(Math.PI * 0.25 * 2, cylinder.Volume, 9);
75 Assert.InRange(cylinder.Parts[0].MeshVolume / cylinder.Volume, 0.95, 1);
76 }
77
78 [Fact]
79 public void KeepsAColliderWhereItsPlacedAndLeavesTriangleMeshesOut()
80 {
81 var shape = BodyShape.Of(new CollisionContent([
82 new Collider(new ColliderShape.Box(Vector3.One), ["solid"]) { Position = new Vector3(0, 5, 0) },
83 new Collider(new ColliderShape.Mesh([
84 new MeshPart([Vector3.Zero, Vector3.UnitX, Vector3.UnitZ], [0, 1, 2], CollisionSurface.None)
85 ]), ["solid"])
86 ]));
87
88 Assert.False(shape.Convex);
89 Assert.Equal(1, shape.Volume, 9);
90 Assert.Equal(new MapVector(-0.5, 4.5, -0.5), shape.Bounds(MapVector.Zero, Orientation.Identity).Min);
91 Assert.Equal(4.5, shape.Lowest(Orientation.Identity, new MapVector(0, 1, 0)), 9);
92 }
93
94 [Fact]
95 public void TakesMassFromTheCollisionThenTheSurfacesDensityThenWaters()
96 {
97 var shape = Box(2);
98 var collision = new CollisionContent([]);
99
100 Assert.Equal(30, BodyShape.Mass(new BodySpec(collision with { Motion = new CollisionMotion { Mass = 30 } }),
101 shape));
102 Assert.Equal(12, BodyShape.Mass(new BodySpec(collision) { Mass = 12 }, shape));
103 Assert.Equal(8 * 700, BodyShape.Mass(new BodySpec(collision) { Surface = Formats.ContentId.Parse("a:wood") },
104 shape, _ => 700));
105 Assert.Equal(8 * BodyShape.DefaultDensity, BodyShape.Mass(new BodySpec(collision), shape));
106 }
107
108 [Fact]
109 public void ClipsABodyAtTheSurfaceForTheVolumeUnderAndItsCentre()
110 {
111 var up = new MapVector(0, 1, 0);
112 var level = Floating.Submerged(Box(2), MapVector.Zero, Orientation.Identity, MapVector.Zero, up);
113 Assert.Equal(4, level.Volume, 9);
114 Assert.Equal(-0.5, level.Centre.Y, 9);
115 Assert.Equal(0, level.Centre.X, 9);
116
117 var turned = Floating.Submerged(Box(2), MapVector.Zero, Orientation.Axis(new MapVector(0, 0, 1), Math.PI / 4),
118 MapVector.Zero, up);
119 Assert.Equal(4, turned.Volume, 9);
120
121 var shallow = Floating.Submerged(Box(2), new MapVector(0, 0.5, 0), Orientation.Identity, MapVector.Zero, up);
122 Assert.Equal(2, shallow.Volume, 9);
123 Assert.Equal(Box(2).Volume,
124 Floating.Submerged(Box(2), new MapVector(3, -9, 1), Orientation.Identity, MapVector.Zero, up).Volume, 9);
125 Assert.Equal(0,
126 Floating.Submerged(Box(2), new MapVector(0, 1.5, 0), Orientation.Identity, MapVector.Zero, up).Volume, 9);
127 }
128
129 [Fact]
130 public void ATiltedBodyIsPushedUpOffItsMiddle()
131 {
132 var tilted = Floating.Submerged(Box(2), MapVector.Zero, Orientation.Axis(new MapVector(0, 0, 1), 0.3),
133 new MapVector(0, 0.3, 0), new MapVector(0, 1, 0));
134
135 Assert.NotEqual(0, tilted.Centre.X, 3);
136 }
137
138 [Theory]
139 [InlineData(250, 0.25)]
140 [InlineData(500, 0.5)]
141 [InlineData(800, 0.8)]
142 public void FloatsWithItsShareUnderAsItsDensityOverWaters(double density, double share)
143 {
144 var (position, velocity, last) = Drop(Sea(0), Box(1), density, new MapVector(0, 1, 0), 30);
145
146 Assert.NotNull(last);
147 Assert.Equal(share, last.Share, 2);
148 Assert.Equal(0.5 - share, position.Y, 2);
149 Assert.Equal(0, velocity.Y, 2);
150 }
151
152 [Fact]
153 public void ABodyDenserThanTheLiquidSinksAndOneOutOfItIsLeftAlone()
154 {
155 var (position, velocity, last) = Drop(Sea(0), Box(1), 2000, new MapVector(0, -1, 0), 2);
156
157 Assert.True(position.Y < -5);
158 Assert.True(velocity.Y < 0);
159 Assert.Equal(1, last!.Share, 9);
160 Assert.Null(Floating.Float(Sea(0),
161 new FloatingBody(Box(1), 500, new MapVector(0, 3, 0), Orientation.Identity, MapVector.Zero,
162 MapVector.Zero), Gravity, Tick));
163 }
164
165 [Fact]
166 public void FloatsHigherInADenserLiquid()
167 {
168 var water = Drop(Sea(0), Box(1), 500, new MapVector(0, 1, 0), 30);
169 var slime = Drop(Sea(0, new Liquid { Base = EngineLiquids.For("slime") }), Box(1), 500,
170 new MapVector(0, 1, 0), 30);
171
172 Assert.Equal(0.25, slime.Last!.Share, 2);
173 Assert.True(slime.Position.Y > water.Position.Y);
174 }
175
176 [Fact]
177 public void DriftsAtTheFlowsSpeedAndStopsSpinning()
178 {
179 var flow = new MapVector(1.5, 0, -0.5);
180 var (_, velocity, _) = Drop(Sea(0, flow: flow), Box(1), 500, new MapVector(0, 0.2, 0), 20);
181
182 Assert.Equal(1.5, velocity.X, 3);
183 Assert.Equal(-0.5, velocity.Z, 3);
184
185 var spun = Floating.Float(Sea(0),
186 new FloatingBody(Box(1), 500, MapVector.Zero, Orientation.Identity, MapVector.Zero,
187 new MapVector(0, 4, 0)), Gravity, Tick)!;
188 Assert.Equal(4 * Math.Exp(-2.0 * 1 * Tick), spun.Spin.Y, 9);
189 }
190
191 [Fact]
192 public void DragsALighterBodyHarderByTheLiquidItDisplaces()
193 {
194 var light = Floating.Float(Sea(10),
195 new FloatingBody(Box(1), 100, MapVector.Zero, Orientation.Identity, new MapVector(1, 0, 0),
196 MapVector.Zero), Gravity, Tick)!;
197 var heavy = Floating.Float(Sea(10),
198 new FloatingBody(Box(1), 2000, MapVector.Zero, Orientation.Identity, new MapVector(1, 0, 0),
199 MapVector.Zero), Gravity, Tick)!;
200
201 Assert.Equal(Math.Exp(-2.0 * 10 * Tick), light.Velocity.X, 9);
202 Assert.Equal(Math.Exp(-2.0 * 0.5 * Tick), heavy.Velocity.X, 9);
203 Assert.Equal(1000 * 15.24, light.Buoyancy.Y, 6);
204 }
205}