Implement source-backed Scooter vehicle in tutorial and outside

This commit is contained in:
UnixNight committed 2026-11-04 18:06:09 -05:00
1 parent 8df5e81070
commit 62c07a3d59
44 files changed
+3135 -174

No files matched your search

+102
View File
@@ -0,0 +1,102 @@
using System.Numerics;
namespace GamePort.Game;
public readonly record struct ScooterSteering(float Speed=0,float Angle=0,bool KeyboardTurn=false,bool Reverse=false,
bool Braking=false,bool Throttle=false,float BrakeTime=0,float YawRate=0,float Blend=0);
public readonly record struct ScooterDrive(ScooterSteering Steering,Vector3 Velocity,Vector3 Rotation);
public readonly record struct ScooterContact(float Speed,Vector3 Acceleration,bool Impact);
public readonly record struct ScooterImpact(Vector3 Velocity,Vector3 AngularVelocity,float Time,bool Active);
/// <summary>465830 keyboard prefix and465220 ordinary non-AI/non-sprint drive.
/// Wheel/body support is observed independently; never fabricated here.</summary>
public static class ScooterMotion
{
public const float Maximum=1485;
/// <summary>421910 with the constructor's disabled bounce flag.</summary>
public static ScooterContact Contact(float speed,Vector3 velocity,Vector3 rotation,Vector3 normal,
Vector3 acceleration,bool impact,bool throttle,float dt)
{
float length=OriginalBodySupport.QuickMagnitude(velocity with{Y=0});
var forward=RocketOrientation.Rotate(Vector3.UnitZ,rotation);
if(!impact&&length>20&&MathF.Abs(Vector3.Dot(forward,normal))>=.9f)
return new(0,Vector3.Zero,true);
if(impact||speed<=0||length==0)return new(speed,acceleration,impact);
float dot=Vector3.Dot(velocity/OriginalBodySupport.QuickMagnitude(velocity),normal);
dot*=dot<-.85f?3.5f:1.5f;
if(dot<0&&throttle)speed=MathF.Max(230,speed-MathF.Abs(dot)*1200*dt);
return new(speed,acceleration,impact);
}
/// <summary>4669F0 ordinary enabled-body impact suffix.</summary>
public static ScooterImpact Impact(Vector3 velocity,Vector3 angular,float time,float dt)
{
time+=dt;
return time>=4?new(Vector3.Zero,Vector3.Zero,time,false):
new(velocity+new Vector3(-velocity.X*dt,0,-velocity.Z*dt),angular-angular*dt,time,true);
}
public static ScooterSteering Input(ScooterSteering state,PlayerInput input,int rear,float dt)
{
var angle=state.Angle;var keyboard=state.KeyboardTurn;
if(input.Left){angle+=200*dt;keyboard=true;}
else if(input.Right){angle-=200*dt;keyboard=true;}
else if(keyboard)angle=angle>0?MathF.Max(0,angle-200*dt):MathF.Min(0,angle+200*dt);
angle=Math.Clamp(angle,-45,45);float speed=state.Speed;bool reverse=state.Reverse,brake=state.Braking;
bool throttle=rear>0&&(input.Forward||input.Fire);
if(throttle){if(speed<Maximum)speed=MathF.Min(Maximum,speed+700*dt);reverse=false;}
if(rear>0&&input.Backward)
{
if(!reverse)
{
reverse=true;
if(speed>0){speed=MathF.Max(0,speed-1200*dt);brake=true;}
else if(speed<0){speed=MathF.Min(0,speed+1200*dt);brake=true;}
}
else speed=MathF.Max(-Maximum*.4f,speed-1200*dt);
}
else {brake=false;reverse=false;}
return state with{Speed=speed,Angle=angle,KeyboardTurn=keyboard,Reverse=reverse,Braking=brake,Throttle=throttle};
}
public static ScooterDrive Apply(ScooterSteering state,Vector3 velocity,Vector3 rotation,bool supported,int front,int rear,float dt,float friction=.9f)
{
float speed=state.Speed,rate=state.YawRate,blend=state.Blend,timer=state.BrakeTime;
var horizontal=velocity with{Y=0};var magnitude=OriginalBodySupport.QuickMagnitude(horizontal);
if(magnitude>0)
{
if(front>0)
{
rate=front*state.Angle*MathF.Min(magnitude*.005f,1);
var direction=horizontal/magnitude;var forward=RocketOrientation.Rotate(Vector3.UnitZ,rotation);
var delta=rate*dt*(Vector3.Dot(direction,forward)<0?-1:1);
rotation=RocketOrientation.Add(rotation,new(0,delta,0));
}
if(front+rear!=0)
{
if(!state.Throttle&&!state.Reverse)
{speed=(float)(speed-speed*.5f*dt);if(speed>=-1&&speed<=1)speed=0;}
if(!state.Braking)speed=(float)(speed-Math.Abs(state.Angle)*(1.0/45)*(speed*.8f)*dt);
speed=(float)(speed-Math.Sin(rotation.X*.01745329238474369f)*500*dt);
}
}
if(supported&&front+rear>=2)
{
var forward=RocketOrientation.Rotate(Vector3.UnitZ,rotation);var direction=forward;
var liveMagnitude=OriginalBodySupport.QuickMagnitude(velocity);
if(speed>0&&liveMagnitude!=0)
{
direction=velocity/liveMagnitude;
if(Vector3.Dot(direction,forward)<0)direction=forward;
if(!state.Braking)
{
var factor=(float)(2250*.6600000262260437f*.20000000298023224f/speed);
blend=MathF.Min(friction,friction*factor);
direction=new(direction.X+forward.X*blend,forward.Y,direction.Z+forward.Z*blend);
}
timer=0;
}
else if(state.Braking){timer+=dt;if(timer<1)speed=0;}
var next=direction*speed;
if(velocity.Y<0)next.Y+=velocity.Y;
velocity=next;
}
return new(state with{Speed=speed,YawRate=rate,Blend=blend,BrakeTime=timer},velocity,rotation);
}
}