piqnt / planck.js

2D JavaScript Physics Engine
http://piqnt.com/planck.js/
MIT License
4.87k stars 236 forks source link

pointers on how to network planck #272

Closed mreinstein closed 7 months ago

mreinstein commented 7 months ago

I'd like to run planck across a few clients, using an authoritative server model. I understand how to do this in terms of the basics (fixing the time step, keeping the simulation deterministic, etc.)

I'm not sure how I would serialize/deserlialize planck's internals for each fixed step. Is there an API I can use for this? Or something higher level, where someone has attempted this already.

mreinstein commented 7 months ago

Here's a list of things so far that seem like might be sufficient to sync a physics object's state:

Are there any other things that might need to be sync'd? I'm not sure how I'd go about syncing a joint constraint for example

zOadT commented 7 months ago

Hey! There is a serializer in src/serializer already. It isn't optimized for file size, but you can probably follow it for your implementation

mreinstein commented 7 months ago

Ah, thanks for the pointer! So it seems this serializer is just enumerating all the object/array props, rather than a hardcoded list.

zOadT commented 7 months ago

I don't think it just enumerates all props, see for example _serialize in Body.ts. Every class defines what properties are relevant this way (so it is hardcoded per class)

mreinstein commented 7 months ago

Oops! I completely misinterpreted what was going on in src/serializer.ts. Thanks again, this is super helpful!

shakiba commented 7 months ago

Current serializer is optimized for saving/loading a world in a readable format, and is not optimized for fast and efficient networking.

For networking I recommend directly iterating over all objects and fields and write to and read from an efficient stream.

mreinstein commented 7 months ago

For networking I recommend directly iterating over all objects and fields and write to and read from an efficient stream.

That's the plan, I just didn't have any list of the "important" fields that one might need to sync for different primitives (fixtures, joints, etc.)

shakiba commented 7 months ago

Hmm, I'm not quite sure, but happy to help with implementation.

We probably need to implement an interface like current serialize but write to a streaming interface (instead of json).

We also need to find out the answer to your question, probably we can see how this is implemented in C++ for Box2D.