Preserve contact-owned roots and independent body modes

This commit is contained in:
UnixNight committed 2026-11-05 11:06:13 -05:00
1 parent 3845ea7114
commit c9d0c5be59
25 files changed
+16388 -74

No files matched your search

@@ -0,0 +1,129 @@
using System.Numerics;
using System.Text.Json;
using GamePort.Game;
using var modes=JsonDocument.Parse(File.ReadAllText(Path.Combine(AppContext.BaseDirectory,"body-modes-reference.json")));
int full=0,mutations=0;
foreach(var c in modes.RootElement.GetProperty("cases").EnumerateArray())
{
var p=c.GetProperty("input");var f=c.GetProperty("flags");var o=c.GetProperty("observed");
string name=c.GetProperty("name").GetString()!;int mutation=c.GetProperty("mutation").GetInt32();
bool geometry=f.GetProperty("geometry").GetBoolean();
var initial=new OrdinaryBodyState(V3(p,"position"),V3(p,"velocity"),V3(p,"acceleration"),V3(p,"impulse"),
p.GetProperty("supported").GetBoolean(),F(p,"published_speed"),V3(p,"angular"),V3(p,"rotation"),
V4(p,"position").W,V4(p,"previous"),V3(p,"anchor"),new(
f.GetProperty("integration").GetBoolean(),f.GetProperty("support").GetBoolean(),
f.GetProperty("collision").GetBoolean(),f.GetProperty("world_response").GetBoolean(),f.GetProperty("alternate").GetBoolean()));
bool worldMutation=mutation is >=1 and <=6;
//The native mode probe substitutes a linear dispatcher. For its six
//mutation cases, a real wall supplies our callback, so clipped XYZ is
//deliberately excluded while state and branch ownership are compared.
var polygons=new List<CameraRayPolygon>{Floor()};
if(worldMutation)polygons.Add(new(new(new(61,-1000,-1000),new(61,1000,-1000),new(61,0,1000),2),-Vector3.UnitX));
var physics=new OriginalBodyPhysics(new(polygons));int contacts=0,landings=0;
var result=physics.Step(initial,F(p,"seconds"),F(p,"radius"),F(p,"factor"),F(p,"friction"),F(p,"suppression_scale"),
geometryEnabled:geometry,wallContact:(body,_)=>
{
contacts++;
var flags=body.EffectiveModes;
if(mutation is 1 or 6)flags=flags with{Support=false};
if(mutation is 2 or 6)flags=flags with{WorldResponse=false};
if(mutation is 3 or 6)flags=flags with{Collision=false};
if(mutation is 4 or 6)flags=flags with{Integration=false};
if(mutation==5)geometry=false;
return body with{Modes=flags};
},landingContact:(body,_)=>
{
landings++;
return mutation switch
{
10=>body with{Modes=body.EffectiveModes with{Support=false}},
11=>body with{Modes=body.EffectiveModes with{Collision=false}},
12=>body with{PublishedSpeed=100},
13=>body with{Modes=body.EffectiveModes with{Integration=false}},
14=>body with{Velocity=new(7,123,9)},
15=>body with{Acceleration=new(7,123,9)},
16=>body with{Position=new(77,88,99),PositionW=2},
17=>body with{Impulse=new(7,123,9)},
18=>body with{SupportAnchor=new(7,123,9)},
19=>body with{AngularVelocity=new(7,123,9)},
20=>body with{Supported=false},
_=>body
};
},geometryState:()=>geometry);
var actual=result.Body;
if(!worldMutation){Near4(actual.Root,V4(o,"position"),name+" root");full++;}
else{Check(contacts==1&&result.CandidatePublished,name+" current world branch continues after mode mutation");mutations++;}
Near4(actual.PreviousRoot??initial.Root,V4(o,"previous"),name+" previous");
Near3(actual.Velocity,V3(o,"velocity"),name+" velocity");Near3(actual.Acceleration,V3(o,"acceleration"),name+" acceleration");
Near3(actual.Impulse,V3(o,"impulse"),name+" impulse");Near3(actual.AngularVelocity,V3(o,"angular"),name+" angular");
Near3(actual.Rotation,V3(o,"rotation"),name+" rotation");Near3(actual.SupportAnchor,V3(o,"anchor"),name+" anchor");
Near3(result.SupportAnchor,actual.SupportAnchor,name+" anchor diagnostic");
Near(actual.PublishedSpeed,F(o,"speed"),name+" speed");Check(actual.Supported==(F(o,"supported")!=0),name+" supported");
Check(landings==(int)F(o,"landings")&&result.Landing.HasValue==(landings!=0),name+" landing callbacks");
var final=o.GetProperty("final_flags").EnumerateArray().Select(x=>x.GetInt32()!=0).ToArray();var flagsAfter=actual.EffectiveModes;
Check(final.SequenceEqual(new[]{flagsAfter.Integration,flagsAfter.Support,flagsAfter.WorldResponse,flagsAfter.Collision,flagsAfter.HoldPosition,geometry}),name+" final flags");
}
Check(full==81&&mutations==6,"complete mode fixture coverage");
Console.WriteLine("PASS81 copied-original full numeric body mode/landing cases;6 callback-mode ownership cases compare state with explicit real-wall candidate difference");
using var roots=JsonDocument.Parse(File.ReadAllText(Path.Combine(AppContext.BaseDirectory,"body-root-publication-reference.json")));
int rootCases=0;
foreach(var c in roots.RootElement.GetProperty("cases").EnumerateArray())
{
var p=c.GetProperty("input").EnumerateArray().Select(x=>x.GetSingle()).ToArray();var o=c.GetProperty("observed");
string name=c.GetProperty("name").GetString()!;int kind=(int)p[0];bool support=p[3]!=0;
var wall=new CameraRayPolygon(new(new(-1000,-100000,0),new(-1000,100000,0),new(100000,0,0),2),Vector3.UnitZ);
var physics=new OriginalBodyPhysics(new([wall,Floor()]));
var entry=new Vector4(0,p[1],20,1);var previous=kind==11?new Vector4(999,999,999,3):entry;
//Native17 isolation cases no-op47B890; the managed support gate isolates
//the same publication outputs. The other2 cases execute real support.
var input=new OrdinaryBodyState(new(0,p[1],20),new(0,p[2],-900),Vector3.Zero,new(0,p[4]!=0?30:0,0),
PublishedSpeed:100,PreviousRoot:previous,Modes:BodyModes.Ordinary with{Support=support});
Vector4 seen=default,seenPrevious=default;Vector3 seenVelocity=default;int contacts=0;
var result=physics.Step(input,1f/60,radius:10,wallContact:(body,_)=>
{
contacts++;seen=body.Root;seenPrevious=body.PreviousRoot!.Value;seenVelocity=body.Velocity;
return kind switch
{
1=>body with{Position=body.Position+Vector3.UnitX*100},
2=>body with{PreviousRoot=body.PreviousRoot!.Value+new Vector4(100,0,0,0)},
3=>body with{PositionW=2},4=>body with{PreviousRoot=body.PreviousRoot!.Value with{W=2}},
5=>body with{Position=new(101,-30000,303)},
6=>body with{Position=new(101,250,303),PreviousRoot=new Vector4(101,250,303,1)},
7=>body with{PositionW=2,PreviousRoot=body.PreviousRoot!.Value with{W=2}},
8=>body with{Velocity=new(9,8,7),Acceleration=new(4,5,6),Impulse=new(0,77,0)},
9=>body with{Position=new(101,5,303)},
10=>body with{Position=body.Position+Vector3.UnitX*100,PreviousRoot=body.PreviousRoot!.Value+new Vector4(100,0,0,0),Velocity=new(9,8,7),Impulse=new(0,77,0)},
_=>body
};
});
Near4(result.Body.Root,V4(o,"live"),name+" live");Near4(result.Body.PreviousRoot!.Value,V4(o,"previous"),name+" previous");
Near3(result.Body.Velocity,V3(o,"velocity"),name+" velocity");Near3(result.Body.Acceleration,V3(o,"acceleration"),name+" acceleration");
Near3(result.Body.Impulse,V3(o,"impulse"),name+" impulse");Near4(seen,V4(o,"seen"),name+" callback sees live root");
Near4(seenPrevious,V4(o,"seenPrevious"),name+" callback sees entry previous");Near3(seenVelocity,V3(o,"seenVelocity"),name+" callback velocity");
var flags=o.GetProperty("flags").EnumerateArray().Select(x=>x.GetSingle()).ToArray();
Check(contacts==flags[0]&&result.CandidatePublished==(flags[1]!=0)&&result.Body.Supported==(flags[5]!=0)
&&result.Landing.HasValue==(flags[6]!=0),name+" publication/landing");rootCases++;
}
Check(rootCases==19,"complete native root fixture");
Console.WriteLine("PASS19 copied-original live/previous Vec4 publication cases, callback visibility, clamping and real support after root changes");
//A disabled world/body pass preserves the context needed by a later attached query.
var empty=new OriginalBodyPhysics(new([]));var state=new OrdinaryBodyState(new(0,100,0),Vector3.One,Vector3.Zero,Vector3.Zero);
var active=empty.Step(state,1f/60);var saved=empty.LastStaticContext;
foreach(var flags in new[]{BodyModes.Ordinary with{WorldResponse=false},BodyModes.Ordinary with{Collision=false},BodyModes.Ordinary with{Integration=false}})
{
var inactive=empty.Step(active.Body with{Modes=flags},1f/60);
Check(ReferenceEquals(saved,empty.LastStaticContext)&&inactive.CollisionPasses==0&&inactive.TriangleTests==0,"inactive path retains context without traversing stale geometry");
}
Console.WriteLine("PASS independent disabled gates preserve the prior static context");
static CameraRayPolygon Floor()=>new(new(new(-1000,0,-1000),new(1000,0,-1000),new(0,0,1000),1),Vector3.UnitY);
static float F(JsonElement e,string key)=>e.GetProperty(key).GetSingle();
static Vector3 V3(JsonElement e,string key){var a=e.GetProperty(key).EnumerateArray().Select(x=>x.GetSingle()).ToArray();return new(a[0],a[1],a[2]);}
static Vector4 V4(JsonElement e,string key){var a=e.GetProperty(key).EnumerateArray().Select(x=>x.GetSingle()).ToArray();return new(a[0],a[1],a[2],a[3]);}
static void Near(float a,float b,string name){if(!float.IsFinite(a)||MathF.Abs(a-b)>.004f)throw new Exception($"{name}: {a} != {b}");}
static void Near3(Vector3 a,Vector3 b,string name){Near(a.X,b.X,name+" X");Near(a.Y,b.Y,name+" Y");Near(a.Z,b.Z,name+" Z");}
static void Near4(Vector4 a,Vector4 b,string name){Near3(new(a.X,a.Y,a.Z),new(b.X,b.Y,b.Z),name);Near(a.W,b.W,name+" W");}
static void Check(bool value,string name){if(!value)throw new Exception(name);}