Godot Version
4.3.1
Question
Hi everyone,
I’m making a interpolator for my multiplayer code. The basic idea is I receive an object’s position, and move it from its old position to the new one before the next position arrives. I’m currently experimenting with the interpolation part using two sprites, a physics bound one and one that follows each frame, and came up with this:
@onready var cursor = $"Sprite2D"
@onready var follower = $"Sprite2D2"
var visual_object = null
var physics_object = null
var linear_direction = null
var frame_distance_percent = 0
func _process(delta: float) -> void:
# Get percentage of frame this delta takes up in terms of physics rate
var frame_percent = 1.0/((1.0/delta)/60.0)
# Add to variable to keep track of time until next frame
frame_distance_percent = frame_percent + frame_distance_percent
# If we haven't reached the end of the current frame, interpolate position
if frame_distance_percent <= 1.0:
# Move along direction from visual object position to physics object position, with percentage
# equal to the time within the current frame
var interp_pos = visual_object + linear_direction * frame_distance_percent
follower.global_position = interp_pos
func _physics_process(delta: float) -> void:
var pos = get_viewport().get_mouse_position()
# Set current position data of both objects
# As well as reset the frame percentage
cursor.global_position = pos
physics_object = pos
visual_object = follower.global_position
linear_direction = physics_object-visual_object
frame_distance_percent = 0
I tried using lerp to do this instead but had some trouble since it would smooth out the movement instead of making even, snappy jumps (if that makes any sense).
The way I’m currently doing this smoothing is I take the two positions, split the direction vector of them into equal segments, and move the following sprite each process call an equal distance calculated by delta. I was wondering if anyone had any tips to improve my method above, since I feel like doing this a few times every frame for multiple objects would be quite expensive…
On a side note, I was planning on doing the same thing to smooth out my player camera. Basically the character body would move in physics time, while the camera would be interpolated to the body to give the illusion of smoother movement while running/walking. Is this a good idea or does setting the camera position this many times reduce fps?
Thanks