Howdy!
Welcome back to my blog series where we have been writing a custom networked player controller in Unreal. Last time, we implemented Entity Extrapolation as a way of replicating movement for the other players in the game.
This strategy doesn't work well for player controllers and this week we will be implementing a better approach, entity interpolation.
Entity Interpolation
The idea of entity interpolation is that, instead of getting information about where the player might be going and attempting to predict their movement until the next network update, as we did with entity extrapolation, let's instead interpolate between the last two known positions of the player received from the server.
This diagram shows this same idea. Because we are interpolating between two known positions, the movement of the player should always be close to identical to what it was on the player's local machine (assuming the server update rate is relatively quick).
To implement this, I created another queue of FPlayerMoves called serverPositionsToSimulate which stores the last three position updates received from the server (I chose three to give ample buffer in the event of a dropped packet). When the multicast is received by a simulated proxy (an actor which is owned by a networked player, not the local one), then the position received from the server in enqueued, as shown in the image above.
Once three moves are in the queue, the variables are set so that in the update loop:
we interpolate between the oldest server update to the most recent one over the time of one server update (in this case 100ms). Once that 100ms is up, we pop the top of the queue and interpolate between the next two positions. Because we received new server positions about every 100ms, we should always have two positions to interpolate between.
Average Conditions:
Bad Conditions:
And that's it! As you can see in the videos above, the improvement is pretty noticeable, especially in the bad network environment. There might be the occasional speed up for the other networked entities in the event of extreme lag spikes or packet loss, but the movement remains smooth which is a significant improvement over the naive and entity extrapolation approaches.
The only drawback to this approach is that we are technically seeing all other entities in the past compared to where we are locally because we are interpolating between old network updates. We will start to address this problem next week when we start implementing lag compensation for the networked controller.
Github:
Opmerkingen