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

+137 -71
View File
@@ -2,27 +2,42 @@ using System.Numerics;
namespace GamePort.Game;
/// <summary>Independent479D20 virtual gates, not the global geometry switch.
/// Null Modes on legacy state means these ordinary defaults.</summary>
public sealed record BodyModes(bool Integration=true,bool Support=true,bool Collision=true,
bool WorldResponse=true,bool HoldPosition=false)
{
public static BodyModes Ordinary {get;} = new();
}
public readonly record struct OrdinaryBodyState(Vector3 Position, Vector3 Velocity,
Vector3 Acceleration, Vector3 Impulse, bool Supported = false, float PublishedSpeed = 0,
Vector3 AngularVelocity = default, Vector3 Rotation = default);
Vector3 AngularVelocity = default, Vector3 Rotation = default,float PositionW=1,
Vector4? PreviousRoot=null,Vector3 SupportAnchor=default,BodyModes? Modes=null)
{
public OrdinaryBodyState():this(Vector3.Zero,Vector3.Zero,Vector3.Zero,Vector3.Zero) { }
public Vector4 Root => new(Position,PositionW);
public BodyModes EffectiveModes => Modes??BodyModes.Ordinary;
}
public readonly record struct BodyLandingObservation(Vector3 Position, Vector3 Velocity,
Vector3 Acceleration, bool Supported);
public readonly record struct OrdinaryBodyStep(OrdinaryBodyState Body, Vector3 PreviousPosition,
Vector3 SupportAnchor, SupportHit? SupportHit, BodyLandingObservation? Landing,
CameraRayPolygon? Contact, int CollisionPasses, int TriangleTests);
CameraRayPolygon? Contact, int CollisionPasses, int TriangleTests,bool CandidatePublished=false);
public enum BodyResponseProfile { Jimmy, Baseball, Rocketship }
/// <summary>
/// Bounded479D20 composition: enabled integration/support/pairs/static flags,
/// prevent-lowering true, ordinary Jimmy370/374/37C and no dynamic body pass.
/// Special Player374 policies are rejected at construction. This is independent
/// of controller ownership and published speed is distinct from input speed608.
/// Euler addition follows the probe's334 fixture contract; actual generic angle
/// wrapping is not recovered. The live player bridge supplies zero angular speed.
/// See collision-body-probe.md and collision-body-composition-source.md.
/// Bounded479D20 composition with independent body modes, finite Vec4 publication,
/// ordinary/profile contacts and explicit callback ownership. Special Jimmy
/// policies and dynamic pairs remain gated; no raw-control gate belongs here.
/// Source roots/modes/anchor persist in Body; PreviousPosition in the step result
/// remains the entry-XYZ diagnostic used by the legacy dynamic adapter.
/// Angular addition retains the body probe's fixture contract; live Jimmy supplies
/// zero angular speed. Radius/friction/factor arguments are fixed for this pass.
/// See body-root-publication.md, body-modes.md and collision-body-composition-source.md.
/// </summary>
public sealed class OriginalBodyPhysics
{
@@ -51,11 +66,12 @@ public sealed class OriginalBodyPhysics
float radius = OriginalBodySupport.JimmyRadius, float factor = OriginalBodySupport.JimmyFactor,
float friction = .8f, float suppressionScale = .5f, bool geometryEnabled = true,
BodyResponseProfile profile = BodyResponseProfile.Jimmy,
Func<OrdinaryBodyState,CameraRayPolygon,OrdinaryBodyState>? wallContact = null)
Func<OrdinaryBodyState,CameraRayPolygon,OrdinaryBodyState>? wallContact = null,
Func<OrdinaryBodyState,BodyLandingObservation,OrdinaryBodyState>? landingContact = null,
Func<bool>? geometryState = null)
{
Finite(body.Position); Finite(body.Velocity); Finite(body.Acceleration); Finite(body.Impulse);
Finite(body.AngularVelocity); Finite(body.Rotation);
if (!float.IsFinite(body.PublishedSpeed) || !float.IsFinite(friction) || !float.IsFinite(suppressionScale)
Validate(body);
if (!float.IsFinite(friction) || !float.IsFinite(suppressionScale)
|| !float.IsFinite(factor)) throw new ArgumentOutOfRangeException(nameof(body));
if (!float.IsFinite(radius) || radius <= 0) throw new ArgumentOutOfRangeException(nameof(radius));
if (!float.IsFinite(seconds) || seconds <= 0 || seconds >= 1)
@@ -63,9 +79,14 @@ public sealed class OriginalBodyPhysics
var previous = body.Position;
var wasSupported = body.Supported;
body = body with { Supported = false };
if(body.EffectiveModes.Support)body = body with { Supported = false };
//479D3D's support clear precedes479D58's integration gate. Inactive
//integration preserves impulse, roots, anchor and the shared context.
if(!body.EffectiveModes.Integration)
return new(body,previous,body.SupportAnchor,null,null,null,0,0);
var geometryAtIntegration=geometryState?.Invoke()??geometryEnabled;
var anchor = new Vector3(0, -25000, 0);
if (!geometryEnabled)
if (!geometryAtIntegration)
{
//479DF7..479E5A: global geometry-off policy, not prior support.
anchor.Y = previous.Y;
@@ -77,16 +98,94 @@ public sealed class OriginalBodyPhysics
var impulse = body.Impulse;
var delta = RecoveredMotion.Integrate(ref velocity, ref impulse, body.Acceleration,
RecoveredMotion.Gravity, seconds);
var context = new StaticCollisionContext(radius);
LastStaticContext=context;
var horizontalFirst = delta.Y <= 0;
var move = collision.Move(previous, horizontalFirst ? delta with { Y = 0 } : delta,
radius, context, geometryEnabled: geometryEnabled);
var acceleration = body.Acceleration;
body=body with{Velocity=velocity,Impulse=impulse,SupportAnchor=anchor};
var collisionBranch=body.EffectiveModes.Collision;
StaticCollisionContext? context=null;
CameraRayPolygon? terminal=null;
bool published=false;
if(collisionBranch&&body.EffectiveModes.WorldResponse)
{
context=new(radius);LastStaticContext=context;
body=body with{PreviousRoot=body.Root};
var candidateW=body.PositionW;
var horizontalFirst = delta.Y <= 0;
var move=collision.Move(body.Position,horizontalFirst?delta with{Y=0}:delta,
radius,context,geometryEnabled:geometryAtIntegration);
terminal=move.CallbackPolygon;
body=Contact(body,terminal,profile);
//The dispatcher changes its candidate, not the actor's live root.
//The hook sees the live root and may change previous/live independently.
if(wallContact!=null&&terminal is{StoredNormal.Y:>-.7f}wall)body=wallContact(body,wall);
Validate(body);
if(body.PreviousRoot==body.Root)
{
var candidate=move.Position;
if(horizontalFirst)candidate.Y+=delta.Y;
candidate.Y=MathF.Max(candidate.Y,-25000);
body=body with{Position=candidate,PositionW=candidateW};published=true;
}
}
else if(collisionBranch)
{
//47A1CC: disabling world response does not disable integration,
//support/friction or initialize the static collision context.
body=body with{PreviousRoot=body.Root,Position=body.Position+delta};
}
else if(!body.EffectiveModes.HoldPosition)
{
//47A264: collision disabled translates without updating previous;
//alternate228 holds position. Both skip support and friction below.
body=body with{Position=body.Position+delta};
}
SupportHit? supportHit = null;
BodyLandingObservation? landing = null;
if(collisionBranch&&body.EffectiveModes.Support)
{
var supportPrevious=body.PreviousRoot is{} root?new Vector3(root.X,root.Y,root.Z):previous;
var support = OriginalBodySupport.Resolve(new(body.Position,supportPrevious,body.Velocity,body.Acceleration,radius,factor,
PreventLowering: profile != BodyResponseProfile.Baseball),
wasSupported, (start, end, sides) => collision.TraceSupport(start, end, sides),
geometryEnabled:geometryState?.Invoke()??geometryEnabled);
if(support.Supported)
{
body=body with{Position=support.Body.Position,Acceleration=support.Body.Acceleration,
SupportAnchor=support.SupportAnchor};
if(support.Landed)
{
landing=new(body.Position,body.Velocity,body.Acceleration,body.Supported);
if(profile==BodyResponseProfile.Baseball)
body=body with{Impulse=new(body.Velocity.X,body.Velocity.Y*-.65f,body.Velocity.Z)};
if(landingContact!=null)body=landingContact(body,landing.Value);
Validate(body);
}
//47BB83 restores cached pre-callback X/Z and zeros Y. Root,
//anchor, acceleration, impulse and mode edits survive37C.
body=body with{Velocity=support.Body.Velocity,Supported=true};
supportHit=support.Hit;
}
}
anchor=body.SupportAnchor;
if(collisionBranch&&body.PublishedSpeed==0&&body.Supported&&OriginalBodySupport.QuickMagnitude(body.Velocity)>0)
{
var v=body.Velocity;var w=body.AngularVelocity;
body=body with{Velocity=v+new Vector3(-v.X*friction*seconds,0,-v.Z*friction*seconds),
AngularVelocity=w+new Vector3(-w.X*friction*seconds,-w.Y*friction*seconds,-w.Z*friction*seconds)};
}
if(OriginalBodySupport.QuickMagnitude(body.AngularVelocity)*suppressionScale<=.1f)
body=body with{AngularVelocity=Vector3.Zero};
if(body.PublishedSpeed==0&&OriginalBodySupport.QuickMagnitude(body.Velocity)*suppressionScale<=.1f)
body=body with{Velocity=Vector3.Zero};
return new(body,previous,anchor,supportHit,landing,terminal,context?.Passes??0,context?.TriangleTests??0,published);
}
private static OrdinaryBodyState Contact(OrdinaryBodyState body,CameraRayPolygon? terminal,BodyResponseProfile profile)
{
var velocity=body.Velocity;var acceleration=body.Acceleration;
//47B80F/47B82E -> Jimmy ordinary374 or positive-Y-only370.
// Neither supported callback moves the actor root, so the source's
// post-callback previous/live-root equality test permits publication.
if (move.CallbackPolygon is { } contact && contact.StoredNormal.Y <= -.7f)
if (terminal is { } contact && contact.StoredNormal.Y <= -.7f)
{
if (profile == BodyResponseProfile.Rocketship)
velocity.Y = -120; //43EB70: unconditional; acceleration survives.
@@ -96,7 +195,7 @@ public sealed class OriginalBodyPhysics
if (acceleration.Y > 0) acceleration.Y = 0;
}
}
else if (profile == BodyResponseProfile.Baseball && move.CallbackPolygon is { } wall)
else if (profile == BodyResponseProfile.Baseball && terminal is { } wall)
{
//40FEE0 ->458890 ->472750: reflect horizontal direction, keep Y,
//then damp horizontal components by1-.46. Its angular tail does not
@@ -111,55 +210,22 @@ public sealed class OriginalBodyPhysics
reflected.Z * magnitude * (1-.46f));
}
}
var position = move.Position;
if(wallContact!=null&&move.CallbackPolygon is{StoredNormal.Y:>-.7f}terminal)
{
body=wallContact(body with{Position=position,Velocity=velocity,Acceleration=acceleration,Impulse=impulse},terminal);
position=body.Position;velocity=body.Velocity;acceleration=body.Acceleration;impulse=body.Impulse;
}
if (horizontalFirst) position.Y += delta.Y;
position.Y = MathF.Max(position.Y, -25000);
body = body with { Position = position, Velocity = velocity, Acceleration = acceleration, Impulse = impulse };
return body with{Velocity=velocity,Acceleration=acceleration};
}
SupportHit? supportHit = null;
BodyLandingObservation? landing = null;
if (geometryEnabled)
{
var support = OriginalBodySupport.Resolve(new(position, previous, velocity, acceleration, radius, factor,
PreventLowering: profile != BodyResponseProfile.Baseball),
wasSupported, (start, end, sides) => collision.TraceSupport(start, end, sides));
if (support.Supported)
{
//37C observes new root/zero acceleration but old velocity/support.
//Jimmy's inherited37C is a no-op; the observation is diagnostic.
if (support.Landed)
{
landing = new(support.Body.Position, velocity, support.Body.Acceleration, false);
//40FF80 receives old live velocity after root/acceleration
//publication and writes impulse288, before support zeros Y.
if (profile == BodyResponseProfile.Baseball)
body = body with {Impulse = new(velocity.X, velocity.Y * -.65f, velocity.Z)};
}
body = body with { Position = support.Body.Position, Velocity = support.Body.Velocity,
Acceleration = support.Body.Acceleration, Supported = true };
anchor = support.SupportAnchor;
supportHit = support.Hit;
}
}
if (body.PublishedSpeed == 0 && body.Supported && OriginalBodySupport.QuickMagnitude(body.Velocity) > 0)
{
var v = body.Velocity;
var w = body.AngularVelocity;
body = body with {
Velocity = v + new Vector3(-v.X * friction * seconds, 0, -v.Z * friction * seconds),
AngularVelocity = w + new Vector3(-w.X * friction * seconds, -w.Y * friction * seconds, -w.Z * friction * seconds)
};
}
if (OriginalBodySupport.QuickMagnitude(body.AngularVelocity) * suppressionScale <= .1f)
body = body with { AngularVelocity = Vector3.Zero };
if (body.PublishedSpeed == 0 && OriginalBodySupport.QuickMagnitude(body.Velocity) * suppressionScale <= .1f)
body = body with { Velocity = Vector3.Zero };
return new(body, previous, anchor, supportHit, landing, move.CallbackPolygon, context.Passes, context.TriangleTests);
private static void Finite(Vector4 value)
{
if(!float.IsFinite(value.X)||!float.IsFinite(value.Y)||!float.IsFinite(value.Z)||!float.IsFinite(value.W))
throw new ArgumentOutOfRangeException(nameof(value));
}
private static void Validate(OrdinaryBodyState body)
{
Finite(body.Position);Finite(body.Velocity);Finite(body.Acceleration);Finite(body.Impulse);
Finite(body.AngularVelocity);Finite(body.Rotation);Finite(body.SupportAnchor);
if(body.PreviousRoot is{} previous)Finite(previous);
if(!float.IsFinite(body.PositionW)||!float.IsFinite(body.PublishedSpeed))
throw new ArgumentOutOfRangeException(nameof(body));
}
private static void Finite(Vector3 value)
+1 -1
View File
@@ -56,7 +56,7 @@ public sealed class Scooter : IDisposable
}
public void Reset()
{
Release();body=default;Steering=default;RiderRotation=Rotation=destination=Vector3.Zero;
Release();body=new();Steering=default;RiderRotation=Rotation=destination=Vector3.Zero;
faceRate=5;poseClock=impactTime=0;ImpactActive=jumpLatch=RiderStopped=false;wheels.Reset();
BodySteps=InputSteps=FrameSteps=Contacts=Jumps=0;
}