speed = (currentTime - startTime) / (endTime - startTime)
frame = Date.now()
const speed = (frame - positions[0].frame) / (positions[0 + delay].frame - positions[1 + delay].frame);
- The interpolation factor
speed
represents the position between the two most recent positions in the positions array. frame
is the current frame of the game.positions[0].frame
is the frame of the most recent position in the positions array.positions[0 + delay].frame
andpositions[1 + delay].frame
are the frames of the two most recent positions, with a delay offset.- The formula
(frame - positions[0].frame) / (positions[0 + delay].frame - positions[1 + delay].frame)
calculates the interpolation factorspeed
as a value between 0 and 1, based on the current frame and the frames of the two most recent positions.
interpolate point = (1 - speed) * from + speed * to
clientPlayerInterpolated.pos.x = (1 - speed) * positions[1 + delay].x + speed * positions[0 + delay].x;
clientPlayerInterpolated.pos.y = (1 - speed) * positions[1 + delay].y + speed * positions[0 + delay].y;
- The formula
(1 - speed) * positions[1 + delay].x + speed * positions[0 + delay].x
performs the linear interpolation between the x-coordinates of the two most recent positions. - The formula
(1 - speed) * positions[1 + delay].y + speed * positions[0 + delay].y
performs the linear interpolation between the y-coordinates of the two most recent positions.
const speed = (frame - positions[0].frame) / (positions[0 + delay].frame - positions[1 + delay].frame);
clientPlayerInterpolated.pos.x = (1 - speed) * positions[1 + delay].x + speed * positions[0 + delay].x;
clientPlayerInterpolated.pos.y = (1 - speed) * positions[1 + delay].y + speed * positions[0 + delay].y;