How can I change a node's position during physics interpolation without fast moving effect?

Godot Version

4.3

Question

Currently, I have enabled physics interpolation. When I directly change the position, the object flickers between the two points, as if it were moving at high speed rather than teleporting instantly.
According to the doc, I attempted to use reset_physics_interpolation() to resolve this issue, but calling this function had no effect.

I tried the following approaches:

# 1
global_position = new_pos
reset_physics_interpolation()

# 2
reset_physics_interpolation()
global_position = new_pos
reset_physics_interpolation()

# 3
set_deferred("global_position", new_pos)
reset_physics_interpolation()

# 4
reset_physics_interpolation()
set_deferred("global_position", new_pos)
reset_physics_interpolation()

None of these approaches resolved the issue.
How do I use this function correctly?

Edit: Node are CharacterBody2D and RigidBody2D

What are you trying to reset? If it’s a rigid body then the physics engine will take precedence once you teleport it so it won’t work. You need to do it inside the RigidBody2D._integrate_forces() function and modify the state.transform.origin instead.

extends RigidBody2D

@onready var reset_pos = global_position
var reset = false

func _integrate_forces(state: PhysicsDirectBodyState2D) -> void:
	if reset:
		state.transform.origin = reset_pos
		# Call reset_physics_interpolation() at the end of the frame once the physics engine has been updated
		reset_physics_interpolation.call_deferred()
		reset = false
		

func _on_area_2d_body_entered(body: Node2D) -> void:
	if body == self:
		reset = true

Result:

Left is being interpolated, right is not. I lowered the physics ticks per second to 20 in the example.

2 Likes

hide the node before teleporting, then show it again. it should look like in dragon ball.


visible = false
global_position = new_pos
visible = true

maybe it needs a few extra steps but you get the idea.

1 Like