Better movement handling

This commit is contained in:
Marco 2025-08-14 17:50:11 +02:00
commit c68e40b8c3
5 changed files with 96 additions and 143 deletions

View file

@ -75,47 +75,51 @@ public partial class IsoMovementModule : ModuleBase<PlayerState, CharacterBody3D
HitboxSpriteProvider.SetVisibility(_isStrafing);
}
private Vector2 InterpolateVelocityAxes(
Vector2 current, Vector2 target, float accel, float decel, float delta)
{
// Accelerate when we're increasing magnitude toward target on that axis; otherwise decelerate.
float stepX = (Mathf.Abs(target.X) > Mathf.Abs(current.X)) ? accel : decel;
float stepY = (Mathf.Abs(target.Y) > Mathf.Abs(current.Y)) ? accel : decel;
float newX = Mathf.MoveToward(current.X, target.X, stepX * delta);
float newY = Mathf.MoveToward(current.Y, target.Y, stepY * delta);
return new Vector2(newX, newY);
}
public override void PhysicsProcess(double delta)
{
var frameVelocity = MainObject.Velocity.ToVector2();
var frameVelocityY = MainObject.Velocity.Y;
var v3 = MainObject.Velocity;
Vector2 v = v3.ToVector2();
float dt = (float)delta;
// Read desired input direction for this physics tick
Vector2 inputDir = PlayerStorage.MovementDirection;
// Clamp diagonal magnitude so diagonals aren't faster (helps remove jittery speed jumps)
float len = inputDir.Length();
if (len > 1f)
inputDir /= len;
if (_isStrafing)
{
// Instant movement at strafe speed
frameVelocity = PlayerStorage.MovementDirection * StrafeSpeed;
// Strafing stays instant/responsive if you want that behavior
v = inputDir * StrafeSpeed;
}
else
{
Vector2 targetVelocity = PlayerStorage.MovementDirection * Speed;
// Build a target *velocity* (not just direction)
Vector2 targetVel = inputDir * Speed;
if (PlayerStorage.MovementDirection != Vector2.Zero)
{
frameVelocity = frameVelocity.MoveToward(targetVelocity, Acceleration * (float)delta);
}
else
{
frameVelocity = frameVelocity.MoveToward(Vector2.Zero, Deceleration * (float)delta);
}
// Per-axis interpolation prevents direction snaps when keys change
v = InterpolateVelocityAxes(v, targetVel, Acceleration, Deceleration, dt);
}
//MainObject.Velocity += _movementDirection * MovementSpeed;
var velocityY = Mathf.Clamp(frameVelocityY + Gravity * (float)delta, -FallSpeed, FallSpeed);
// Y (gravity)
float vy = Mathf.Clamp(v3.Y + Gravity * dt, -FallSpeed, FallSpeed);
if (Input.IsKeyLabelPressed(Key.Z)) vy = 10;
//frameVelocity.Y = velocityY;
// Debug moonjump
if (Input.IsKeyLabelPressed(Key.Z))
{
//velocityY -= (float)(100 * delta);
velocityY = 10;
}
MainObject.Velocity = frameVelocity.ToVector3(velocityY);
MainObject.Velocity = v.ToVector3(vy);
MainObject.MoveAndSlide();
}
}