using System.Numerics; using EvilEngine.Api.Physics; using EvilEngine.Formats.Collision; using EvilEngine.Formats.Maps; using EvilEngine.Formats.Records; using EvilEngine.Host.Liquids; using EvilEngine.Host.Physics; namespace EvilEngine.Host.Tests; public sealed class FloatingTests { private const double Tick = 0.015; private static readonly MapVector Gravity = new(0, -15.24, 0); private static readonly Vector3[] Cube = [ new(-1, -1, -1), new(1, -1, -1), new(-1, 1, -1), new(1, 1, -1), new(-1, -1, 1), new(1, -1, 1), new(-1, 1, 1), new(1, 1, 1) ]; private static BodyShape Box(double size) { var half = (float)(size / 2); return BodyShape.Of(new CollisionContent([ new Collider(new ColliderShape.Box(new Vector3(2 * half)), ["solid"]) ])); } private static LiquidSpace Sea(double height, Liquid? liquid = null, MapVector? flow = null) { var space = new LiquidSpace(_ => null, _ => { }); space.Add(new LiquidBody(null, liquid ?? new Liquid { Base = EngineLiquids.For("water") }, new LiquidShape.Level(height)) { Flow = flow }); return space; } private static (MapVector Position, MapVector Velocity, Floated? Last) Drop(LiquidSpace sea, BodyShape shape, double mass, MapVector start, double seconds, Orientation? orientation = null) { var (position, velocity, spin) = (start, MapVector.Zero, MapVector.Zero); Floated? last = null; for (var tick = 0; tick < seconds / Tick; tick++) { var body = new FloatingBody(shape, mass, position, orientation ?? Orientation.Identity, velocity, spin); last = Floating.Float(sea, body, Gravity, Tick); if (last is not null) (velocity, spin) = (last.Velocity, last.Spin); var push = last?.Buoyancy ?? MapVector.Zero; velocity = new MapVector(velocity.X + (Gravity.X + push.X / mass) * Tick, velocity.Y + (Gravity.Y + push.Y / mass) * Tick, velocity.Z + (Gravity.Z + push.Z / mass) * Tick); position = new MapVector(position.X + velocity.X * Tick, position.Y + velocity.Y * Tick, position.Z + velocity.Z * Tick); } return (position, velocity, last); } [Fact] public void MeasuresEveryShapesVolume() { Assert.Equal(8, Box(2).Volume, 9); Assert.Equal(8, BodyShape.Of(new CollisionContent([ new Collider(new ColliderShape.Hull(Cube), ["solid"]) ])).Volume, 9); var sphere = BodyShape.Of(new CollisionContent([new Collider(new ColliderShape.Sphere(0.5f), ["solid"])])); Assert.Equal(4.0 / 3 * Math.PI * 0.125, sphere.Volume, 9); Assert.InRange(sphere.Parts[0].MeshVolume / sphere.Volume, 0.8, 1); var capsule = BodyShape.Of(new CollisionContent([new Collider(new ColliderShape.Capsule(1, 0.25f), ["solid"])])); Assert.Equal(Math.PI * 0.0625 * (1 + 1.0 / 3), capsule.Volume, 9); var cylinder = BodyShape.Of(new CollisionContent([new Collider(new ColliderShape.Cylinder(2, 0.5f), ["solid"])])); Assert.Equal(Math.PI * 0.25 * 2, cylinder.Volume, 9); Assert.InRange(cylinder.Parts[0].MeshVolume / cylinder.Volume, 0.95, 1); } [Fact] public void KeepsAColliderWhereItsPlacedAndLeavesTriangleMeshesOut() { var shape = BodyShape.Of(new CollisionContent([ new Collider(new ColliderShape.Box(Vector3.One), ["solid"]) { Position = new Vector3(0, 5, 0) }, new Collider(new ColliderShape.Mesh([ new MeshPart([Vector3.Zero, Vector3.UnitX, Vector3.UnitZ], [0, 1, 2], CollisionSurface.None) ]), ["solid"]) ])); Assert.False(shape.Convex); Assert.Equal(1, shape.Volume, 9); Assert.Equal(new MapVector(-0.5, 4.5, -0.5), shape.Bounds(MapVector.Zero, Orientation.Identity).Min); Assert.Equal(4.5, shape.Lowest(Orientation.Identity, new MapVector(0, 1, 0)), 9); } [Fact] public void TakesMassFromTheCollisionThenTheSurfacesDensityThenWaters() { var shape = Box(2); var collision = new CollisionContent([]); Assert.Equal(30, BodyShape.Mass(new BodySpec(collision with { Motion = new CollisionMotion { Mass = 30 } }), shape)); Assert.Equal(12, BodyShape.Mass(new BodySpec(collision) { Mass = 12 }, shape)); Assert.Equal(8 * 700, BodyShape.Mass(new BodySpec(collision) { Surface = Formats.ContentId.Parse("a:wood") }, shape, _ => 700)); Assert.Equal(8 * BodyShape.DefaultDensity, BodyShape.Mass(new BodySpec(collision), shape)); } [Fact] public void ClipsABodyAtTheSurfaceForTheVolumeUnderAndItsCentre() { var up = new MapVector(0, 1, 0); var level = Floating.Submerged(Box(2), MapVector.Zero, Orientation.Identity, MapVector.Zero, up); Assert.Equal(4, level.Volume, 9); Assert.Equal(-0.5, level.Centre.Y, 9); Assert.Equal(0, level.Centre.X, 9); var turned = Floating.Submerged(Box(2), MapVector.Zero, Orientation.Axis(new MapVector(0, 0, 1), Math.PI / 4), MapVector.Zero, up); Assert.Equal(4, turned.Volume, 9); var shallow = Floating.Submerged(Box(2), new MapVector(0, 0.5, 0), Orientation.Identity, MapVector.Zero, up); Assert.Equal(2, shallow.Volume, 9); Assert.Equal(Box(2).Volume, Floating.Submerged(Box(2), new MapVector(3, -9, 1), Orientation.Identity, MapVector.Zero, up).Volume, 9); Assert.Equal(0, Floating.Submerged(Box(2), new MapVector(0, 1.5, 0), Orientation.Identity, MapVector.Zero, up).Volume, 9); } [Fact] public void ATiltedBodyIsPushedUpOffItsMiddle() { var tilted = Floating.Submerged(Box(2), MapVector.Zero, Orientation.Axis(new MapVector(0, 0, 1), 0.3), new MapVector(0, 0.3, 0), new MapVector(0, 1, 0)); Assert.NotEqual(0, tilted.Centre.X, 3); } [Theory] [InlineData(250, 0.25)] [InlineData(500, 0.5)] [InlineData(800, 0.8)] public void FloatsWithItsShareUnderAsItsDensityOverWaters(double density, double share) { var (position, velocity, last) = Drop(Sea(0), Box(1), density, new MapVector(0, 1, 0), 30); Assert.NotNull(last); Assert.Equal(share, last.Share, 2); Assert.Equal(0.5 - share, position.Y, 2); Assert.Equal(0, velocity.Y, 2); } [Fact] public void ABodyDenserThanTheLiquidSinksAndOneOutOfItIsLeftAlone() { var (position, velocity, last) = Drop(Sea(0), Box(1), 2000, new MapVector(0, -1, 0), 2); Assert.True(position.Y < -5); Assert.True(velocity.Y < 0); Assert.Equal(1, last!.Share, 9); Assert.Null(Floating.Float(Sea(0), new FloatingBody(Box(1), 500, new MapVector(0, 3, 0), Orientation.Identity, MapVector.Zero, MapVector.Zero), Gravity, Tick)); } [Fact] public void FloatsHigherInADenserLiquid() { var water = Drop(Sea(0), Box(1), 500, new MapVector(0, 1, 0), 30); var slime = Drop(Sea(0, new Liquid { Base = EngineLiquids.For("slime") }), Box(1), 500, new MapVector(0, 1, 0), 30); Assert.Equal(0.25, slime.Last!.Share, 2); Assert.True(slime.Position.Y > water.Position.Y); } [Fact] public void DriftsAtTheFlowsSpeedAndStopsSpinning() { var flow = new MapVector(1.5, 0, -0.5); var (_, velocity, _) = Drop(Sea(0, flow: flow), Box(1), 500, new MapVector(0, 0.2, 0), 20); Assert.Equal(1.5, velocity.X, 3); Assert.Equal(-0.5, velocity.Z, 3); var spun = Floating.Float(Sea(0), new FloatingBody(Box(1), 500, MapVector.Zero, Orientation.Identity, MapVector.Zero, new MapVector(0, 4, 0)), Gravity, Tick)!; Assert.Equal(4 * Math.Exp(-2.0 * 1 * Tick), spun.Spin.Y, 9); } [Fact] public void DragsALighterBodyHarderByTheLiquidItDisplaces() { var light = Floating.Float(Sea(10), new FloatingBody(Box(1), 100, MapVector.Zero, Orientation.Identity, new MapVector(1, 0, 0), MapVector.Zero), Gravity, Tick)!; var heavy = Floating.Float(Sea(10), new FloatingBody(Box(1), 2000, MapVector.Zero, Orientation.Identity, new MapVector(1, 0, 0), MapVector.Zero), Gravity, Tick)!; Assert.Equal(Math.Exp(-2.0 * 10 * Tick), light.Velocity.X, 9); Assert.Equal(Math.Exp(-2.0 * 0.5 * Tick), heavy.Velocity.X, 9); Assert.Equal(1000 * 15.24, light.Buoyancy.Y, 6); } }