237 lines
12 KiB
C#
237 lines
12 KiB
C#
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,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,bool CandidatePublished=false);
|
|
|
|
public enum BodyResponseProfile { Jimmy, Baseball, Rocketship }
|
|
|
|
/// <summary>
|
|
/// 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
|
|
{
|
|
private readonly OriginalStaticCollision collision;
|
|
public StaticCollisionContext? LastStaticContext {get;private set;}
|
|
|
|
public OriginalBodyPhysics(OriginalStaticCollision collision, bool allowSpecialMaterials = false)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(collision);
|
|
if (collision.HasSpecialPlayerContacts && !allowSpecialMaterials)
|
|
throw new NotSupportedException("Ordinary body composition requires recovered special-contact policies for this scene");
|
|
this.collision = collision;
|
|
}
|
|
|
|
public int? MaterialFor(SupportHit hit) => collision.MaterialFor(hit);
|
|
|
|
/// <summary>Attached47B4B0 inherits the sphere context from physics. Support
|
|
/// and ray queries neither clear last polygon nor reset nearest distances.</summary>
|
|
public StaticCollisionMove MoveAttached(Vector3 root,Vector3 desired,float radius)
|
|
{
|
|
if(LastStaticContext==null)throw new InvalidOperationException("Attached movement requires the preceding body phase");
|
|
return collision.Move(root,desired-root,radius,LastStaticContext);
|
|
}
|
|
|
|
public OrdinaryBodyStep Step(OrdinaryBodyState body, float seconds,
|
|
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,BodyLandingObservation,OrdinaryBodyState>? landingContact = null,
|
|
Func<bool>? geometryState = null)
|
|
{
|
|
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)
|
|
throw new ArgumentOutOfRangeException(nameof(seconds));
|
|
|
|
var previous = body.Position;
|
|
var wasSupported = body.Supported;
|
|
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 (!geometryAtIntegration)
|
|
{
|
|
//479DF7..479E5A: global geometry-off policy, not prior support.
|
|
anchor.Y = previous.Y;
|
|
body = body with { Supported = true, Acceleration = new(0, -RecoveredMotion.Gravity.Y, 0),
|
|
Velocity = body.Velocity with { Y = 0 } };
|
|
}
|
|
body = body with { Rotation = body.Rotation + body.AngularVelocity * seconds };
|
|
var velocity = body.Velocity;
|
|
var impulse = body.Impulse;
|
|
var delta = RecoveredMotion.Integrate(ref velocity, ref impulse, body.Acceleration,
|
|
RecoveredMotion.Gravity, seconds);
|
|
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 (terminal is { } contact && contact.StoredNormal.Y <= -.7f)
|
|
{
|
|
if (profile == BodyResponseProfile.Rocketship)
|
|
velocity.Y = -120; //43EB70: unconditional; acceleration survives.
|
|
else
|
|
{
|
|
if (velocity.Y > 0) velocity.Y = 0;
|
|
if (acceleration.Y > 0) acceleration.Y = 0;
|
|
}
|
|
}
|
|
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
|
|
//change position/velocity; sprite/Euler side effects remain separate.
|
|
var horizontal = velocity with {Y=0};
|
|
var magnitude = OriginalBodySupport.QuickMagnitude(horizontal);
|
|
if (magnitude > 0)
|
|
{
|
|
var direction = horizontal / magnitude;
|
|
var reflected = direction - 2 * Vector3.Dot(direction, wall.StoredNormal) * wall.StoredNormal;
|
|
velocity = new(reflected.X * magnitude * (1-.46f), velocity.Y,
|
|
reflected.Z * magnitude * (1-.46f));
|
|
}
|
|
}
|
|
return body with{Velocity=velocity,Acceleration=acceleration};
|
|
}
|
|
|
|
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)
|
|
{
|
|
if (!float.IsFinite(value.X) || !float.IsFinite(value.Y) || !float.IsFinite(value.Z))
|
|
throw new ArgumentOutOfRangeException(nameof(value));
|
|
}
|
|
}
|