wip - fixing movement physics
This commit is contained in:
@@ -23,6 +23,8 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
||||
|
||||
[Export]
|
||||
public float DragCoefficient { get; set; } = 1.3f;
|
||||
[Export]
|
||||
public float FloatingDragCoefficientMultiplier { get; set; } = 0.8f;
|
||||
|
||||
[Export]
|
||||
public Label DebugTextLabel { get; set; }
|
||||
@@ -35,6 +37,8 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
||||
|
||||
private float TilesToMeters => 9.81f / Gravity;
|
||||
|
||||
private float PxToMeters => (1 / UnitToPx) * TilesToMeters;
|
||||
|
||||
[Export]
|
||||
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);
|
||||
}
|
||||
|
||||
private float CalculateRunDrag(float delta) {
|
||||
return
|
||||
private float CalculateDrag(float velocity, float delta) {
|
||||
var minopposing = Mathf.Abs(velocity * 0.99f);
|
||||
return Mathf.Clamp(
|
||||
0.5f *
|
||||
DragCoefficient *
|
||||
(this.IsOnFloor() ? DragCoefficient : DragCoefficient * FloatingDragCoefficientMultiplier) *
|
||||
(Mathf.Pow(TilesToMeters, 3) * AirDensity) *
|
||||
(Mathf.Pow(TilesToMeters, 2) * DragArea) *
|
||||
Mathf.Pow(Velocity.X * TilesToMeters, 2) *
|
||||
Mathf.Pow(velocity * PxToMeters, 2) *
|
||||
_invmass *
|
||||
-Mathf.Sign(Velocity.X) *
|
||||
-Mathf.Sign(velocity) *
|
||||
delta *
|
||||
(1f / TilesToMeters);
|
||||
(1f / TilesToMeters),
|
||||
-minopposing,
|
||||
minopposing);
|
||||
}
|
||||
|
||||
public override void _Ready() {
|
||||
@@ -98,11 +105,10 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
||||
var lastCollision = this.IsOnCeiling();
|
||||
RecalculateLv();
|
||||
|
||||
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;
|
||||
float velx = (Velocity.X / UnitToPx) + CalculateDrag(Velocity.X, (float)delta);
|
||||
float vely = (Velocity.Y / UnitToPx) + Gravity * (float)delta + CalculateDrag(Velocity.Y, (float)delta);
|
||||
|
||||
if (Mathf.Abs(velx) < 0.5) velx = 0;
|
||||
if (Mathf.Abs(velx) < 0.3) velx = 0;
|
||||
|
||||
var grounded = this.IsOnFloor();
|
||||
var coyoteGrounded = _timeOffGround < CoyoteTiming;
|
||||
@@ -140,7 +146,7 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
||||
$"vely: {vely}\n" +
|
||||
$"velx: {velx}\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);
|
||||
Velocity = new Vector2(velx, vely) * UnitToPx;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user