Clientside prediction & correction
Members - Reputation: 100
Posted 02 January 2012 - 07:24 AM
I'm creating a 2d sidescroller mmorpg and I wanted to ask how to program client-side prediction & correction?
Well especially the correction, because prediction is basically just running the physics code you have running on the server.
The server sends corrections about every two seconds. This messages contains X & Y positions and X & Y velocity and the Input like
left 0, right 1, jump 1...
Is it ok that I do the same for other clients? So instead of sending snapshots of position and velocity, I only send input that has changed, and the local
client will predict where the other clients are moving to. The server sends corrections every 2-3 seconds.
Thanks a lot!
Members - Reputation: 130
Posted 02 January 2012 - 03:59 PM
Sending commands are much more efficient than sending snapshots of position.
The client should be able to calculate and run the command such as "moveto objectid,x,y,speed".
Sending corrections... I do not get it... The coordinate system should be the same everywhere.
A little difference is acceptable.
Crossbones+ - Reputation: 12258
Posted 13 January 2012 - 04:37 AM
My assumption is, that the server outrules everything else, that is, the only consistent world exist on the server side. The client controls the own player and predicts the other players.
You could use two steps (client side):
Prediction is done only on the client side. For each player you got the following data
t = time
x,y = position
vx,vy = velocity
That is, you need to hold the last X frames of data you receive from the server (history of server-side player movement)
t_0 is the current time. At this time the current player position is not available by the client yet, so you need to predict it. A simple prediction would be to extrapolate the position/velocity on a spline depending on t_4,t_3,t_2,t_1. To refine it, you can use this extrapolated data as input for your physics engine , to predict a more consistent position/velocity.
t_0 is always recalculated , whenever you receive a new data tuple, you need to update your history of t_4 to t_1 accordingly.
The problem is, if positioning the player directly at the position at t_0, and you receiving new data (=correction), then you get choppy movement. To avoid this, you need to approximate the calculated position in a smooth way. That is, instead of just positioning the player on the calculated position at time t_0 you take the current position and target position (t_0) and interpolate the position to the target position on a smooth curve.
Members - Reputation: 135
Posted 13 January 2012 - 01:40 PM
each command has a timecode.
Clients run their user commands locally in realtime. this is your prediction. The client also saves state of the result of each movement command and the timecode for that command/state pair.
Client sends bunches of commands including previous commands over udp at a regular interval.. the reason for duplicaitng older commands is to compensate for packet loss.
Server rebuilds the user command stream using the timecodes.
Server simulates all usercommands that have a newer timecode than the user command executed last frame on the server.
At each snapshot, the server sends back the state of the last executed command and the timecode of that command. We can call this the "Last Acknowledged Command" (LAC).
When the client receives the snapshot, the client character is immediately snapped to the state that the server snapshot indicates. The client then uses the timecode of the snapshot as a start point and simulates all the user commands locally that occur after the snapshot to bring the client back into realtime predicted state. This is done within one frame and any huge error will be smoothed over the next few frames. The result is that if the physics sim is deterministic, the client and server will agree most of the time and you will not see much snapping at all.
Even without error smoothing I've had a 200ms connection feel smooth.
They also use delta compression on the bunches of commands and snapshots to minimize bandwidth usage.
Also to note is that on the server, each client is in a quasi stable state. The snapshots takes stills of this and the clients tend to interpolate remote players between snapshot orientations or actually accept a list of commands to sim the remote player characters. The result is that all remote players look smoothish to each client, but if you were local to the server, using the server's direct state, you'd see every remote player jittering.
This is why the local client on a listen server in id games is an actual client that also recieves snapshots from the server code and does all processing in duplicate of the server (for it's own client character at least) as a real client should.
This method also requires that each character can be simulated through physics separately from the rest of the world. This will not work with globally correct physics sims that aim to sim the entire world each frame (your character running into a stack of cardboard boxes). Some intermediate might be possible where if the client impacts objects that can move, that those objects are now brought along into the independant simulation of the client commands that have reached the server.
Edited by Infernal-rk, 13 January 2012 - 01:49 PM.