How to set up correctly my multiplayer

Hello everyone,

I am currently working on a FPS game

I finished working on the physic engine and now i want to set up the multiplayer

I’ve read a lot of topics about networking in fast paced games, but the examples that i have seen do not completely allow me to start into the subject with the necessary knowledge

Let me first explain my physic engine :

Each player’s inputs are stored in an array, for example when the player press the up arrow (keycode = 37) then array[37] = true, else false

I have a main loop that handle players movements physics and which is invoked each frame with requestanimationframe()

In this loop i verify if array[37] is true, then i add a certain value to Z axis, multiplied by the time elapsed with clock.getDelta()

So the distance traveled by each player in 1sec will be the same and independent of the refresh rate of their screen

Now the fun part : How to calcultate this, server side ?

I want the server to be authoritative, i just want to get the players inputs and let the server decides of the new positions of the players

Of course i want to implement client side prediction so the client doesn’t have to wait for the server to give them their new coordinates

First of all, do i have to send the key pressed event to the server, or each time the main loop (client side) detect a key is pressed in the variable array ?

Does the server also has to execute a loop which verify the key pressed in order to apply physics ?

But with this option i need to choose an interval for the loop, which will be different from the client

And what if the player pressed on W and 0,1 sec after on S, if he has 500ms ping, typically the server will know the player has pressed on W and only 500 ms after on S, so during 500ms, the loop will increase its Z axis value because array[W] will be true …

And in fact, that won’t match with the client game state and it’s gonna be unplayable

Do you have any ideas, examples, documentation on a similar game ?

I want to implement it in such way that its gonna be performant, efficient and scalable…

I’m using node js & socket.io to achieve this

Thank you guys !

There are many ways to accomplish this and it kind of depends on the architecture you’re aiming for.

A short list of things to consider:

  • Work with ticks rather than frames to handle game events (player input is one of them). The tick-rate should be the same across all clients and the server. Using framerate for this must be avoided, since every client will have different (and fluctuating) framerates. A tick-rate of 30 ticks per second is usually more than enough. Your game code should also have this abstraction. This means that your input handler should not immediately move characters around, but rather encode player input into these same commands that the game also receives from the server.
  • Send commands rather than input. When a player moves forward, encode that into a command, embed the time and interpolate between ticks. Make sure the server is always the authoritative factor, meaning it should do snapshot prediction and broadcast these to all connected clients.
  • Have a deterministic physics engine. This means that when a force/impulse is applied to an object, it should behave exactly the same, every time.

Fabien Sanglard has some excellent books (also available online for free) that explains how ID did their Network Architecture with Quake 3 Arena or DOOM (3).

Isaac Sukin has a very well written guide on game loops in JavaScript and even an open-source library that takes multiplayer games into account.

3 Likes