Playcanvas on Glitch.me

Tl;dr: How can I run playcanvas with physics on a glitch server (with nodejs)?

So I was following the playcanvas multiplayer tutorial (here) where you set up a glitch server to relay player position data. However, I found that calling rigidbody.teleport() all the time to keep all the entities in sync was really slow. I wanted to see if I could run all the playcanvas physics serverside and have each client transmit player input (keypresses). Then, I could take all the physics off the client side and have the server tell all the model components on the client side where to go.

So I tried downloading playcanvas and ammo onto the glitch server and immediately ran into two problems:

  1. Playcanvas seems to require the DOM to run correctly
  2. I don’t know how to “import” the playcanvas code into server.js

Is there a better way of getting physics to be in sync for all the clients? If not, is there any way to run playcanvas physics in a nodejs environment?

1 Like

@devMidgard Ran a ‘headless’ browser for his .io games IIRC. Not sure if it will work on glitch though.

You can also run ammo.js or another physics engine without PlayCanvas on the server as that shouldn’t require a DOM.

1 Like

It’s really interesting subject.

So, you offer simulate physics on server side. Like, send to server all calls for applyImpulse etc
And then send it to all clients to update?

In sum, how would you sync physic’s state between clients, in a nutshell?

1 Like

@yaustar I’d rather avoid using ammo.js directly if possible, I like that playcanvas provides a nice level of abstraction away from it as it seems the documentation for ammo isn’t that great.

@mikefinch Yeah that’s basically the idea, all the physics would happen on the server and all entity positions would be sent to the clients.

1 Like

As ammo.js is compiled C++ code of Bullet, you can use the Bullet documentation (or the PlayCanvas engine source code which is a good reference).

The list of available functions can be found here: https://github.com/playcanvas/ammo.js/blob/master/ammo.idl

Otherwise, you can use another physics engine like cannon.js or oimo.js

Ideally, it would be best if there was a deterministic physics engine with a fixed timestep so it could work on both client and server with corrections every now and then.

Here’s a nice little article on the issues with network latency for multiplayer: https://www.gamasutra.com/blogs/NeemaTeymory/20160906/280377/Why_Making_Multiplayer_Games_is_Hard_Lag_Compensating_Weapons_in_MechWarrior_Online.php

1 Like

It still sounds very trivial.

I believe there should be solution for that. It should work internally, sync states between all the clients.
I don’t think we should write it from scratch.

https://gafferongames.com/post/introduction_to_networked_physics/ (omg very useful )

3 Likes

You can hit me up on Skype midgardh I’ll send you a zip with an example of a websocket server running in nodejs with the PlayCanvas engine included. I did it very dirty but it works and you can have the server running a playcanvas scene, with entities, components, physics, etc. Then just send the entity’s position to the clients. Pretty simple stuff.

2 Likes

@devMidgard
can you post this so i can take a look as well. thanks

@Zeppelin7
Have you gotten very far with this idea? This sounds like a solution that could work.

1 Like

Hey, sure, I’ll set up a github repo this week. Maybe we can work together to get it more well tested and cleaned as my implementation is pretty dirty :’)

3 Likes

Thank you very much @devMidgard. I tried using nodejs but I couldn’t create a pc.Application without a valid canvas, so I tried using JSDOM but got an ‘unsupported browser exception’. Please let me know when you get that github repo set up :slight_smile:.

@yqu In theory it should work, but I still haven’t been able to actually test it. It seems like the only viable solution though, otherwise each client instance will be calculating the physics a little bit off from the others.

Buddy, I would suggest you to run Ammo simulation on sever.
Then just share current state along the clients, and that’s all.

You don’t have to run PC on your server, only physics simulation and other server-like things.

1 Like

To be honest and fair, It’s simply far more intuitive and easy to build mutiplayer games with the same tech on client&server.

PlayCanvas just makes it ultra comfortable to work with. Adding components, creating new entities, handling scenes… it’s just the good shit! And having all of those features on server side is super super handy.

1 Like

Btw guys it’s gonna take me a little longer to set up the github repository, I’ve been feeling sick and not been up to it this week.

1 Like

So I got a little of time to put a simple example gameserver.

Here’s the git: https://github.com/devMidgard/playcanvas-nodejs
Here’s a simple game client: http://launch.playcanvas.com/606346?debug=true

It’s pretty simple to set up and expand. Let me know if you got any more doubts.

1 Like