Final basic ground and jump physics tweaks.
Release / Export Game (push) Successful in 1m57s

This commit is contained in:
qwsdcvghyu89
2026-06-11 15:25:50 +10:00
parent a0a288eee2
commit a4d73f8697
2 changed files with 61 additions and 60 deletions
File diff suppressed because one or more lines are too long
+46 -51
View File
@@ -3,12 +3,14 @@ using System;
using System.Diagnostics;
public partial class PlayerPhysicsController : CharacterBody2D {
[Export]
public float FloatingWalkAcceleration { get; set; } = 1f;
[Export]
public float RunMultiplier { get; set; } = 1.84f;
// [Export]
// public float RunMultiplier { get; set; } = 1.84f;
[Export]
public float WalkAcceleration { get; set; } = 3f;
[Export]
public float StoppingDeceleration { get; set; } = 8f;
[Export]
public float CancellationAcceleration { get; set; } = 4f;
[Export]
public float JumpHeight { get; set; } = 5.5f;
@@ -16,15 +18,15 @@ public partial class PlayerPhysicsController : CharacterBody2D {
private float PeakTime { get; set; } = 0.1f;
[Export]
public float DragArea { get; set; } = 4f;
private float MaxFallSpeed { get; set; } = 8f;
[Export]
public float AirDensity { get; set; } = 37.26982f;
private float MaxRiseSpeed { get; set; } = 16f;
[Export]
public float DragCoefficient { get; set; } = 1.3f;
private float MaxRunSpeed { get; set; } = 16f;
[Export]
public float FloatingDragCoefficientMultiplier { get; set; } = 0.8f;
private float MaxWalkSpeed { get; set; } = 8f;
[Export]
public Label DebugTextLabel { get; set; }
@@ -42,33 +44,17 @@ public partial class PlayerPhysicsController : CharacterBody2D {
[Export]
public float UnitToPx { get; set; } = 32;
private float _lv = 0.0f;
private float _invmass = 1/2f;
[Export]
public float Mass { get => 1/_invmass; set => _invmass = 1/value; }
private float _timeOffGround = 0.0f;
private float epsilon = 10e-5f;
[Export]
public float CoyoteTiming { get; set; } = 0.1f;
private float _lv = 0.0f;
private void RecalculateLv() {
_lv = Mathf.Sqrt(Mathf.Pow(Gravity * PeakTime / 2, 2) + 2 * Gravity * JumpHeight);
}
private float CalculateDrag(float velocity, float delta) {
var minopposing = Mathf.Abs(velocity * 0.99f);
return Mathf.Clamp(
0.5f *
(this.IsOnFloor() ? DragCoefficient : DragCoefficient * FloatingDragCoefficientMultiplier) *
(Mathf.Pow(TilesToMeters, 3) * AirDensity) *
(Mathf.Pow(TilesToMeters, 2) * DragArea) *
Mathf.Pow(velocity * PxToMeters, 2) *
_invmass *
-Mathf.Sign(velocity) *
delta *
(1f / TilesToMeters),
-minopposing,
minopposing);
}
public override void _Ready() {
RecalculateLv();
base._Ready();
@@ -88,12 +74,16 @@ public partial class PlayerPhysicsController : CharacterBody2D {
< 300 => 4,
_ => 1
};
if (!Sprite.IsPlaying())
Sprite.Play();
} else {
Sprite.SpeedScale = 1;
}
if (grounded && absvel.X < 0.1) {
Sprite.Animation = "idle";
if (!Sprite.IsPlaying())
Sprite.Play();
}
if (!grounded && Velocity.Y > 0.1) {
@@ -105,11 +95,8 @@ public partial class PlayerPhysicsController : CharacterBody2D {
}
}
private float _timeOffGround = 0.0f;
[Export]
public float CoyoteTiming { get; set; } = 0.1f;
public float Deadzone { get; set; } = 0.1f;
public override void _PhysicsProcess(double delta) {
var xAxis = Input.GetAxis("move_left", "move_right");
@@ -119,10 +106,10 @@ public partial class PlayerPhysicsController : CharacterBody2D {
var lastCollision = this.IsOnCeiling();
RecalculateLv();
float velx = (Velocity.X / UnitToPx) + CalculateDrag(Velocity.X, (float)delta);
float vely = (Velocity.Y / UnitToPx) + Gravity * (float)delta + CalculateDrag(Velocity.Y, (float)delta);
float velx = (Velocity.X / UnitToPx);
float vely = (Velocity.Y / UnitToPx) + Gravity * (float)delta;
if (Mathf.Abs(velx) < 0.3) velx = 0;
if (Mathf.Abs(velx) < 0.1) velx = 0;
var grounded = this.IsOnFloor();
var coyoteGrounded = _timeOffGround < CoyoteTiming;
@@ -138,32 +125,40 @@ public partial class PlayerPhysicsController : CharacterBody2D {
vely *= 1/4f;
}
if (Input.IsActionPressed("run")) {
velx += xAxis * WalkAcceleration * (float)delta * RunMultiplier;
} else {
velx += xAxis * WalkAcceleration * (float)delta;
}
var accel = WalkAcceleration;
if (xAxis * Velocity.X < 0) accel = StoppingDeceleration;
velx += xAxis * accel * (float)delta;
} else {
if (Input.IsActionJustReleased("jump") && vely < 0) {
vely *= 1/4f;
}
if (Input.IsActionPressed("run")) {
velx += xAxis * FloatingWalkAcceleration * (float)delta * RunMultiplier;
} else {
velx += xAxis * FloatingWalkAcceleration * (float)delta;
}
}
if (Mathf.Abs(xAxis) < Deadzone) {
var autostop = -Mathf.Sign(velx) * StoppingDeceleration * (float)delta;
if (Mathf.Abs(autostop) > Mathf.Abs(velx)) velx = 0f;
else velx += autostop;
}
var maxXSpeed = Input.IsActionPressed("run") ? MaxRunSpeed : MaxWalkSpeed;
var maxYSpeed = vely > 0 ? MaxFallSpeed : MaxRiseSpeed;
var (absvelx, absvely) = (Mathf.Abs(velx), Mathf.Abs(vely));
float xcancel = 0, ycancel = 0;
if (absvelx > maxXSpeed) xcancel = -Math.Sign(velx) * CancellationAcceleration * (float)delta;
if (absvely > maxYSpeed) ycancel = -Math.Sign(vely) * CancellationAcceleration * (float)delta;
if (Mathf.Abs(xcancel) + 0.1 > absvelx) velx = 0;
else velx += xcancel;
if (Mathf.Abs(ycancel) + 0.1 > absvely) vely = 0;
else vely += ycancel;
DebugTextLabel.Text =
$"grounded: {grounded}\n" +
$"vely: {vely}\n" +
$"velx: {velx}\n" +
$"lv : {_lv:F2} or {_lv/(float)delta:F2}\n" +
$"rd : {CalculateDrag(Velocity.X, (float)delta):F2}, {CalculateDrag(Velocity.Y, (float)delta):F2}";
$"lv : {_lv:F2} or {_lv/(float)delta:F2}\n";
// Force = new Vector2(0, forcey);
Velocity = new Vector2(velx, vely) * UnitToPx;
Velocity = new Vector2(velx, vely);
Velocity *= UnitToPx;
MoveAndSlide();
}
}