liabru / matter-js

a 2D rigid body physics engine for the web ▲● ■
MIT License
16.86k stars 1.97k forks source link

Networking multiplayer game (determinstic lockstep) #1040

Closed ZeroTixDev closed 2 years ago

ZeroTixDev commented 3 years ago

Im trying to make a multiplayer game with determinstic lockstep using the matterjs physics engine. For determinstic lockstep, I need to take frequent snapshots/copies of the world that I can rollback to at any time. The problem is copying the world efficiently.

(Also I mean determinstic lockstep as in each client running their own physics and the server exchanging the inputs among players. Everytime theres a phyiscs update, a tick variable goes up. When the client sends an input, they send { input, tick }. So when other client receive the input, they go back in time to the input tick and integrate the input into the game. Otherwise known as asynchronous determinstic lockstep [i think])

(Btw, I did this before without using a physics engine and just did a simple deepcopy function which was pretty fast). I've looked on the internet and couldn't find any efficient ways to clone the matterjs world. I did try to serialize the game and deserialize. However, when I did serialize, the string was so long even with just a few objects (around 40k chars) and took around 10ms to serialize/deserialize which was unacceptable. (serializing code from matterjs-tools serializer using resurrectjs). I tried to deepcopy engine.world or each body in engine.world.bodies but there were 3 circular properties in each of them. body.parts, body.vertices, body.parent!! So how should I efficiently clone the world? If this is not possible, I will be very sad D:

ZeroTixDev commented 3 years ago

Is anyone going to answer?

liabru commented 2 years ago

Multiplayer is out of scope but has been discussed multiple times so you take a look over past issues on the topic.