Saturday, March 25, 2017

xna - How to sync the actions in a mutiplayer game?



I connect the clients with UDP (its a peer to peer connection on a multicast network) and the clients are sending their positions in every frame (in WP7 it means the default 30 FPS) to each other. This game is kinda a pong game, and my problem is the next: whenever the opponent hits the ball the angle will not be the same on both mobiles. I think its because the latency (1 pixel difference can cause a different angle). So my question is: how can I sync the hitting event?



Answer



In a multiplayer game, every gameplay-relevant decision should be made by only one system. When multiple systems make a decision, like in your case the trajectory of the ball, and they disagree due to timing issues, the game gets out of sync.


When each client calculates the angle only after its own collisions and sends the new trajectory of the ball to the other, you will get the best results.


But note that this allows the players to cheat. They could manipulate their client to always tell the other client that they hit, and that the ball is now flying in an impossible to catch trajectory. The only way to fix that, is to introduce a neutral referee in form of a central server which receives the users input, calculates all game mechanics, and sends the results to the clients.


By the way: You could safe a lot of bandwidth when you would send the client positions only when they have actually changed, and not on every single frame.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...