schteppe / p2.js

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

World state serialisation? #226

Closed josephrocca closed 8 years ago

josephrocca commented 8 years ago

Does p2.js support serialisation of world state? I couldn't find any details on saving world state anywhere.

If it is not supported, is there a recommended approach to this? I'm trying to save the world so the user can reset it back to the original state.

I believe matter.js supports JSON serialisation with resurrect.js. Could the same method be applied to p2.js?

Thanks!

josephrocca commented 8 years ago

I just read on your cannonjs issues page (an old issue submitted by me as it turns out!) that you did have this at one point in p2.js but took it out - is that implementation somewhere in the archives of github still?

I want to save the whole world state, but could roughly work around the problem by just storing each existing object state for now. My attempts at this aren't going to well. I'm storing these values and then setting them when I want to "load" the previous state:

body.position[0]
body.position[1]
body.interpolatedPosition[0]
body.interpolatedPosition[1]
body.angle
body.interpolatedAngle
body.previousAngle
body.velocity[0]
body.velocity[1]
body.vlambda[0]
body.vlambda[1]
body.force[0]
body.force[1]
body.angularForce
body.angularVelocity
body.invInertiaSolve
body.invMassSolve
body.idleTime
body.interpolatedAngle
body.wlambda
body.sleepState
body.timeLastSleepy
body.wantsToSleep

It seems to work for a couple of save/loads but then weird stuff happens (objects lose their bounciness, for example). Any ideas?

schteppe commented 8 years ago

Hi, Not any more. I realized that it's out of the scope for the project to support a serializer.

Maybe have a look at the physicstoy project? Here's the BodyHandler, that instantiates and updates a Body from serialized data: https://github.com/schteppe/physicstoy/blob/master/public/js/handlers/BodyHandler.js

Do you trash everything before rebuilding the scene? Or do you try to restore existing instances?

How about the shapes, contact materials and materials?

I never tried resurrect.js but it looks cool!

josephrocca commented 8 years ago

I was trying to restore existing instances, but don't really know what I'm doing (didn't consider that the state of anything but the bodies could change).

I've had a shot at using resurrect.js and it looks amazingly simple. I just need to give it access to all the p2 constructors. Any idea how I can get a reference to constructors like TupleDictionary that aren't exported? If ressurect ends up working well it'd be great to see it as part of p2! Otherwise a guide in the wiki would be great and I'd be more than happy to provide that once I get it working.

Thanks!

schteppe commented 8 years ago

The simplest way is probably just to pick a few things that you want to persist, and just scrap and recreate all instances when you need to restore. Make it super simple, with pure functions :)

// create a new instance given a config
var world = createWorld(config);
josephrocca commented 8 years ago

True! Silly of me to try and restore existing instances earlier. I guess the only reason I'd love to see world.toJSON()/world.fromJSON() is for simple projects where the createWorld() boilerplate can seem like a bit of a drag. I totally agree though that any reasonably sized project would want to spin their own flavour of serialisation, so I can see why you decided to take it out.

Thanks again for your help - you're always so responsive and supportive with those who use your projects. I really appreciate that! :)