Skip to content

Commit 649360f

Browse files
authored
Fix FPS-dependent movement in Navigation AStar demo (#1227)
Previously, the way in which the spaceship steered depended on the rendered framerate. This moves ship movement to the physics step to avoid this, and enables physics interpolation for smooth motion.
1 parent a5f9289 commit 649360f

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

2d/navigation_astar/character.gd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func _ready() -> void:
2525
_change_state(State.IDLE)
2626

2727

28-
func _process(_delta: float) -> void:
28+
func _physics_process(_delta: float) -> void:
2929
if _state != State.FOLLOW:
3030
return
3131

@@ -44,6 +44,7 @@ func _unhandled_input(event: InputEvent) -> void:
4444
if event.is_action_pressed(&"teleport_to", false, true):
4545
_change_state(State.IDLE)
4646
global_position = _tile_map.round_local_position(_click_position)
47+
reset_physics_interpolation()
4748
elif event.is_action_pressed(&"move_to"):
4849
_change_state(State.FOLLOW)
4950

@@ -52,7 +53,7 @@ func _move_to(local_position: Vector2) -> bool:
5253
var desired_velocity: Vector2 = (local_position - position).normalized() * speed
5354
var steering: Vector2 = desired_velocity - _velocity
5455
_velocity += steering / MASS
55-
position += _velocity * get_process_delta_time()
56+
position += _velocity * get_physics_process_delta_time()
5657
rotation = _velocity.angle()
5758
return position.distance_to(local_position) < ARRIVE_DISTANCE
5859

2d/navigation_astar/game.tscn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,4 @@ texture = ExtResource("6_b3lcn")
5959

6060
[node name="Camera2D" type="Camera2D" parent="."]
6161
offset = Vector2(576, 324)
62+
process_callback = 0

2d/navigation_astar/project.godot

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ move_to={
4040
]
4141
}
4242

43+
[physics]
44+
45+
common/physics_ticks_per_second=120
46+
common/physics_interpolation=true
47+
4348
[rendering]
4449

4550
renderer/rendering_method="gl_compatibility"

0 commit comments

Comments
 (0)