wip - fixing movement physics

This commit is contained in:
qwsdcvghyu89
2026-06-11 14:43:13 +10:00
parent 77bc341f9b
commit 3ccf193a7c
5 changed files with 66 additions and 18 deletions
File diff suppressed because one or more lines are too long
+4
View File
@@ -26,6 +26,7 @@ move_right={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"location":0,"echo":false,"script":null) , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":0,"axis_value":1.0,"script":null) , Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":0,"axis_value":1.0,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":14,"pressure":0.0,"pressed":true,"script":null)
] ]
} }
move_left={ move_left={
@@ -33,6 +34,7 @@ move_left={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"location":0,"echo":false,"script":null) , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":0,"axis_value":-1.0,"script":null) , Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":0,"axis_value":-1.0,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":13,"pressure":0.0,"pressed":true,"script":null)
] ]
} }
jump={ jump={
@@ -46,6 +48,7 @@ look_up={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194320,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194320,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":119,"location":0,"echo":false,"script":null) , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":119,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":3,"axis_value":-1.0,"script":null) , Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":3,"axis_value":-1.0,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":11,"pressure":0.0,"pressed":true,"script":null)
] ]
} }
look_down={ look_down={
@@ -53,6 +56,7 @@ look_down={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":82,"key_label":0,"unicode":114,"location":0,"echo":false,"script":null) , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":82,"key_label":0,"unicode":114,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":3,"axis_value":1.0,"script":null) , Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":3,"axis_value":1.0,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":12,"pressure":0.0,"pressed":true,"script":null)
] ]
} }
run={ run={
+17 -11
View File
@@ -23,6 +23,8 @@ public partial class PlayerPhysicsController : CharacterBody2D {
[Export] [Export]
public float DragCoefficient { get; set; } = 1.3f; public float DragCoefficient { get; set; } = 1.3f;
[Export]
public float FloatingDragCoefficientMultiplier { get; set; } = 0.8f;
[Export] [Export]
public Label DebugTextLabel { get; set; } public Label DebugTextLabel { get; set; }
@@ -35,6 +37,8 @@ public partial class PlayerPhysicsController : CharacterBody2D {
private float TilesToMeters => 9.81f / Gravity; private float TilesToMeters => 9.81f / Gravity;
private float PxToMeters => (1 / UnitToPx) * TilesToMeters;
[Export] [Export]
public float UnitToPx { get; set; } = 32; public float UnitToPx { get; set; } = 32;
@@ -49,17 +53,20 @@ public partial class PlayerPhysicsController : CharacterBody2D {
_lv = Mathf.Sqrt(Mathf.Pow(Gravity * PeakTime / 2, 2) + 2 * Gravity * JumpHeight); _lv = Mathf.Sqrt(Mathf.Pow(Gravity * PeakTime / 2, 2) + 2 * Gravity * JumpHeight);
} }
private float CalculateRunDrag(float delta) { private float CalculateDrag(float velocity, float delta) {
return var minopposing = Mathf.Abs(velocity * 0.99f);
return Mathf.Clamp(
0.5f * 0.5f *
DragCoefficient * (this.IsOnFloor() ? DragCoefficient : DragCoefficient * FloatingDragCoefficientMultiplier) *
(Mathf.Pow(TilesToMeters, 3) * AirDensity) * (Mathf.Pow(TilesToMeters, 3) * AirDensity) *
(Mathf.Pow(TilesToMeters, 2) * DragArea) * (Mathf.Pow(TilesToMeters, 2) * DragArea) *
Mathf.Pow(Velocity.X * TilesToMeters, 2) * Mathf.Pow(velocity * PxToMeters, 2) *
_invmass * _invmass *
-Mathf.Sign(Velocity.X) * -Mathf.Sign(velocity) *
delta * delta *
(1f / TilesToMeters); (1f / TilesToMeters),
-minopposing,
minopposing);
} }
public override void _Ready() { public override void _Ready() {
@@ -98,11 +105,10 @@ public partial class PlayerPhysicsController : CharacterBody2D {
var lastCollision = this.IsOnCeiling(); var lastCollision = this.IsOnCeiling();
RecalculateLv(); RecalculateLv();
float maxOpposingVel = -(Velocity.X * 0.99f / UnitToPx); float velx = (Velocity.X / UnitToPx) + CalculateDrag(Velocity.X, (float)delta);
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 + CalculateDrag(Velocity.Y, (float)delta);
float vely = (Velocity.Y / UnitToPx) + Gravity * (float)delta;
if (Mathf.Abs(velx) < 0.5) velx = 0; if (Mathf.Abs(velx) < 0.3) velx = 0;
var grounded = this.IsOnFloor(); var grounded = this.IsOnFloor();
var coyoteGrounded = _timeOffGround < CoyoteTiming; var coyoteGrounded = _timeOffGround < CoyoteTiming;
@@ -140,7 +146,7 @@ public partial class PlayerPhysicsController : CharacterBody2D {
$"vely: {vely}\n" + $"vely: {vely}\n" +
$"velx: {velx}\n" + $"velx: {velx}\n" +
$"lv : {_lv:F2} or {_lv/(float)delta:F2}\n" + $"lv : {_lv:F2} or {_lv/(float)delta:F2}\n" +
$"rd : {CalculateRunDrag((float)delta)}"; $"rd : {CalculateDrag(Velocity.X, (float)delta):F2}, {CalculateDrag(Velocity.Y, (float)delta):F2}";
// Force = new Vector2(0, forcey); // Force = new Vector2(0, forcey);
Velocity = new Vector2(velx, vely) * UnitToPx; Velocity = new Vector2(velx, vely) * UnitToPx;
+21
View File
@@ -0,0 +1,21 @@
using Godot;
using System;
public partial class ResetPlayer : Area2D {
[Export]
public Node2D Player { get; set; }
[Export]
public Vector2 ResetPosition { get; set; }
public override void _Ready() {
this.BodyShapeEntered += OnBodyShapeEntered;
base._Ready();
}
private void OnBodyShapeEntered(Rid bodyRid, Node2D body, long bodyShapeIndex, long localShapeIndex) {
if (Player == body) {
Player.Position = ResetPosition;
}
}
}
+1
View File
@@ -0,0 +1 @@
uid://d0qoxtv6ld1ef