Tuned platformer physics
This commit is contained in:
@@ -16,14 +16,39 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
||||
[Export]
|
||||
public float JumpScale { get; set; } = 12;
|
||||
|
||||
[Export]
|
||||
public float FrictionCoefficent { get; set; } = 0.1f;
|
||||
|
||||
[Export]
|
||||
public Label DebugTextLabel { get; set; }
|
||||
|
||||
[Export]
|
||||
public float Gravity { get; set; } = 9.8f;
|
||||
public AnimatedSprite2D Sprite { get; set; }
|
||||
|
||||
[Export]
|
||||
public float Gravity { get; set; } = 980f;
|
||||
|
||||
private float _timeInFlight = 0;
|
||||
|
||||
private float epsilon = 10e-5f;
|
||||
|
||||
public override void _Process(double delta) {
|
||||
var grounded = this.IsOnFloor();
|
||||
var absvel = new Vector2(Mathf.Abs(Velocity.X), Mathf.Abs(Velocity.Y));
|
||||
Sprite.FlipH = Velocity.X < 0;
|
||||
if (grounded && absvel.X > 0) {
|
||||
Sprite.Play("walk", absvel.X switch {
|
||||
< 10 => 1,
|
||||
< 150 => 2,
|
||||
< 300 => 4,
|
||||
_ => 1
|
||||
});
|
||||
} else if (grounded && absvel.X < epsilon && absvel.Y < epsilon) {
|
||||
Sprite.Play("idle");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public override void _PhysicsProcess(double delta) {
|
||||
var xAxis = Input.GetAxis("move_left", "move_right") * MovementScale;
|
||||
var yAxis = Input.GetAxis("look_up", "look_down");
|
||||
@@ -39,17 +64,21 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
||||
_timeInFlight = 0;
|
||||
|
||||
jumpStrength *= Mathf.Exp(-JumpStrengthDecay * _timeInFlight);
|
||||
|
||||
// autorun
|
||||
var friction = Mathf.Abs(Velocity.X) > 125 ? FrictionCoefficent * 1/8f : FrictionCoefficent;
|
||||
// var decay = Mathf.Abs(Velocity.X) > 125 ? MovementDecay * 1/4f : MovementDecay;
|
||||
xAxis *= Mathf.Exp(-MovementDecay * Mathf.Abs(Velocity.X));
|
||||
xAxis -= Velocity.X * friction;
|
||||
|
||||
|
||||
var moveVector = new Vector2(xAxis, Gravity + jumpStrength);
|
||||
DebugTextLabel.Text = $"grounded: {grounded}\nmv: {moveVector}\njumpStrength: {jumpStrength}\ntimeInFlight: {_timeInFlight}";
|
||||
var moveVector = new Vector2((float)(delta * xAxis), (float)( delta * (Gravity + jumpStrength)));
|
||||
DebugTextLabel.Text = $"grounded: {grounded}\nmv: {moveVector}\n vel: {Velocity}\n jumpStrength: {jumpStrength}\ntimeInFlight: {_timeInFlight}";
|
||||
Velocity += moveVector;
|
||||
MoveAndSlide();
|
||||
|
||||
if (!grounded)
|
||||
_timeInFlight += (float)delta;
|
||||
base._Process(delta);
|
||||
base._PhysicsProcess(delta);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user