schteppe / p2.js

JavaScript 2D physics library
Other
2.63k stars 330 forks source link

p2.js Multiplayer Game issue #357

Open ghost opened 4 years ago

ghost commented 4 years ago

I was trying to make a game with socket.io and p2.js. I was thinking of sending the world instance to the client from the server, then having the client render it. I wasn't able to send the instance of world to the client because the object has a circular structure. Any idea of how to tackle this issue?

ghost commented 4 years ago

Nevermind, I made a custom flattening script. Now my server can't do world.step(1/30) because it returns a maximum call stack reached error.

Fxlr8 commented 4 years ago

Sending the whole world state every tick is extremely inefficient and practically useless. Try to send as little information as possible. For example, only send positions of the objects that move. Then apply this updates to the client's state. Game networking is a complex task

XWILKINX commented 3 years ago

I use Socket.IO and P2.js for my real-time multiplayer game Deadswitch 3.

It's not possible to emit the world instance to a client (hence the maximum call stack error) due to circular dependencies.

Instead, simply send any position/velocity/data changes that occurred. I recommend sending this at a interval, not every frame. Personally I send data updates 5 times a second, and let the client handle interpolation.

Let me know if you have any other questions!