Creating an authoritative server logic for fps physics

Hello

I am experimenting in creating an authoritative server logic with the first person movement script. I am using the ws library to communicate between playcanvas client and a node server. I have some questions when implementing this movement logic to be simulated on the server.

How is it possible to implement this the correct way? Do I simulate the physics on the server side by installing ammojs on the server side? I am assuming moving the rigidbody and detecting collisions should all be done on the server side if so how do I move the player camera to match that movement as like in the first person movement script.

Thanks

Hi @nasjarta,

Yes, ideally you will be running a physics simulation on both the clients and the server, if you have the resources to do that.

On the client the simulation will be used to predict the movement of bodies to avoid showing objects lagging.

On the server you will receive the controller input from each client and simulate all bodies. Then the server will send the final, master, state to each connected client.

The clients are responsible for reconciling their current state with the final state they received from the server. Usually that involves rewinding back to find if their predictive state is in accordance with the server.

This blog has a lot of useful articles on the topic:

2 Likes

@moka and his team have created library to run PlayCanvas on a server so it could be used as an authoritative server GitHub - meta-space-org/playnetwork: Multiplayer server for writing your PlayCanvas based project with authoritative logic, running PlayCanvas engine on the back-end.

This also includes a 3D physics example too.

Alternatively, you could use Colyseus or Photon to run the backend.

When it comes to physics, it’s tricky to get it right over a multiplayer situation as it requires it to be deterministic or for the game to be okay with a little inaccuracy. Many games don’t use ‘true’ physics and rely on nav meshes and basic collision detection to make this easier.

You could also use other physics libraries to make this easier such as Cannon.js (open source and modifiable) or Rapier (deterministic but would require you to compile to WASM to use in PlayCanvas)

As mentioned by Leonidas above, you would have to simulate on both the client and server and check if they are correct/valid based on timestamps.

Some more talks on the subject:

3 Likes

As always thanks for your responses Leonidas and yaustar this is really overwhelming for me as a beginner but I trying to understand slowly the concepts of network programming. Thanks for the tips and resources :smiley:

The library to run PlayCanvas server looks really cool so I wanted to test it however I could not get the 3D example of it to work after following the readme and running the server. In the client I get this error message:

Probably best to post an issue on their repo. It wasn’t written by us so we can’t really help :sweat_smile:

1 Like