Implement verified Grappler body camera and helpers in VR08

This commit is contained in:
UnixNight committed 2026-11-04 11:27:03 -05:00
1 parent ffea678e90
commit 43806fe862
39 files changed
+2429 -40

No files matched your search

+35
View File
@@ -0,0 +1,35 @@
using System.Numerics;
namespace GamePort.Game;
public readonly record struct GrapplerCameraLocals(Vector3 Eye,Vector3 Look,Vector3 Gains)
{
public static GrapplerCameraLocals Default=>new(new(0,200,-350),new(0,80,0),new(1.6f,1,1.6f));
public GrapplerCameraLocals Equip()=>this with{Eye=new(0,50,-350),Look=new(0,150,80)};
public GrapplerCameraLocals Attach()=>this with{Eye=new(Eye.X,400,-650),Gains=new(.8f,1,.8f)};
public GrapplerCameraLocals Detach()=>new(new(0,50,-350),new(0,130,100),new(1.6f,1,1.6f));
}
public readonly record struct GrapplerCameraResult(Vector3 Eye,Vector3 Target,Vector3 Look,Vector3 DesiredEye,float RayGain);
/// <summary>Ordinary438BC0/439900 follow branches. Excludes flags828/851 and
/// Bubble mode2; aim is quantized from look minus OLD shared eye.</summary>
public static class GrapplerCamera
{
public static GrapplerCameraResult Follow(GrapplerCameraLocals locals,Vector3 root,Vector3 rotation,
float turnRate,int movementMode,Vector3 oldEye,float seconds,Func<Vector3,Vector3,SupportHit?>? ray)
{
GrapplerMotion.Time(seconds);GrapplerMotion.Finite(root);GrapplerMotion.Finite(rotation);GrapplerMotion.Finite(oldEye);
if(movementMode==2)throw new NotSupportedException("Grappler camera excludes Bubble mode2");
var eyeLocal=locals.Eye;var lookLocal=locals.Look;var eyeRotation=rotation;Vector3 gains;
if(movementMode==0)
{eyeLocal.Y-=20;lookLocal.Y=(float)(lookLocal.Y+1.9*10);gains=locals.Gains+new Vector3(0,.2f,0);}
else {eyeRotation=new(0,RocketOrientation.AddAxis(rotation.Y,turnRate),rotation.Z);gains=new(1.2f,2,1.2f);}
var desired=root+RocketOrientation.Rotate(eyeLocal,eyeRotation);
var look=root+RocketOrientation.Rotate(lookLocal,rotation);float gain=1;
if(ray?.Invoke(look,desired) is{} hit){desired=look-(look-hit.Position)*.75f;gain=1.5f;}
var eye=oldEye+(desired-oldEye)*gains*(gain*seconds);
var(pitch,yaw)=OriginalHeading.Angles(look-oldEye);
var direction=RocketOrientation.Rotate(Vector3.UnitZ,new(pitch*.02197265625f,yaw*.02197265625f,0));
return new(eye,eye+direction*200,look,desired,gain);
}
}
+115
View File
@@ -0,0 +1,115 @@
using System.Numerics;
using GamePort.Assets;
namespace GamePort.Game;
/// <summary>Process globals owned by retained Jimmy; not TSK fields.</summary>
public sealed class GrapplerMemory
{
public Vector3 Anchor;
public float FirstLength,Length,ReelTarget;
public float RopeScaleZ=1;
public GrapplerSwing Swing;
public void Clear(){Anchor=default;FirstLength=Length=ReelTarget=0;RopeScaleZ=1;Swing=default;}
}
public readonly record struct GrapplerMove(Vector3 Root,Vector3 Velocity);
public readonly record struct GrapplerInputResult(Vector3 Root,Vector3 Rotation,Vector3 Velocity,bool NewAttachment,bool EndsActiveInput=false);
/// <summary>Owned raw7 controller, bounded to VR08. Body and input phases are
/// placed by LabSimulation; shared globals survive reconstructed scene loads.</summary>
public sealed class GrapplerGadget : IDisposable
{
private readonly GrapplerRay ray;
public GrapplerMemory Memory {get;}
public GrapplerCameraLocals Camera {get;private set;}=GrapplerCameraLocals.Default;
public bool Equipped {get;private set;}
public bool Attached {get;private set;}
public bool SwingPose {get;private set;}
public bool CursorVisible {get;private set;}
public bool LoopPlaying {get;private set;}
public Vector3 CursorPosition {get;private set;}
public Vector3 RopeRoot {get;private set;}
public Vector3 RopeRotation {get;private set;}=new(0,0,180);
public int Attachments {get;private set;}
public int Detachments {get;private set;}
public int Contacts {get;private set;}
public event Action<bool>? EquippedChanged;
public event Action? Detached;
public event Action<int,bool>? Sound;
public event Action? LoopStopped;
public GrapplerGadget(Mesh world,string level,GrapplerMemory? memory=null)
{ray=new(world,level);Memory=memory??new();}
public void Equip()
{
if(Equipped)Release();
Equipped=true;Camera=Camera.Equip();EquippedChanged?.Invoke(true);
}
public void Release()
{
Detach();CursorVisible=false;Camera=GrapplerCameraLocals.Default;
if(Equipped){Equipped=false;EquippedChanged?.Invoke(false);}
SetLoop(false);
}
public void Reset()
{Release();Memory.Clear();Attachments=Detachments=Contacts=0;RopeRoot=default;RopeRotation=new(0,0,180);}
public void Detach()
{
if(!Attached)return;
Attached=SwingPose=false;Detachments++;
Memory.Swing=Memory.Swing with{X=Memory.Swing.X*.96f,Z=Memory.Swing.Z*.96f};
Camera=Camera.Detach();SetLoop(false);Detached?.Invoke();
}
public void Contact()
{
if(!Attached)return;
Contacts++;var response=GrapplerGeometry.Contact(Memory.Swing);Memory.Swing=response.Swing;
if(response.Detach)Detach();
}
public GrapplerInputResult StepInput(Vector3 root,Vector3 rotation,Vector3 velocity,PlayerInput input,
float seconds,Func<Vector3,GrapplerMove> move)
{
if(!Equipped)return new(root,rotation,velocity,false);
SetLoop(Attached&&input.Fire);
Memory.Length=GrapplerMotion.FastReel(Memory.Length,Memory.ReelTarget,seconds);
if(GrapplerMotion.JumpDetaches(input.Jump,input.Fire,Attached))
{Detach();return new(root,rotation,Vector3.Zero,false);}
if(Attached)
{
rotation=new(0,rotation.Y,0);SwingPose=true;
var constraint=GrapplerGeometry.Constrain(root,Memory.Anchor,Memory.Swing,Memory.Length,seconds);
var body=move(constraint.DesiredRoot);root=body.Root;velocity=body.Velocity with{Y=0};
//The callback can detach/clear scalars, but this source tail still runs.
Memory.Swing=GrapplerMotion.Swing(Memory.Swing,seconds,constraint.Offset.X,constraint.Offset.Z,Memory.Length);
var(pitch,yaw)=OriginalHeading.Angles(constraint.Offset);
RopeRotation=new(pitch*.02197265625f,yaw*.02197265625f,0);
RopeRoot=constraint.DesiredRoot+Vector3.UnitY*116;
}
var aim=ray.Aim(root,rotation,input.Fire,input.GrapplerEnter,Memory.Length,seconds);
Memory.Length=aim.Length;CursorVisible=aim.CursorVisible;CursorPosition=aim.CursorPosition;
if(aim.EndsActiveInput||!input.Fire)return new(root,rotation,velocity,false,aim.EndsActiveInput);
if(Attached)
{
bool reeling=Memory.Length>250;
Memory.Length=GrapplerMotion.HeldReel(Memory.Length,seconds);SetLoop(reeling);
return new(root,rotation,velocity,false,true);
}
//426A70 performs a fresh query; a cursor position is not an anchor cache.
if(ray.Trace(root,rotation) is not{} hit)return new(root,rotation,velocity,false,true);
var attached=GrapplerMotion.Attach(root,hit.Hit.Position,velocity,Memory.FirstLength);
Memory.Anchor=hit.Hit.Position;Memory.Length=attached.Length;Memory.FirstLength=attached.InitialLength;
if(attached.RopeScaleZ is{} scale)Memory.RopeScaleZ=scale;
Memory.ReelTarget=attached.FastReelTarget;Memory.Swing=default;
Attached=true;Attachments++;Camera=Camera.Attach();
Sound?.Invoke(31,false);SetLoop(false);SetLoop(true);
return new(root,rotation,attached.Velocity,true,true);
}
public void Dispose()=>Release();
private void SetLoop(bool play)
{
if(LoopPlaying==play)return;LoopPlaying=play;
if(play)Sound?.Invoke(34,true);else LoopStopped?.Invoke();
}
}
+48 -4
View File
@@ -5,8 +5,8 @@ namespace GamePort.Game;
public readonly record struct PlayerInput(bool Forward, bool Backward, bool Left, bool Right, bool Jump,
bool Rise = false, bool Descend = false, bool StrafeLeft = false, bool StrafeRight = false, bool Fast = false, bool Fire = false,
bool RocketTurn = false, bool RocketRoll = false, bool RocketMouseRise = false);
public enum PlayerPose { Idle, Run, Backpedal, Jump, Fall, Fly, Shoot }
bool RocketTurn = false, bool RocketRoll = false, bool RocketMouseRise = false,bool GrapplerEnter=false);
public enum PlayerPose { Idle, Run, Backpedal, Jump, Fall, Fly, Shoot, Swing }
/// <summary>
/// Walking, airborne steering and jetpack checkpoint. Recovered arithmetic is described in
@@ -35,6 +35,8 @@ public sealed class LabSimulation
private Vector3 noclipEntry;
private float noclipEntryYaw;
private int gadgetMode = -1;
private Vector3? grapplerCameraTarget;
public GrapplerGadget? Grappler {get;private set;}
public bool Noclip { get; private set; }
public bool HasNoclipEntry { get; private set; }
public float NoclipSpeed { get; set; } = 600;
@@ -61,6 +63,7 @@ public sealed class LabSimulation
if (gadgetMode == 0) ReleaseBubble();
if (gadgetMode == 2) Invisible.Release();
if (gadgetMode == 6) Baseball.Release();
if (gadgetMode == 7) Grappler?.Release();
gadgetMode = -1;
if (value == 0)
{
@@ -74,6 +77,11 @@ public sealed class LabSimulation
}
if (value == 2 && !Invisible.Equip(Energy, IgnoresGadgetEnergy)) return;
if (value == 6) Baseball.Equip();
if(value==7)
{
if(Grappler==null)return;
Grappler.Equip();
}
gadgetMode = value;
}
}
@@ -110,6 +118,7 @@ public sealed class LabSimulation
public float TurnRate { get; private set; }
public bool Grounded { get; private set; }
public PlayerPose Pose => Noclip ? Velocity.LengthSquared() > 0 ? PlayerPose.Fly : PlayerPose.Idle
: Grappler?.SwingPose==true ? PlayerPose.Swing
: GadgetMode == 0 ? bubblePose
: pendingImpulse.Y > 0 ? PlayerPose.Jump
: Grounded ? Shooting ? PlayerPose.Shoot : Speed < -1 ? PlayerPose.Backpedal : Speed > 1 ? PlayerPose.Run : PlayerPose.Idle
@@ -128,7 +137,7 @@ public sealed class LabSimulation
public Func<Vector3, Vector3, float, Vector3>? DynamicRootResolver { get; set; }
public Vector3 ActorRoot => Position + Vector3.UnitY * rootHeight;
public Vector3 CameraEye { get; private set; }
public Vector3 CameraTarget => ActorRoot + new Vector3(0, 80 + 1.9f * Baseball.CameraPitch, 0);
public Vector3 CameraTarget => grapplerCameraTarget??ActorRoot + new Vector3(0, 80 + 1.9f * Baseball.CameraPitch, 0);
public Matrix4x4 Model => Matrix4x4.CreateRotationZ(-(TracksBubbleOrientation?Roll:0) * MathF.PI / 180)
* Matrix4x4.CreateRotationX(-(TracksBubbleOrientation?Pitch:0) * MathF.PI / 180) * Matrix4x4.CreateRotationY(-Yaw * MathF.PI / 180)
* Matrix4x4.CreateTranslation(Position - new Vector3(0, modelMinimumY, 0));
@@ -163,6 +172,8 @@ public sealed class LabSimulation
public void Reset()
{
if(gadgetMode==7){GadgetMode=-1;DefaultGadgetRequested?.Invoke();}
Grappler?.Reset();grapplerCameraTarget=null;
if (gadgetMode == 0) { GadgetMode=-1; DefaultGadgetRequested?.Invoke(); }
if (gadgetMode == 2) gadgetMode = -1;
Invisible.Reset();
@@ -282,6 +293,25 @@ public sealed class LabSimulation
AdjustEnergy(-1);ScoreChanged?.Invoke(-1);CancelPoweredFlight();
}
public void EnableGrappler(Mesh world,GrapplerMemory memory)
{
if(LevelCode!="VR08"||originalBody==null)throw new NotSupportedException("Live Grappler requires the VR08 source body");
Grappler=new(world,LevelCode,memory);
Grappler.Detached+=()=>{Velocity=Vector3.Zero;forceFall=true;};
}
private GrapplerMove MoveGrappler(Vector3 desired)
{
var moved=originalBody!.MoveAttached(ActorRoot,desired,InteractionRadius);
if(moved.CallbackPolygon is{} contact)
{
if(contact.StoredNormal.Y<=-.7f)
{if(Velocity.Y>0)Velocity=Velocity with{Y=0};if(extraAirAcceleration>0)extraAirAcceleration=0;}
else {WallHits++;Grappler!.Contact();}
}
return new(moved.Position,Velocity);
}
public void StopForPrimaryFire() => Speed = 0;
public void SetShooting(bool shooting) => Shooting = shooting;
public void AdjustEnergy(float delta) => energy.Adjust(delta, LevelCode, id => EnergySound?.Invoke(id));
@@ -343,6 +373,7 @@ public sealed class LabSimulation
if (enabled == Noclip) return false;
if (enabled)
{
if(gadgetMode==7){GadgetMode=-1;DefaultGadgetRequested?.Invoke();}
if (gadgetMode==0) { GadgetMode=-1; DefaultGadgetRequested?.Invoke(); }
noclipEntry = Position; noclipEntryYaw = Yaw; HasNoclipEntry = true;
}
@@ -426,6 +457,13 @@ public sealed class LabSimulation
}
Baseball.StepCameraInput(input);
StepCamera(); //438BC0/439900 input path owns follow; still provisional geometry/state.
if(Grappler is{Equipped:true} grappler)
{
var state=grappler.StepInput(ActorRoot,new(Pitch,Yaw,Roll),Velocity,input,dt,MoveGrappler);
Position=state.Root-Vector3.UnitY*rootHeight;Pitch=state.Rotation.X;Yaw=state.Rotation.Y;Roll=state.Rotation.Z;Velocity=state.Velocity;
if(state.NewAttachment){jumpState=jumpState with{JumpState=0,EffectRemaining=0};JetpackEffectVisible=false;}
if(state.EndsActiveInput)return;
}
if (movementMode==2)
{
AdjustEnergy(-dt*.5f);
@@ -548,7 +586,7 @@ public sealed class LabSimulation
Grounded = result.Body.Supported;
if (Grounded) forceFall = false;
if (result.Landing != null) Landings++;
if (result.Contact is { StoredNormal.Y: > -.7f }) WallHits++;
if (result.Contact is { StoredNormal.Y: > -.7f }) {WallHits++;Grappler?.Contact();}
Travel += Vector2.Distance(new(before.X, before.Z), new(Position.X, Position.Z));
MaximumRise = Math.Max(MaximumRise, Position.Y - startFloor);
}
@@ -556,6 +594,12 @@ public sealed class LabSimulation
private void StepCamera()
{
const float dt = FixedStepClock.Step;
if(Grappler is{} grappler&&movementMode!=2&&!Noclip)
{
var follow=GrapplerCamera.Follow(grappler.Camera,ActorRoot,new(Pitch,Yaw,Roll),TurnRate,movementMode,CameraEye,dt,cameraRay);
CameraEye=follow.Eye;grapplerCameraTarget=follow.Target;return;
}
grapplerCameraTarget=null;
var desired = DesiredEye();
var delta = (desired.Eye - CameraEye) * new Vector3(1.6f, 1.2f, 1.6f) * desired.Gain * dt;
CameraEye = cameraRay == null ? collision.ClipCamera(CameraTarget, CameraEye + delta) : CameraEye + delta;
+10
View File
@@ -27,6 +27,7 @@ public enum BodyResponseProfile { Jimmy, Baseball, Rocketship }
public sealed class OriginalBodyPhysics
{
private readonly OriginalStaticCollision collision;
public StaticCollisionContext? LastStaticContext {get;private set;}
public OriginalBodyPhysics(OriginalStaticCollision collision, bool allowSpecialMaterials = false)
{
@@ -38,6 +39,14 @@ public sealed class OriginalBodyPhysics
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,
@@ -68,6 +77,7 @@ public sealed class OriginalBodyPhysics
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);
+1 -1
View File
@@ -61,7 +61,7 @@ public sealed class OriginalStaticCollision
public static OriginalStaticCollision FromRouteScene(Mesh mesh, string level)
{
ArgumentNullException.ThrowIfNull(mesh);
if (level is not ("LV1B" or "VR01" or "VR02" or "VR04" or "VR07" or "LEV1"))
if (level is not ("LV1B" or "VR01" or "VR02" or "VR04" or "VR07" or "VR08" or "LEV1"))
throw new NotSupportedException("Original static game-filter recovery covers the route and VR01");
if (mesh.Indices.Length % 3 != 0 || mesh.CollisionPolygons.Length != mesh.Indices.Length / 3)
throw new InvalidDataException("Source static collision requires original polygon metadata");
+3 -1
View File
@@ -27,6 +27,7 @@ public sealed class PlayerSceneBinding : IDisposable
inventory.GadgetSelected += Selected;
player.Invisible.EquippedChanged += InvisibleEquipped;
player.Baseball.EquippedChanged += BaseballEquipped;
if(player.Grappler!=null)player.Grappler.EquippedChanged+=InvisibleEquipped;
player.DefaultGadgetRequested += DefaultGadget;
Selected(inventory.SelectedGadget);
}
@@ -68,7 +69,7 @@ public sealed class PlayerSceneBinding : IDisposable
if (!string.Equals(actor.Tag, tag, StringComparison.OrdinalIgnoreCase)) return;
//427FF0 switches raw mode, not display selection. A rejected Invisible
//equip can leave display2 with raw mode-1; cleanup does not equip it.
if (player.GadgetMode is 0 or 1 or 2 or 6 || inventory.SelectedGadget is -1 or 3)
if (player.GadgetMode is 0 or 1 or 2 or 6 or 7 || inventory.SelectedGadget is -1 or 3)
{ inventory.SelectGadget(-1); player.GadgetMode = -1; }
else if (player.GadgetMode != -1) UnsupportedGadgetCleanups++;
}
@@ -131,6 +132,7 @@ public sealed class PlayerSceneBinding : IDisposable
inventory.GadgetSelected -= Selected;
player.Invisible.EquippedChanged -= InvisibleEquipped;
player.Baseball.EquippedChanged -= BaseballEquipped;
if(player.Grappler!=null)player.Grappler.EquippedChanged-=InvisibleEquipped;
player.DefaultGadgetRequested -= DefaultGadget;
if(Rocketship!=null)Rocketship.RiderHit-=player.RocketImpact;
}
+5
View File
@@ -526,6 +526,11 @@ public sealed class SceneDirector
}
}
public void ReleasePlayerGadget()
{
if(actors.TryGetValue("JIM1",out var jimmy))GadgetReleaseRequested?.Invoke(jimmy);
}
private void DeactivateInvention(GamEntity source)
{
//Both CAM/MCA constructors default this property to true. The source
+1
View File
@@ -132,6 +132,7 @@ public sealed class SceneInteractions
{ destination = ReturnLevel; start = ReturnStart; }
if (string.Equals(destination, "none", StringComparison.OrdinalIgnoreCase))
{ unsupported.Add("RETURN has no stored destination"); break; }
if(returning)director.ReleasePlayerGadget(); //457FF7 selected cleanup precedes RETURN.
Transition?.Invoke(new(destination, start, source.Number("FadeTime"), source.Integer("FadeType"), returning));
break;
}