Compose source body physics and enable tutorial static response

This commit is contained in:
UnixNight committed 2026-10-26 22:22:42 -04:00
1 parent df0263b26e
commit 35a2a66606
26 files changed
+1045 -18

No files matched your search

+38 -6
View File
@@ -9,7 +9,7 @@ public enum PlayerPose { Idle, Run, Backpedal, Jump, Fall, Fly }
/// <summary>
/// Walking, airborne steering and jetpack checkpoint. Recovered arithmetic is described in
/// movement-checkpoint.md. Capsule, floor-derived pivot and 60Hz are port policies;
/// movement-checkpoint.md. Legacy capsule, floor-derived pivot and60Hz are port policies;
/// original collision, intro, object interactions and animation timing remain open.
/// </summary>
public sealed class LabSimulation
@@ -18,6 +18,7 @@ public sealed class LabSimulation
private readonly SceneCollision collision;
private readonly Func<Vector3, Vector3, SupportHit?>? cameraRay;
private readonly Func<Vector3, Vector3, SupportHit?>? ceilingRay;
private readonly OriginalBodyPhysics? originalBody;
private readonly Vector3 sourceStart;
private readonly float startYaw, modelMinimumY, rootHeight, startFloor;
private float targetYaw, extraAirAcceleration, shadowDistance, lateralContribution;
@@ -34,6 +35,8 @@ public sealed class LabSimulation
public string LevelCode { get; set; } = "LV1B";
public int GadgetMode { get; set; } = -1;
public float Energy { get; set; } = 20;
//Source260/live15C serves both body collision and contact tests. The name
//predates source-body integration; burst100 is not an interaction-only size.
public float InteractionRadius { get; private set; } = 60;
public bool JetpackEffectVisible { get; private set; }
public float JetpackRemaining => jumpState.EffectRemaining;
@@ -50,6 +53,8 @@ public sealed class LabSimulation
public bool InputAfterPlayerUpdate { get; set; }
public float Yaw { get; private set; }
public float Speed { get; private set; }
public float PublishedSpeed { get; private set; }
public bool UsesOriginalBody => originalBody != null;
public float TurnRate { get; private set; }
public bool Grounded { get; private set; }
public PlayerPose Pose => Noclip ? Velocity.LengthSquared() > 0 ? PlayerPose.Fly : PlayerPose.Idle
@@ -76,11 +81,13 @@ public sealed class LabSimulation
public LabSimulation(SceneCollision collision, Mesh standingPose, Vector3 start, float yaw,
Func<Vector3, Vector3, SupportHit?>? cameraRay = null,
Func<Vector3, Vector3, SupportHit?>? ceilingRay = null)
Func<Vector3, Vector3, SupportHit?>? ceilingRay = null,
OriginalStaticCollision? originalCollision = null)
{
this.collision = collision;
this.cameraRay = cameraRay;
this.ceilingRay = ceilingRay;
originalBody = originalCollision == null ? null : new OriginalBodyPhysics(originalCollision);
sourceStart = start;
startYaw = yaw;
var bounds = standingPose.Bounds();
@@ -99,10 +106,10 @@ public sealed class LabSimulation
public void Reset()
{
var floor = collision.GroundHeight(sourceStart, 2500)!.Value;
Position = new(sourceStart.X, floor + SceneCollision.Skin, sourceStart.Z);
Position = new(sourceStart.X, floor + (UsesOriginalBody ? 0 : SceneCollision.Skin), sourceStart.Z);
Velocity = Vector3.Zero;
Yaw = targetYaw = startYaw;
Speed = TurnRate = extraAirAcceleration = shadowDistance = 0;
Speed = PublishedSpeed = TurnRate = extraAirAcceleration = shadowDistance = 0;
lateralContribution = 0; pendingImpulse = Vector3.Zero; movementMode = 0;
jumpState = default;
forceFall = JetpackEffectVisible = false;
@@ -222,7 +229,7 @@ public sealed class LabSimulation
public void StopDebugMotion()
{
Velocity = pendingImpulse = Vector3.Zero;
Speed = TurnRate = extraAirAcceleration = lateralContribution = 0;
Speed = PublishedSpeed = TurnRate = extraAirAcceleration = lateralContribution = 0;
jumpState = default; movementMode = 0; forceFall = JetpackEffectVisible = false;
targetYaw = Yaw;
}
@@ -316,12 +323,18 @@ public sealed class LabSimulation
else if (air.Pose is JetpackPose.Jump or JetpackPose.Fly) forceFall = false;
if (air.StartGadget) Bursts++;
Velocity = velocity; // Produced body state is consumed by the next physics pass.
PublishedSpeed = Speed; //437C22..437C2D: controller608 -> body160.
}
private void StepPhysics()
{
PhysicsSteps++;
const float dt = FixedStepClock.Step;
if (originalBody != null)
{
StepOriginalPhysics();
return;
}
var wasGrounded = Grounded;
var velocity = Velocity;
// Keep the existing capsule/gravity adapter until C4 recovers Jimmy's
@@ -355,6 +368,25 @@ public sealed class LabSimulation
MaximumRise = Math.Max(MaximumRise, Position.Y - startFloor);
}
private void StepOriginalPhysics()
{
if (DynamicRootResolver != null)
throw new NotSupportedException("Original ordinary-body subset cannot use the provisional dynamic resolver");
var before = Position;
var result = originalBody!.Step(new(ActorRoot, Velocity, Vector3.UnitY * extraAirAcceleration,
pendingImpulse, Grounded, PublishedSpeed), FixedStepClock.Step, InteractionRadius);
Position = result.Body.Position - Vector3.UnitY * rootHeight;
Velocity = result.Body.Velocity;
pendingImpulse = result.Body.Impulse;
extraAirAcceleration = result.Body.Acceleration.Y;
Grounded = result.Body.Supported;
if (Grounded) forceFall = false;
if (result.Landing != null) Landings++;
if (result.Contact is { StoredNormal.Y: > -.7f }) WallHits++;
Travel += Vector2.Distance(new(before.X, before.Z), new(Position.X, Position.Z));
MaximumRise = Math.Max(MaximumRise, Position.Y - startFloor);
}
private void StepCamera()
{
const float dt = FixedStepClock.Step;
@@ -383,5 +415,5 @@ public sealed class LabSimulation
step is >= 80 and < 120, step is >= 60 and < 95, false, step < 180);
public bool SmokePassed => Steps >= 240 && Travel > 50 && MaximumRise > 20 && Jumps > 0 && Landings > 0 && Grounded;
public string Report => $"Walk checkpoint: steps={Steps}, controller={ControllerSteps}, physics={PhysicsSteps}, effect={EffectSteps}, travel={Travel:F2}, jumps={Jumps}, landings={Landings}, max rise={MaximumRise:F2}, wall hits={WallHits}, ceiling hits={CeilingHits}, grounded={Grounded}, feet={Position}.";
public string Report => $"Walk checkpoint: steps={Steps}, controller={ControllerSteps}, physics={PhysicsSteps}, effect={EffectSteps}, travel={Travel:F2}, jumps={Jumps}, landings={Landings}, max rise={MaximumRise:F2}, wall hits={WallHits}, ceiling hits={CeilingHits}, grounded={Grounded}, feet={Position}, body={(UsesOriginalBody ? "source ordinary" : "provisional capsule")}.";
}
+121
View File
@@ -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));
}
}
+9 -3
View File
@@ -325,7 +325,8 @@ internal static class Program
debugCameraRay = cameraRay;
simulation = new LabSimulation(new SceneCollision(world), standingPose, originalStart.Position, originalStart.Rotation.Y,
cameraRay == null ? null : (look, eye) => cameraRay.Trace(look, eye),
cameraRay == null ? null : (start, end) => cameraRay.TraceGeometry(start, end))
cameraRay == null ? null : (start, end) => cameraRay.TraceGeometry(start, end),
level.Code == "VR04" ? OriginalStaticCollision.FromRouteScene(world,level.Code) : null)
{ LevelCode = level.Code, InputAfterPlayerUpdate = carriedSave != null };
if (carriedPlayer != null)
{
@@ -396,8 +397,13 @@ internal static class Program
mesh = spriteVisuals.Mesh;
staticVisuals = new StaticSceneVisuals(mesh, director, root, Path.Combine(Path.GetDirectoryName(scene!)!, "models"));
mesh = staticVisuals.Mesh;
simulation!.DynamicRootResolver = (previous, position, radius) =>
DynamicBodyResponse.Resolve(staticVisuals.DoorColliders(), previous, position, radius);
if (simulation!.UsesOriginalBody)
{
if (staticVisuals.DoorColliders().Any())
throw new NotSupportedException("Source ordinary-body scene has unsupported dynamic door colliders");
}
else simulation.DynamicRootResolver = (previous, position, radius) =>
DynamicBodyResponse.Resolve(staticVisuals.DoorColliders(), previous, position, radius);
jetpackVisuals = new JetpackVisuals(mesh, Path.GetFullPath(Path.Combine(Path.GetDirectoryName(scene!)!, "..")));
mesh = jetpackVisuals.Mesh;
hud = new SceneHud(mesh, Path.Combine(Path.GetDirectoryName(scene!)!, "../ui/ui.json"));