diff --git a/components/GroundMotionComponent.cs b/components/GroundMotionComponent.cs index 1637d20..5cb9108 100644 --- a/components/GroundMotionComponent.cs +++ b/components/GroundMotionComponent.cs @@ -1,28 +1,49 @@ -using Godot; using System; +using System.Numerics; +using Godot; +using Vector2 = Godot.Vector2; +using Vector3 = Godot.Vector3; /// /// public class GroundMotionComponent { - public float MaxSpeed = 3; - - public void Process(float delta, Entity entity, Vector3 target_position) + public float Accel = 50; + public float Damping = 0.2f; + public float MaxSpeed = 2; + + public void PhysicsProcess(float delta, Entity entity, Vector3 target_position) { - Vector3 direction_xy = new Vector3(target_position.x - entity.GlobalTranslation.x, - 0, + Vector2 plane_target_vector = new Vector2(target_position.x - entity.GlobalTranslation.x, target_position.z - entity.GlobalTranslation.z); + float distance_error = plane_target_vector.Length(); - Vector3 new_velocity = direction_xy.Normalized() * MaxSpeed; - new_velocity.y = entity.Velocity.y - 9.81f * delta; + if (distance_error < 0.01) + { + entity.Velocity = Vector3.Zero; + } else { + Vector2 plane_velocity = new Vector2(entity.Velocity.x, entity.Velocity.z); + plane_velocity = plane_velocity + plane_target_vector / distance_error * Accel * delta; - if (direction_xy.LengthSquared() > 0.01) - { - entity.Velocity = entity.MoveAndSlide(new_velocity); - } - else - { - entity.Velocity = Vector3.Up * -9.81f * delta; + float projected_step = plane_target_vector.Dot(plane_velocity * delta); + GD.Print("Projected step: " + projected_step); + if (projected_step > 1) + { + plane_velocity /= projected_step; + } + + float plane_speed = plane_velocity.Length(); + if (plane_speed > MaxSpeed) + { + plane_velocity *= MaxSpeed / plane_speed; + } + + entity.Velocity = new Vector3( + plane_velocity.x, 0, plane_velocity.y); } + + entity.Velocity.x -= entity.Velocity.x * Damping; + entity.Velocity.z -= entity.Velocity.z * Damping; + entity.Velocity = entity.MoveAndSlide(entity.Velocity); } } \ No newline at end of file diff --git a/entities/Player.cs b/entities/Player.cs index f025d37..2cd39d3 100644 --- a/entities/Player.cs +++ b/entities/Player.cs @@ -30,10 +30,10 @@ public class Player : Entity } - public override void _Process(float _delta) + public override void _PhysicsProcess(float delta) { - base._Process(_delta); - _groundMotion.Process(_delta, this, TargetPosition); + base._PhysicsProcess(delta); + _groundMotion.PhysicsProcess(delta, this, TargetPosition); } public void OnPositionUpdated(Vector3 newPosition) diff --git a/scenes/AdaptiveWorldStream.tscn b/scenes/AdaptiveWorldStream.tscn index 68eb5a5..7520505 100644 --- a/scenes/AdaptiveWorldStream.tscn +++ b/scenes/AdaptiveWorldStream.tscn @@ -21,7 +21,7 @@ albedo_color = Color( 1, 1, 1, 0.156863 ) extents = Vector3( 20, 1, 20 ) [sub_resource type="CapsuleShape" id=7] -radius = 0.475 +radius = 0.2 height = 0.5 [sub_resource type="CapsuleMesh" id=8]