Tweaking GroundMotionComponent
parent
385ed55505
commit
3ed113dd41
|
@ -1,28 +1,49 @@
|
|||
using Godot;
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using Godot;
|
||||
using Vector2 = Godot.Vector2;
|
||||
using Vector3 = Godot.Vector3;
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
public class GroundMotionComponent
|
||||
{
|
||||
public float MaxSpeed = 3;
|
||||
public float Accel = 50;
|
||||
public float Damping = 0.2f;
|
||||
public float MaxSpeed = 2;
|
||||
|
||||
public void Process(float delta, Entity entity, Vector3 target_position)
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Reference in New Issue