First of all, I read all of the Valve and Gaffer articles but I still have no clue, how to achieve my goal.
I am creating a UDP multiplayer game with a lot of physics (knockbacks, attacks). Packets delivery and sequence numbers are done - there is no loss, but the problem is with synchronizing ME (when I'm moving) on the second client (my opponent's).
As I read, there are two options:
1) delay the client by 100 ms and draw everything from the past - which is unacceptable, cause it really has to be a real time game. I know that for Valve this is enough, cause they have only shooting and they go back in time for just couple of variables, check some timesteps and they're done, but here, I have to check everything, if the player was in the air, if he was punched etc.
There is also a second option which is in a REAL TIME:
2) Real time, but with dead reckoning/extrapolation:
- we get last two received positions
- we make a normalized vector
- no matter what - we just move the player by that vector in an update method
And here is the problem. I decided to pick option two - with extrapolation, cause it's the only solution, but I have no idea, how to stop the character in a place on the opponents client, where it was stopped on it's own client.
An example: Speed is constant - 5 pixels per move. Perfect world - we send a packet with position every frame. So we move by 5 steps on X axis:
0,0 -> 5,0 -> 10,0 -> 15,0 -> 20,0 -> 25,0 (We stoppped on the client)
So we've sent 5 positions to the server and he sent it to the opponent.
We receive these packets on a second client (opponent) and we have to move the character with received packets.
And here comes the question, HOW? Of course, vectors, I know these, but I have no idea how to synchronize this, that at the end the character on the opponent's phone is still on the same position (25,0).
An example of a problem: We move 4 right, 2 left on the client: 0 > 5 > 10 > 15 > 20 > 15 > 10.
Opponents gets: 0, 5, 10, 15, 20 (he's moving by 1,0 normalized vector) but this is extrapolation, he moves also to 25, 30 (WHICH IS OKAY, it doesn't matter if he runned too far, it's only 10 pixels), then he get's the newest packet, he changes the vector to (-1,0) to move left, he moves two times and here comes the total misunderstanding, he will move only two times and he will land on (20,0), not on (10,0), cause this is the end of the packets.
A quicker explanation - he wasted his moves by moving 20 -> 25 -> 30, he should have use them to move these two points left - from x = 20 to x = 10;
I really need help with this, I went through hell by implementing reliable UDP and this desynchronization is killing me, really.
If someone had some experience with these things in Java, or anything, I would be grateful.