I'm creating a multiplayer game using LibGdx (with Box2d) and Kryonet. Since this is the first time I work on multiplayer games, I read a bit about server - client implementations, and it turns out that the server should handle important tasks like collision detection, hits, characters dying etc...

Based on some articles (like the excellent Gabriel Gambetta Fast paced multiplayer series), I also know that the client should work in parallel to avoid the lag while the server responds to commands.

Physics wise, each game will have 2 players, and any projectiles fired.

What I'm thinking of doing is the following:

  1. Create a physics world on the client

  2. When the game is signaled to start, I create the same physics world on the server (without any rendering obviously).

  3. Whenever the player issues a command (move or fire), I send the command to the server and immediately start processing it on the client.

  4. When the server receives the command, it applies it on the server's world (set velocity etc...)

  5. Each 100ms, the server sends the new state to the client which corrects what was calculated locally.

  6. Any critical action (hit, death, level up) is calculated only on the server and sent to the client.

Essentially, I would have a Box2d World object running on the server for each game in progress, in sync with the worlds running on the clients.

The alternative would be to do my own calculations on the server instead of relying on Box2D to do them for me, but I'm trying to avoid that.

My question is:

  1. Is it wise to have, for example, 1000 instances of the World object running and executing steps on the server? Tomcat used around 750 MBytes of memory when trying it without any object added to the world. Anybody tried that before?
  2. If not, is there any alternative? Google did not help me, are there any guidelines to use when you want to have physics on both the client and the server?

Thanks for any help.

  • \$\begingroup\$ A thought. - Have the player have their own world that's synched with the server world, then check for out of sync by using a special sync object with positional values (compare them every so often). \$\endgroup\$
    – user52358
    Oct 1 '14 at 9:40

The memory usage won't be the bottleneck in your game server, since an instance of the world object does not take that much memory. Tomcat alone takes a lot of the 750MB of memory you observed. The real issue will be CPU cost, if you have 1k instance of world with 2 players and many projectiles on each, your server will have some hard time unless it is very very powerfull. Even there, you will be limited on the number of players.

One solution could be having a dispaching server, where users will connect first and that will redirect the them on a light loaded server, that can run multiples instances of the world object. Of course if you have only 1 server, this won't help you but if you can have 2k players playing your game, I guess you can rent another server, all will be ready if you code it early. With this technique you virtually have no limit in the number of players.

Another solution, but this is not what you want at a first look, is creating the server on one of the two players, where every calculations will be done. From time to time, results will be sent to a master server that will check if everything has been done without cheat. (Using some hash and challenge over network to check if client and servers are acting as expected) But you will have to work a lot on predictions because one of the players will have lower ping. No limit in the number of players there too.

I think there is other ways to deal with your issue, but this is what came to my head.

Good luck for your project

  • \$\begingroup\$ You are right, I just made a test with 5000 instances running (with only 1 object added) and that completely crashed my core i5. It will be worse once the full game is running. I guess once we hit more than 1000 concurrent users, buying other servers won't be a problem. So I will implement with this in mind. Thanks! \$\endgroup\$ Nov 20 '13 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.