Getting started: Multiplayer

Hello and sorry for my bad english!

I try to program a little realtime multiplayer 2d game. 2-8 Players moving and shooting, basic stuff.
Here is the current project. Im currently working on this project, dont wonder if something looks strange.

I ran into many questions how to solve problems in server-client communication. But i cant find a nice tutorial how to do it “the right way”. I dont want to make to much wronge desitions on the way. Do someone know a nice tutorial or something?

I start the multiplayer part of my game with this tutorial:
It works fine! But now i have a few question how to go on from here.

Questions like:

  • Do i have to program the same game on my server (for now: glitch server (node .js, socket .io)) and my server calculating if a bullte is hitting someone or not, or can a host user do this job
  • is there a nice way to smooth out clients positions. for now the position updates from the server looks stuttering

I hope everythin was understandable for you and some knows a nice tutorial.


Hi @nordie92 and welcome,

Well you can definitely have each player calculate bullet hitting on his client and propagate that to other players if this isn’t a competitive game or you aren’t concerned with cheating.

Otherwise there is a single way of addressing this: you need to run your game or part of your game logic on the server. And have the server have the final say on each hit.

So you can use a currentPosition and a targetPosition pc.Vec3 objects. The targetPosition will always point to the latest position that arrived from the server.

The currentPosition will lerp using a delta time to the targetPosition. This is the position the player will use each frame. It will be slightly behind the target position and smoothly become equal to the targetPosition.

You can take a look at the default Playcanvas Model Viewer template on how it uses this mechanism to add inertia/smoothness to camera rotation.

Good luck with your multiplayer game, it can be hard from time to time but very much rewarding :slight_smile:

1 Like

Thanks for answering my questions!

Is there a easy way to do this? Is it possible to process parts of the engine behind playcanvas an a server. Like physics and/or the pc namespace? If the answer is to complicated - its fine, dont answer.

Thanks, but even problems seem to be fun on game dev :grin:

I think the engine now supports running on a headless mode in node.js, basically import it using npm and you can then access the API:

const playcanvas = require("playcanvas");

You can study how the start.js script loads a Playcanvas project when you export it from the editor. And similarly load your scene and run it in your Node.js side. Of course you can omit running certain parts like executing the renderer per frame, so it’s best to study how the Playcanvas app boots and take it from there: