I'm working on a platformer where I'd like to be able to move or copy bodies between multiple Box2D b2Worlds. The desire has cropped up three times now - moving objects between documents in my editor, moving the player between levels in normal game play, and giving the player a preview of a non-trivial projectile they can shoot in the game, which seems to be most accurately done by simulating the world for a few seconds ahead.

It looks like I can make a parallel world the "long way" - for each body in the world, copy the body data back into a def and re-create it, then copy the fixtures on it in the same way, then do the same for joints; re-set all the velocities; and if it's a "move" rather than a "copy" delete the original one.

I was wondering if someone has already written this code (and if so, was it actually a workable design or not?), or if Box2D has some affordance for doing this within its API which I am missing.

  • \$\begingroup\$ Please edit your posting to be a question. \$\endgroup\$ – topright Sep 28 '10 at 18:08
  • 2
    \$\begingroup\$ @Pavel He actually does ask a question even though he is lacking a question mark. \$\endgroup\$ – Jesse Dorsey Sep 28 '10 at 19:52
  • \$\begingroup\$ At least to me it wasn't clear what he was asking for. \$\endgroup\$ – topright Sep 28 '10 at 20:05
  • \$\begingroup\$ "if someone has already written this code" Someone can answer "Yes" to the wondering of Joe Wreschnig and it will be legal answer. But I suppose he doesn't going to appreciated that. \$\endgroup\$ – topright Sep 28 '10 at 20:09
  • 2
    \$\begingroup\$ @Pavel Yes, I am sure he expected someone to explain what made it a workable design or not even if he didn't ask for it explicitly. As an answer of "Yes" would be downvoted more than likely. Also, being petty helps no one. \$\endgroup\$ – Jesse Dorsey Sep 28 '10 at 21:20

Thanks for reminding me about this.

The answer, is no, you can't do this in stock Box2D. Joints don't have enough accessors to get back a correct JointDef. You can back-def-ify simple bodies, but nothing jointed, and the simple bodies are usually trivial enough you wouldn't need to do a copy of the world to predict their motion accurately anyway.


Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.