Entity rewinding
In order to solve this, we need to store the state of entities at every network update. When the entity needs to be rewound, we find the two states on either end of the target past time, and interpolate between them. This technique actually shares many similarities with the entity interpolation code we wrote in the last chapter, and in some places you could potentially reuse some of the same code.
So, in our network player class, we'll create a new struct to store the network state:
private struct networkState { public Vector3 Position; public Vector3 Rotation; public Vector3 CamRotation; public double Timestamp; }
We'll keep a buffer of these states, using an array just as in the last chapter:
private networkState[] stateBuffer = new networkState[20]; private int stateCount = 0;
To store a state, we shift the states and insert the state at 0
, incrementing the state count:
void bufferState( networkState state ) { // shift states // the state at index 0 moves to index...