PDA

View Full Version : Multiplayer games - Where do I put the game logic?



haagel
October 29th, 2006, 04:28 PM
I'm a rookie at game development, and I have a question about multiplayer games.

Let's say I'm doing the classic "Pong-game". The game is played over the internet from two different clients. Both clients have event-handlers that moves the players own paddle when he/she press the right buttons on the keyboard. The koordinates of the own paddle is sent to the opponent, and the opponents paddle-position is recieved. But what about the ball? The ball should bounce at walls and at the paddles, but where do I put the logic for that? In one of the clients or both?

In my first version of this game I put this logic in one of the clients. In this client the code checks to see if the ball should bounce. It also moves the ball. The ball coordinates is then sent to the other client.

Is this the wrong way to go? The reason I chose this way to go was that I was afraid that the ball might be on different positions in the clients. With my technique I always know that the ball is in the same position in both clients. But maybe that's the case anyway?

My though was that because I use the UDP-protocol, packets might be lost during transfer. Let's say client1 s paddle is moving up, but during the transfer of the new coordinates, some packets is lost. The paddle moves up and reflects the incoming ball on client1s screen, but in client2s screen the paddle don't move up (because of the lost packets) and misses the ball. This will mean that the ball will be in different position on the two clients screens.

I feel I might be thinking in the wrong direction, but I just want to clear this thing out ones and for all.

I hope someone will take the time to answer my question because I believe it is important for me to know this so I'll be able to make a little more advanced games in the future.

Thanks! //David

Joppe
October 29th, 2006, 04:51 PM
Well, you could do the logic in the middlehand. As I dont think you are sending the packets directly between the clients, you could do the game logic in between. Like player1 moves his paddle downwards and player2 moves his upwards. Both those are sent to the middlehand and the middlehand moves the ball indepedently. Then both clients gets back the coordinates of each other and the ball. The middlehand could also do the hit testing.

SacrificialLamb
October 29th, 2006, 06:13 PM
I understand in some games they have it so that both players do manage every thing on there own machine then test for differences. You having the master position (position reverted to on a discrepancy) of your own unit (paddle) and all other things like the bull should be the same. For lost packets for a pong game I would use the center line to change master position or last player to hit the ball would have master position. Working on the theory that the physics are the same and the position of the ball and the angle of reflection would be right on the machine that last hit the ball, but you would need to change master position of the ball before it would hit the other player so it could not be effected by packet loss.

haagel
October 31st, 2006, 04:02 PM
Thanks for your answers joppe and SacrificialLamb! I appreciate it!

In SacrificialLambs model, how will it work for other games? If I a make som type of game where two players each have some vehicle, let's say a tank, which can shot bullets. The movement of the tank and the firing of the bullets could be done at each client, that is each client moves its own tank and bullets. But the test for hits (that is, when the bullets hit a tank)? Where will that check be done? On the client that shot the bullet or the client that gets hit?

I hope you want to help me a bit more. I feel I need to sort this out before I can go on with my games...

I'm sure some of you have made games of this type or something similiar. How did you solve this? I appreciate all help, ideas and comments!

SacrificialLamb
November 1st, 2006, 02:06 AM
Depending on the bullets nature if it’s fast (not hope in getting away) the client that shot but if you are able to get out of the way I would do it on the client that gets hit because for them what they see on there computer is there right position and if given a time to move and they do and it still gets them I would consider that worse.
I think I should tell/worn you that I have never made a multiplayer game I have picked up some theory but the majority of this principal I have here is me working from my laggy experiences on FPS games.

Joppe
November 1st, 2006, 03:00 AM
For a tank game, I think the check can be done at each client too. I mean, you could just have a huge array of stuff you move that also gets sent to the other client. Like the map for instance, that you dont need to move. So no idea on sending it to the other client. Becouse you could move the bullet, check for hitting and then move the bullet on client2.

bombsledder
November 11th, 2006, 12:14 PM
are you setting your game up like this

client1 <-> client2

sending packets like that, because one way to solve this problem would be

client1 <-server->client

that way the server can hold all the variables, do all the calculations, etc..

im not sure I understand so much so tell me if im wrong

signifer123
November 15th, 2006, 05:20 PM
I prefer the both sides run the test and detect differences, though this is simpler code and will work generically with any server without modification needed, and may prove quite laggy.

In Armagetron, the server keeping track of everything and if a client does something illegal they are killed.

bombsledder
November 15th, 2006, 06:32 PM
well i prefer a server in the middle that way I don't have to worry about cheating since if they use a hex editor there health etc.. will be updated back again as soon as the server updates the health again or w/e the game is going to be