From 77bc341f9b0f232981309ff0a7d24526a46453f9 Mon Sep 17 00:00:00 2001 From: qwsdcvghyu89 <61093706+qwsdcvghyu89@users.noreply.github.com> Date: Tue, 9 Jun 2026 04:00:33 +1000 Subject: [PATCH] Changed run/walk to be based more off of momentum. Fixed jump bug. --- objects/scenes/demo/demo.tscn | 7 ++-- src/components/PlayerPhysicsController.cs | 51 ++++++++++++++++++----- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/objects/scenes/demo/demo.tscn b/objects/scenes/demo/demo.tscn index 0169a9d..896762e 100644 --- a/objects/scenes/demo/demo.tscn +++ b/objects/scenes/demo/demo.tscn @@ -162,9 +162,11 @@ position = Vector2(755, 0) position = Vector2(1, 0) safe_margin = 0.16 script = ExtResource("2_oekxy") -FloatingWalkSpeed = 3.5 -WalkSpeed = 4.0 +FloatingWalkAcceleration = 30.0 +WalkAcceleration = 80.0 JumpHeight = 6.0 +DragArea = 1.525 +AirDensity = 1.3 DebugTextLabel = NodePath("Label") Sprite = NodePath("AnimatedSprite2D") Mass = 0.5 @@ -183,7 +185,6 @@ animation = &"walk" zoom = Vector2(3, 3) [node name="Label" type="Label" parent="Player/CharacterBody2D" unique_id=436935745] -visible = false offset_left = 22.0 offset_top = -75.0 offset_right = 139.0 diff --git a/src/components/PlayerPhysicsController.cs b/src/components/PlayerPhysicsController.cs index f070164..dedfc88 100644 --- a/src/components/PlayerPhysicsController.cs +++ b/src/components/PlayerPhysicsController.cs @@ -4,17 +4,26 @@ using System.Diagnostics; public partial class PlayerPhysicsController : CharacterBody2D { [Export] - public float FloatingWalkSpeed { get; set; } = 1f; + public float FloatingWalkAcceleration { get; set; } = 1f; [Export] public float RunMultiplier { get; set; } = 1.84f; [Export] - public float WalkSpeed { get; set; } = 3f; + public float WalkAcceleration { get; set; } = 3f; [Export] public float JumpHeight { get; set; } = 5.5f; private float PeakTime { get; set; } = 0.1f; + [Export] + public float DragArea { get; set; } = 4f; + + [Export] + public float AirDensity { get; set; } = 37.26982f; + + [Export] + public float DragCoefficient { get; set; } = 1.3f; + [Export] public Label DebugTextLabel { get; set; } @@ -22,7 +31,9 @@ public partial class PlayerPhysicsController : CharacterBody2D { public AnimatedSprite2D Sprite { get; set; } [Export] - public float Gravity { get; set; } = 30f; + public float Gravity { get; set; } = 30.625f; + + private float TilesToMeters => 9.81f / Gravity; [Export] public float UnitToPx { get; set; } = 32; @@ -38,6 +49,19 @@ public partial class PlayerPhysicsController : CharacterBody2D { _lv = Mathf.Sqrt(Mathf.Pow(Gravity * PeakTime / 2, 2) + 2 * Gravity * JumpHeight); } + private float CalculateRunDrag(float delta) { + return + 0.5f * + DragCoefficient * + (Mathf.Pow(TilesToMeters, 3) * AirDensity) * + (Mathf.Pow(TilesToMeters, 2) * DragArea) * + Mathf.Pow(Velocity.X * TilesToMeters, 2) * + _invmass * + -Mathf.Sign(Velocity.X) * + delta * + (1f / TilesToMeters); + } + public override void _Ready() { RecalculateLv(); base._Ready(); @@ -74,9 +98,12 @@ public partial class PlayerPhysicsController : CharacterBody2D { var lastCollision = this.IsOnCeiling(); RecalculateLv(); - float velx = 0f; + float maxOpposingVel = -(Velocity.X * 0.99f / UnitToPx); + float velx = (Velocity.X / UnitToPx) + Mathf.Clamp(CalculateRunDrag((float)delta), Math.Min(maxOpposingVel, -maxOpposingVel), Math.Max(maxOpposingVel, -maxOpposingVel)); float vely = (Velocity.Y / UnitToPx) + Gravity * (float)delta; + if (Mathf.Abs(velx) < 0.5) velx = 0; + var grounded = this.IsOnFloor(); var coyoteGrounded = _timeOffGround < CoyoteTiming; if (grounded || coyoteGrounded) { @@ -87,10 +114,14 @@ public partial class PlayerPhysicsController : CharacterBody2D { vely = -_lv; } + if (!grounded && coyoteGrounded && Input.IsActionJustReleased("jump") && vely < 0) { + vely *= 1/4f; + } + if (Input.IsActionPressed("run")) { - velx = WalkSpeed * RunMultiplier; + velx += xAxis * WalkAcceleration * (float)delta * RunMultiplier; } else { - velx = WalkSpeed; + velx += xAxis * WalkAcceleration * (float)delta; } } else { if (Input.IsActionJustReleased("jump") && vely < 0) { @@ -98,20 +129,18 @@ public partial class PlayerPhysicsController : CharacterBody2D { } if (Input.IsActionPressed("run")) { - velx = FloatingWalkSpeed * RunMultiplier; + velx += xAxis * FloatingWalkAcceleration * (float)delta * RunMultiplier; } else { - velx = FloatingWalkSpeed; + velx += xAxis * FloatingWalkAcceleration * (float)delta; } } - velx *= xAxis; - DebugTextLabel.Text = $"grounded: {grounded}\n" + $"vely: {vely}\n" + $"velx: {velx}\n" + $"lv : {_lv:F2} or {_lv/(float)delta:F2}\n" + - $"tg : {_timeOffGround}"; + $"rd : {CalculateRunDrag((float)delta)}"; // Force = new Vector2(0, forcey); Velocity = new Vector2(velx, vely) * UnitToPx;