Compose source body physics and enable tutorial static response
This commit is contained in:
1 parent
df0263b26e
commit
35a2a66606
26 files changed
+1045
-18
No files matched your search
@@ -0,0 +1,121 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace GamePort.Game;
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
/// <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.
|
||||
/// </summary>
|
||||
public sealed class OriginalBodyPhysics
|
||||
{
|
||||
private readonly OriginalStaticCollision collision;
|
||||
|
||||
public OriginalBodyPhysics(OriginalStaticCollision collision)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(collision);
|
||||
if (collision.HasSpecialPlayerContacts)
|
||||
throw new NotSupportedException("Ordinary body composition requires recovered special-contact policies for this scene");
|
||||
this.collision = collision;
|
||||
}
|
||||
|
||||
public OrdinaryBodyStep Step(OrdinaryBodyState body, float seconds,
|
||||
float radius = OriginalBodySupport.JimmyRadius, float factor = OriginalBodySupport.JimmyFactor,
|
||||
float friction = .8f, float suppressionScale = .5f, bool geometryEnabled = true)
|
||||
{
|
||||
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)
|
||||
|| !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;
|
||||
body = body with { Supported = false };
|
||||
var anchor = new Vector3(0, -25000, 0);
|
||||
if (!geometryEnabled)
|
||||
{
|
||||
//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);
|
||||
var context = new StaticCollisionContext(radius);
|
||||
var horizontalFirst = delta.Y <= 0;
|
||||
var move = collision.Move(previous, horizontalFirst ? delta with { Y = 0 } : delta,
|
||||
radius, context, geometryEnabled: geometryEnabled);
|
||||
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 (velocity.Y > 0) velocity.Y = 0;
|
||||
if (acceleration.Y > 0) acceleration.Y = 0;
|
||||
}
|
||||
var position = move.Position;
|
||||
if (horizontalFirst) position.Y += delta.Y;
|
||||
position.Y = MathF.Max(position.Y, -25000);
|
||||
body = body with { Position = position, Velocity = velocity, Acceleration = acceleration, Impulse = impulse };
|
||||
|
||||
SupportHit? supportHit = null;
|
||||
BodyLandingObservation? landing = null;
|
||||
if (geometryEnabled)
|
||||
{
|
||||
var support = OriginalBodySupport.Resolve(new(position, previous, velocity, acceleration, radius, factor),
|
||||
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);
|
||||
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(Vector3 value)
|
||||
{
|
||||
if (!float.IsFinite(value.X) || !float.IsFinite(value.Y) || !float.IsFinite(value.Z))
|
||||
throw new ArgumentOutOfRangeException(nameof(value));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user