Files
Neutron/tests/GamePort.BodyPhysicsTests/Program.cs
T

124 lines
9.2 KiB
C#

using System.Numerics;
using GamePort.Game;
using GamePort.Assets;
void Check(bool value, string message) { if (!value) throw new Exception(message); }
void Near(Vector3 actual, Vector3 expected, string message) =>
Check(Vector3.Distance(actual,expected)<.002f,$"{message}: {actual} != {expected}");
var floor=new CameraRayPolygon(new(new(-1000,0,-1000),new(1000,0,-1000),new(0,0,1000)),Vector3.UnitY);
var physics=new OriginalBodyPhysics(new([floor]));
OrdinaryBodyState Initial() => new(new(0,75.9f,0),Vector3.Zero,Vector3.Zero,Vector3.Zero);
// References observed from copied479D20/support, body-probe/wine.log.
var cases=new (string Name,OrdinaryBodyState Input,Vector3 Position,Vector3 Velocity,float AngularY,bool Supported,float? LandingY,bool Geometry)[] {
("floor_support",Initial(),new(0,75.9000015f,0),Vector3.Zero,0,true,-13.333334f,true),
("spawn_settle",Initial() with {Position=new(0,26,0)},new(0,75.9000015f,0),Vector3.Zero,0,true,-13.333334f,true),
("pending_jump",Initial() with {Supported=true,Impulse=new(0,550,0),Acceleration=new(0,-264,0)},new(0,84.7711105f,0),new(0,532.266663f,0),0,false,null,true),
("fall_above_floor",Initial() with {Position=new(0,100,0),Velocity=new(0,-200,0)},new(0,96.4444427f,0),new(0,-213.333328f,0),0,false,null,true),
("fall_land",Initial() with {Position=new(0,76,0),Velocity=new(0,-200,0)},new(0,75.9000015f,0),Vector3.Zero,0,true,-213.333328f,true),
("ground_friction",Initial() with {Velocity=new(100,0,50),AngularVelocity=new(0,12,0)},new(1.66666675f,75.9000015f,.833333373f),new(98.6666641f,0,49.3333321f),11.8400002f,true,-13.333334f,true),
("published_speed",Initial() with {Velocity=new(100,0,50),AngularVelocity=new(0,12,0),PublishedSpeed=100},new(1.66666675f,75.9000015f,.833333373f),new(100,0,50),12,true,-13.333334f,true),
("stationary_angular",Initial() with {AngularVelocity=new(0,12,0)},new(0,75.9000015f,0),Vector3.Zero,12,true,-13.333334f,true),
("small_motion",Initial() with {Velocity=new(.125f,0,0)},new(.00208333344f,75.9000015f,0),Vector3.Zero,0,true,-13.333334f,true),
("geometry_disabled",Initial() with {Velocity=new(0,200,0),Acceleration=new(0,-264,0)},new(0,75.9000015f,0),Vector3.Zero,0,true,null,false)
};
foreach(var test in cases)
{
var result=physics.Step(test.Input,1f/60,geometryEnabled:test.Geometry);
Near(result.Body.Position,test.Position,$"{test.Name} root");
Near(result.Body.Velocity,test.Velocity,$"{test.Name} velocity");
Near(result.Body.AngularVelocity,new(0,test.AngularY,0),$"{test.Name} angular velocity");
Near(result.Body.Impulse,Vector3.Zero,$"{test.Name} consumes impulse");
var expectedAcceleration=test.Geometry ? test.Supported ? Vector3.Zero : test.Input.Acceleration : new Vector3(0,800,0);
Near(result.Body.Acceleration,expectedAcceleration,$"{test.Name} acceleration");
Check(result.Body.Supported==test.Supported && result.Landing.HasValue==test.LandingY.HasValue,$"{test.Name} support/landing");
if(test.LandingY is {} vy)
{
var observation=result.Landing!.Value;
Check(!observation.Supported && MathF.Abs(observation.Velocity.Y-vy)<.002f,$"{test.Name} callback sees old velocity/support");
Check(observation.Acceleration.Y==0 && observation.Position==result.Body.Position,$"{test.Name} callback sees new root/acceleration");
}
Console.WriteLine($"PASS original ordinary-body reference {test.Name}");
}
var resting=physics.Step(Initial() with {Supported=true},1f/60);
Check(resting.Body.Supported && resting.Landing==null,"already supported must not emit landing37C again");
Console.WriteLine("PASS prior support suppresses repeated landing callback");
var empty=new OriginalBodyPhysics(new([]));
var rotation=empty.Step(Initial() with {AngularVelocity=new(0,.1f,0)},.5f);
Near(rotation.Body.Rotation,new(0,.05f,0),"rotation is integrated before small angular motion is suppressed");
Check(rotation.Body.AngularVelocity==Vector3.Zero,"small angular suppression");
var moving=empty.Step(Initial() with {Velocity=new(.125f,0,0),PublishedSpeed=1},1f/60,geometryEnabled:false);
Check(moving.Body.Velocity.X!=0,"nonzero published speed suppresses friction and small linear cutoff");
Console.WriteLine("PASS angular integration and independent published speed cutoff");
try { new OriginalBodyPhysics(new([floor with {StoredNormal=Vector3.UnitZ,MaterialId=61}]));
throw new Exception("unsupported special contacts accepted"); }
catch(NotSupportedException) { }
Console.WriteLine("PASS unsupported special-contact scene rejected");
var falling=empty.Step(Initial(),1f/60);
var rising=empty.Step(Initial() with {Impulse=new(0,550,0)},1f/60);
Check(falling.CollisionPasses==0 && rising.CollisionPasses==1,
"falling vertical motion is added after horizontal dispatcher; rising motion is dispatched whole");
Console.WriteLine("PASS positive-Y versus horizontal-first split");
var ceiling=new CameraRayPolygon(new(new(-100,100,-100),new(100,100,100),new(100,100,-100)), -Vector3.UnitY);
var roof=new OriginalBodyPhysics(new([ceiling]));
var roofHit=roof.Step(new(new(0,80,0),new(0,920,0),new(0,100,0),Vector3.Zero),1f/60,10,1);
Near(roofHit.Body.Position,new(0,90,0),"ceiling candidate published after terminal callback370");
Check(roofHit.Contact==ceiling && roofHit.Body.Velocity.Y==0 && roofHit.Body.Acceleration.Y==0,
"370 clears positive vertical velocity and acceleration without landing");
Check(!roofHit.Body.Supported && roofHit.Landing==null,"ceiling does not invent floor support");
Console.WriteLine("PASS composed ceiling370 and support ownership");
var wide=physics.Step(Initial(),1f/60,100);
Near(wide.Body.Position,new(0,126.5f,0),"live radius100 changes support offset as well as collision");
var unclamped=empty.Step(Initial() with {Velocity=new(100,0,0)},.5f,friction:4,geometryEnabled:false);
Near(unclamped.Body.Velocity,new(-100,0,0),"source friction multiplier is not clamped");
Console.WriteLine("PASS radius-dependent support and unclamped friction");
var original=Path.GetFullPath(args.Length==1?args[0]:"../Original");
var world=Mesh.LoadSceneJson("artifacts/extracted/VR04/scene.json");
var level=GamLevel.Load(AssetPath.Resolve(original,"gam/VR04.gam"));
var start=level.FindStart()!;
var standing=AseMesh.Load(AssetPath.Resolve(original,"ASE/jimstop.ase"));
var geometry=OriginalCameraRay.FromRouteScene(world,"VR04");
LabSimulation Simulation(bool retained=false) => new(new(world),standing,start.Position,start.Rotation.Y,
(a,b)=>geometry.Trace(a,b),(a,b)=>geometry.TraceGeometry(a,b),OriginalStaticCollision.FromRouteScene(world,"VR04"))
{LevelCode="VR04",InputAfterPlayerUpdate=retained};
var player=Simulation();
Check(player.UsesOriginalBody && player.PublishedSpeed==0,"tutorial explicitly selects source body");
for(var i=0;i<120;i++)player.Step(default);
Check(player.Grounded && player.Landings==0,"steady source support must not chatter");
var sole=standing.Vertices.Min(v=>Vector3.Transform(v.Position,player.Model).Y);
Check(MathF.Abs(sole-player.Position.Y)<.001f,"modern sole/root render bridge remains explicit and stable");
var rootBefore=player.ActorRoot;
player.Step(new(false,false,false,false,true));
Check(player.PendingImpulse.Y==550 && player.Grounded,"controller queues takeoff after body");
player.Step(default,false);
Check(player.PendingImpulse==Vector3.Zero && !player.Grounded && player.ActorRoot.Y>rootBefore.Y
&& player.ControllerSteps==121 && player.PhysicsSteps==122,"body consumes jump without control ownership");
Console.WriteLine("PASS tutorial root/visual bridge and control-independent pending jump");
player=Simulation();
for(var i=0;i<8;i++)player.Step(new(true,false,false,false,false));
var published=player.PublishedSpeed;
Check(published>0 && published==player.Speed,"controlled tail publishes controller speed");
player.PrepareForScript();
Check(player.Speed==0 && player.PublishedSpeed==published,"preparation preserves previously published speed160");
player.Step(default,false);
Check(player.PublishedSpeed==published,"raw control gate prevents new publication");
player.Step(default);
Check(player.PublishedSpeed==player.Speed,"resumed controlled tail publishes again");
Console.WriteLine("PASS tutorial published speed/preparation/control boundaries");
player=Simulation(true);
Check(player.ApplyRetainedLevelState(new("LV1B",3,1,.6f,4)),"retained tutorial scalar state accepted");
Check(player.PublishedSpeed==0 && player.Velocity==Vector3.Zero,"scene Init resets published body/controller motion");
for(var i=0;i<300;i++)player.Step(LabSimulation.JetpackSmokeInput(i));
Check(player.Bursts>0 && player.Travel>50 && player.MaximumRise>100 && player.Energy==20,
"retained input ordering keeps powered steering and VR04 energy exemption");
Check(player.Grounded && player.Landings>0,"source tutorial powered flight returns to support");
Console.WriteLine("PASS retained tutorial jetpack/steering/landing phases");
player=Simulation();
player.SetNoclip(true);
player.Step(new(false,false,false,false,false,Rise:true));
var physicsSteps=player.PhysicsSteps;
player.SetNoclip(false);
player.Step(default,false);
Check(player.PhysicsSteps==physicsSteps+1 && player.PublishedSpeed==0,"modern noclip exits into source body with cleared motion");
Console.WriteLine("PASS tutorial noclip returns to source body");
Console.WriteLine("Ordinary body:20 groups passed; dynamic actors and special Player contacts remain separate.");