Match original collision rounding through populated dynamic reclip
This commit is contained in:
1 parent
135ff3642a
commit
bf876e9c3c
22 files changed
+701
-14
No files matched your search
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType><TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings><Nullable>enable</Nullable>
|
||||
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="../../src/GamePort/Assets/*.cs" />
|
||||
<Compile Include="../../src/GamePort/Game/*.cs" />
|
||||
<None Include="expected.json" CopyToOutputDirectory="PreserveNewest" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,153 @@
|
||||
using System.Numerics;
|
||||
using System.Text.Json;
|
||||
using GamePort.Game;
|
||||
|
||||
// Synthetic actor composition, not live scene admission. All roots are points;
|
||||
// callbacks mutate roots only, primitive flags remain constant during each query.
|
||||
var fixture = new Fixture();
|
||||
var actual = new List<JsonElement>();
|
||||
void Query(string name) => actual.Add(fixture.Query(name));
|
||||
fixture.Reset(); fixture.Wall(0,35); Query("nearer_wall");
|
||||
fixture.Reset(); fixture.Wall(0,10); Query("equal_dynamic_face");
|
||||
fixture.Reset(); fixture.Wall(0,0); Query("far_wall");
|
||||
fixture.Reset(); fixture.Wall(0,35); fixture.Wall(1,30); Query("two_near_first");
|
||||
fixture.Reset(); fixture.Wall(0,30); fixture.Wall(1,35); Query("two_far_first");
|
||||
fixture.Reset(); fixture.Wall(0,35); fixture.Mutate = true; Query("nearer_callback_root");
|
||||
fixture.Reset(); fixture.Wall(0,35); fixture.Inhibit = true; Query("inhibit_static_visit");
|
||||
fixture.Reset(); fixture.Wall(0,35); fixture.Geometry = false; Query("geometry_off_visits");
|
||||
fixture.Reset(); fixture.Wall(0,35); fixture.Force = true; Query("forced_primitive_mode");
|
||||
fixture.Reset(); fixture.Wall(0,0); fixture.CeilingSeed = true; Query("stale_ceiling_callback");
|
||||
fixture.Reset(); fixture.Wall(0,0); fixture.Root.Y = 30;
|
||||
fixture.Ceiling(35,true); Query("ceiling_response");
|
||||
fixture.Reset(); fixture.Wall(0,35); fixture.Previous.Y = -30;
|
||||
fixture.Ceiling(); Query("wall_and_ceiling");
|
||||
fixture.Reset(); fixture.Wall(0,35); Query("retained_first");
|
||||
fixture.Previous = new(0,0,50); fixture.Root = new(0,0,15,1); Query("retained_second");
|
||||
|
||||
if(args is ["--dump"])
|
||||
{
|
||||
Console.WriteLine(JsonSerializer.Serialize(new {cases = actual,diagnostic_steps = fixture.DiagnosticSteps}));
|
||||
return;
|
||||
}
|
||||
var native = args is ["--native", _];
|
||||
if(args.Length != 0 && !native) throw new ArgumentException("Use --native <observations.json>");
|
||||
using var expected = JsonDocument.Parse(File.ReadAllText(native ? args[1]
|
||||
: Path.Combine(AppContext.BaseDirectory,"expected.json")));
|
||||
var cases = expected.RootElement.GetProperty("cases").EnumerateArray().ToArray();
|
||||
if(cases.Length != actual.Count) throw new Exception("Case count differs");
|
||||
for(var i = 0; i < actual.Count; i++) Compare(cases[i],actual[i],cases[i].GetProperty("name").GetString()!);
|
||||
Compare(expected.RootElement.GetProperty("diagnostic_steps"),
|
||||
JsonSerializer.SerializeToElement(fixture.DiagnosticSteps),"diagnostic_steps");
|
||||
Console.WriteLine($"PASS {actual.Count} populated-grid dynamic/static comparisons and 12 kernel steps; exact float32, callback order and primitive mode.");
|
||||
|
||||
static void Compare(JsonElement expected,JsonElement actual,string path)
|
||||
{
|
||||
if(expected.ValueKind != actual.ValueKind) throw new Exception($"{path}: value kind differs");
|
||||
if(expected.ValueKind == JsonValueKind.Object)
|
||||
{
|
||||
if(expected.EnumerateObject().Count() != actual.EnumerateObject().Count()) throw new Exception($"{path}: fields differ");
|
||||
foreach(var p in expected.EnumerateObject()) Compare(p.Value,actual.GetProperty(p.Name),path+"."+p.Name);
|
||||
}
|
||||
else if(expected.ValueKind == JsonValueKind.Array)
|
||||
{
|
||||
if(expected.GetArrayLength() != actual.GetArrayLength()) throw new Exception($"{path}: array length differs");
|
||||
for(var i = 0; i < expected.GetArrayLength(); i++) Compare(expected[i],actual[i],$"{path}[{i}]");
|
||||
}
|
||||
// Native %.9g and managed shortest-roundtrip text can spell the same float
|
||||
// differently. Compare exact parsed Single values, without an epsilon.
|
||||
else if(expected.ValueKind == JsonValueKind.Number)
|
||||
{
|
||||
if(expected.GetSingle() != actual.GetSingle()) throw new Exception($"{path}: {actual} != original {expected}");
|
||||
}
|
||||
else if(!JsonElement.DeepEquals(expected,actual)) throw new Exception($"{path}: {actual} != original {expected}");
|
||||
}
|
||||
|
||||
sealed class Fixture
|
||||
{
|
||||
public Vector3 Previous;
|
||||
public Vector4 Root;
|
||||
public bool Mutate,Inhibit,Geometry,Force,CeilingSeed;
|
||||
private StaticCollisionContext context = null!;
|
||||
private readonly List<CameraRayPolygon> polygons = [];
|
||||
private readonly List<string> events = [];
|
||||
private int dynamicCallbacks,staticCallbacks,setters,kernelCalls;
|
||||
private CameraRayPolygon seed;
|
||||
public List<JsonElement> DiagnosticSteps { get; } = [];
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
Previous = new(0,0,50); Root = new(0,0,15,1);
|
||||
Mutate = Inhibit = Force = CeilingSeed = false; Geometry = true;
|
||||
context = new(88,77){Nearest = new(77,88,new(111,222,333))};
|
||||
seed = new(new(new(-100,-100,0),new(-100,100,0),new(100,100,0),99,0),Vector3.UnitZ);
|
||||
context.LastPolygon = seed;
|
||||
polygons.Clear(); events.Clear();
|
||||
dynamicCallbacks = staticCallbacks = setters = kernelCalls = 0;
|
||||
}
|
||||
public void Wall(int id,float z) => polygons.Add(new(new(new(-100,-100,z),new(-100,100,z),
|
||||
new(100,100,z),0,id),Vector3.UnitZ));
|
||||
public void Ceiling(float y = 5,bool positiveZ = false) => polygons.Add(new(new(new(-100,y,-100),
|
||||
positiveZ ? new(-100,y,100) : new(100,y,100),
|
||||
positiveZ ? new(100,y,100) : new(100,y,-100),0,1),-Vector3.UnitY));
|
||||
private int PolygonId(CameraRayPolygon? p) => p is null ? -1 : p == seed ? 99 : p.Value.Triangle.PolygonId;
|
||||
private void Notify(Vector3 normal,CameraRayPolygon? polygon,int slot)
|
||||
{
|
||||
events.Add($"{slot:x}:{PolygonId(polygon)}:0");
|
||||
if(polygon is null) dynamicCallbacks++; else staticCallbacks++;
|
||||
if(Mutate) Root = new(777,888,999,2);
|
||||
}
|
||||
public JsonElement Query(string name)
|
||||
{
|
||||
if(CeilingSeed)
|
||||
{
|
||||
seed = seed with { StoredNormal = -Vector3.UnitY };
|
||||
context.LastPolygon = seed;
|
||||
}
|
||||
var current = new Vector3(Root.X,Root.Y,Root.Z);
|
||||
var candidate = current;
|
||||
context.Nearest = context.Nearest with {FaceDistance = -1,EdgeDistance = 10};
|
||||
var box = new DynamicBoxCollider(1,Vector3.Zero,new(-50,-50,-10),new(50,50,10),Matrix4x4.Identity,40);
|
||||
foreach(var triangle in DynamicSceneCollision.Triangles(box))
|
||||
{
|
||||
var cross = Vector3.Cross(triangle.B-triangle.A,triangle.C-triangle.A);
|
||||
var normal = cross/OriginalBodySupport.QuickMagnitude(cross);
|
||||
kernelCalls++;
|
||||
var input = candidate;
|
||||
var hit = OriginalSphereResponse.Resolve(new(triangle.A,triangle.C,triangle.B),normal,
|
||||
Previous,candidate,10,context.Nearest);
|
||||
if(name == "wall_and_ceiling")
|
||||
{
|
||||
var h = hit.State.Hit!.Value;
|
||||
DiagnosticSteps.Add(JsonSerializer.SerializeToElement(new {
|
||||
index = DiagnosticSteps.Count,input = new[]{input.X,input.Y,input.Z,1},
|
||||
output = new[]{hit.Position.X,hit.Position.Y,hit.Position.Z,1},
|
||||
contact = hit.Contact == SphereContact.None ? 0 : 1,face = hit.State.FaceDistance,
|
||||
edge = hit.State.EdgeDistance,hit = new[]{h.X,h.Y,h.Z,1}
|
||||
}));
|
||||
}
|
||||
if(hit.Contact == SphereContact.None) continue;
|
||||
candidate = hit.Position; context.Nearest = hit.State;
|
||||
Notify(normal,null,0x374);
|
||||
}
|
||||
var contact = context.Nearest.FaceDistance != -1 || context.Nearest.EdgeDistance < 10;
|
||||
if(contact && !Inhibit)
|
||||
{
|
||||
var world = new OriginalStaticCollision(polygons);
|
||||
var clipped = world.Move(Previous,candidate-Previous,10,context,edges:false,
|
||||
lowerEdges:Force,geometryEnabled:Geometry);
|
||||
if(clipped.CallbackPolygon is {} polygon)
|
||||
Notify(polygon.StoredNormal,polygon,polygon.StoredNormal.Y > -.7f ? 0x374 : 0x370);
|
||||
events.Add("314:-1:0"); Root = new(clipped.Position,1); setters++;
|
||||
}
|
||||
var p = context.Nearest.Hit!.Value;
|
||||
return JsonSerializer.SerializeToElement(new {
|
||||
name,contact = contact ? 1 : 0,root = new[]{Root.X,Root.Y,Root.Z,Root.W},
|
||||
face = context.Nearest.FaceDistance,edge = context.Nearest.EdgeDistance,
|
||||
hit = new[]{p.X,p.Y,p.Z,1},last = PolygonId(context.LastPolygon),
|
||||
dynamicCallbacks,staticCallbacks,setters,passes = context.Passes,
|
||||
visits = context.NodeVisits,mode = 0,
|
||||
kernelZero = kernelCalls+(Force ? 0 : context.TriangleTests),
|
||||
kernelLower = Force ? context.TriangleTests : 0,events = string.Join(',',events)
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"cases": [
|
||||
{"name":"nearer_wall","contact":1,"root":[0,0,45,1],"face":15,"edge":10,"hit":[0,0,35,1],"last":0,"dynamicCallbacks":1,"staticCallbacks":1,"setters":1,"passes":2,"visits":4,"mode":0,"kernelZero":16,"kernelLower":0,"events":"374:-1:0,374:0:0,314:-1:0"},
|
||||
{"name":"equal_dynamic_face","contact":1,"root":[0,0,20,1],"face":40,"edge":10,"hit":[0,0,10,1],"last":99,"dynamicCallbacks":1,"staticCallbacks":1,"setters":1,"passes":1,"visits":2,"mode":0,"kernelZero":14,"kernelLower":0,"events":"374:-1:0,374:99:0,314:-1:0"},
|
||||
{"name":"far_wall","contact":1,"root":[0,0,20,1],"face":40,"edge":10,"hit":[0,0,10,1],"last":99,"dynamicCallbacks":1,"staticCallbacks":1,"setters":1,"passes":1,"visits":2,"mode":0,"kernelZero":14,"kernelLower":0,"events":"374:-1:0,374:99:0,314:-1:0"},
|
||||
{"name":"two_near_first","contact":1,"root":[0,0,45,1],"face":15,"edge":10,"hit":[0,0,35,1],"last":0,"dynamicCallbacks":1,"staticCallbacks":1,"setters":1,"passes":2,"visits":8,"mode":0,"kernelZero":20,"kernelLower":0,"events":"374:-1:0,374:0:0,314:-1:0"},
|
||||
{"name":"two_far_first","contact":1,"root":[0,0,45,1],"face":15,"edge":10,"hit":[0,0,35,1],"last":1,"dynamicCallbacks":1,"staticCallbacks":1,"setters":1,"passes":2,"visits":8,"mode":0,"kernelZero":20,"kernelLower":0,"events":"374:-1:0,374:1:0,314:-1:0"},
|
||||
{"name":"nearer_callback_root","contact":1,"root":[0,0,45,1],"face":15,"edge":10,"hit":[0,0,35,1],"last":0,"dynamicCallbacks":1,"staticCallbacks":1,"setters":1,"passes":2,"visits":4,"mode":0,"kernelZero":16,"kernelLower":0,"events":"374:-1:0,374:0:0,314:-1:0"},
|
||||
{"name":"inhibit_static_visit","contact":1,"root":[0,0,15,1],"face":40,"edge":10,"hit":[0,0,10,1],"last":99,"dynamicCallbacks":1,"staticCallbacks":0,"setters":0,"passes":0,"visits":0,"mode":0,"kernelZero":12,"kernelLower":0,"events":"374:-1:0"},
|
||||
{"name":"geometry_off_visits","contact":1,"root":[0,0,20,1],"face":40,"edge":10,"hit":[0,0,10,1],"last":99,"dynamicCallbacks":1,"staticCallbacks":1,"setters":1,"passes":1,"visits":2,"mode":0,"kernelZero":12,"kernelLower":0,"events":"374:-1:0,374:99:0,314:-1:0"},
|
||||
{"name":"forced_primitive_mode","contact":1,"root":[0,0,45,1],"face":15,"edge":10,"hit":[0,0,35,1],"last":0,"dynamicCallbacks":1,"staticCallbacks":1,"setters":1,"passes":2,"visits":4,"mode":0,"kernelZero":12,"kernelLower":4,"events":"374:-1:0,374:0:0,314:-1:0"},
|
||||
{"name":"stale_ceiling_callback","contact":1,"root":[0,0,20,1],"face":40,"edge":10,"hit":[0,0,10,1],"last":99,"dynamicCallbacks":1,"staticCallbacks":1,"setters":1,"passes":1,"visits":2,"mode":0,"kernelZero":14,"kernelLower":0,"events":"374:-1:0,370:99:0,314:-1:0"},
|
||||
{"name":"ceiling_response","contact":1,"root":[0,25,23.7499962,1],"face":43.7501259,"edge":10,"hit":[0,35,23.7499962,1],"last":1,"dynamicCallbacks":1,"staticCallbacks":1,"setters":1,"passes":2,"visits":12,"mode":0,"kernelZero":24,"kernelLower":0,"events":"374:-1:0,370:1:0,314:-1:0"},
|
||||
{"name":"wall_and_ceiling","contact":1,"root":[-8.68490019e-07,0,20.0000038,1],"face":50.0001411,"edge":5,"hit":[-8.68490019e-07,-5.48603339e-07,10.0000029,1],"last":99,"dynamicCallbacks":2,"staticCallbacks":1,"setters":1,"passes":1,"visits":6,"mode":0,"kernelZero":18,"kernelLower":0,"events":"374:-1:0,374:-1:0,374:99:0,314:-1:0"},
|
||||
{"name":"retained_first","contact":1,"root":[0,0,45,1],"face":15,"edge":10,"hit":[0,0,35,1],"last":0,"dynamicCallbacks":1,"staticCallbacks":1,"setters":1,"passes":2,"visits":4,"mode":0,"kernelZero":16,"kernelLower":0,"events":"374:-1:0,374:0:0,314:-1:0"},
|
||||
{"name":"retained_second","contact":1,"root":[0,0,45,1],"face":15,"edge":10,"hit":[0,0,35,1],"last":0,"dynamicCallbacks":2,"staticCallbacks":2,"setters":2,"passes":4,"visits":8,"mode":0,"kernelZero":32,"kernelLower":0,"events":"374:-1:0,374:0:0,314:-1:0,374:-1:0,374:0:0,314:-1:0"}
|
||||
],
|
||||
"diagnostic_steps": [
|
||||
{"index":0,"input":[0,0,15,1],"output":[0,0,15,1],"contact":0,"face":-1,"edge":10,"hit":[111,222,333,1]},
|
||||
{"index":1,"input":[0,0,15,1],"output":[0,0,15,1],"contact":0,"face":-1,"edge":10,"hit":[111,222,333,1]},
|
||||
{"index":2,"input":[0,0,15,1],"output":[0,0,15,1],"contact":0,"face":-1,"edge":10,"hit":[111,222,333,1]},
|
||||
{"index":3,"input":[0,0,15,1],"output":[0,0,15,1],"contact":0,"face":-1,"edge":10,"hit":[111,222,333,1]},
|
||||
{"index":4,"input":[0,0,15,1],"output":[0,0,15,1],"contact":0,"face":-1,"edge":10,"hit":[111,222,333,1]},
|
||||
{"index":5,"input":[0,0,15,1],"output":[0,0,15,1],"contact":0,"face":-1,"edge":10,"hit":[111,222,333,1]},
|
||||
{"index":6,"input":[0,0,15,1],"output":[-8.68490133e-07,0,20,1],"contact":1,"face":-1,"edge":5,"hit":[8.68490133e-07,0,10,1]},
|
||||
{"index":7,"input":[-8.68490133e-07,0,20,1],"output":[-8.68490019e-07,0,20.0000038,1],"contact":1,"face":50.0001411,"edge":5,"hit":[-8.68490019e-07,-5.48603339e-07,10.0000029,1]},
|
||||
{"index":8,"input":[-8.68490019e-07,0,20.0000038,1],"output":[-8.68490019e-07,0,20.0000038,1],"contact":0,"face":50.0001411,"edge":5,"hit":[-8.68490019e-07,-5.48603339e-07,10.0000029,1]},
|
||||
{"index":9,"input":[-8.68490019e-07,0,20.0000038,1],"output":[-8.68490019e-07,0,20.0000038,1],"contact":0,"face":50.0001411,"edge":5,"hit":[-8.68490019e-07,-5.48603339e-07,10.0000029,1]},
|
||||
{"index":10,"input":[-8.68490019e-07,0,20.0000038,1],"output":[-8.68490019e-07,0,20.0000038,1],"contact":0,"face":50.0001411,"edge":5,"hit":[-8.68490019e-07,-5.48603339e-07,10.0000029,1]},
|
||||
{"index":11,"input":[-8.68490019e-07,0,20.0000038,1],"output":[-8.68490019e-07,0,20.0000038,1],"contact":0,"face":50.0001411,"edge":5,"hit":[-8.68490019e-07,-5.48603339e-07,10.0000029,1]}
|
||||
]
|
||||
}
|
||||
Reference in new issue
Block a user