Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I did a lot of this recently to make an export/import utility to serialize a Box2D world to JSON and then load it again. You may find the source code useful - check out <a href="http://www.iforce2d.net/b2djson" rel="nofollow">http://www.iforce2d.net/b2djson</a> Scroll towards the bottom and you can see the source code, look at the function b2dJson::b2j(b2Joint* joint)</p> <p>It's not complicated, you just need to check the joint type and cast to a pointer of that type to access the contents:</p> <pre><code>switch ( joint-&gt;GetType() ) { case e_revoluteJoint: { b2RevoluteJoint* revoluteJoint = (b2RevoluteJoint*)joint; ... } } </code></pre> <p>A few things to note though:</p> <ul> <li>you will need to change the Box2D code itself to add some extra GetXXX() functions to the joints to get all the necessary info. I put a list of them on that page too so you can check what these were for my case (I was using the most recent svn)</li> <li>joints hold a memory pointer to the bodies they join which is useless to serialize, so you will first need to serialize the bodies in the world, then give the joints an index in that list</li> <li>joint defs take a reference angle but joints do not hold this after they are created, so you need to use the body angles and the joint angle to calculate what it was</li> <li>gear joints join two other joints and these are held as memory pointers, so to recreate these you will need to first serialize all non-gear joints, then give the gear joints an index in that list</li> <li>recently gear joints were changed so that they no longer keep the pointers to the two other joints they control, so you will need to add them back if you want to support gear joints with the most recent svn</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. COThere's no way to avoid changing source code? and looking at point 2, doesn't boost::serialize basically do this already? I remember documentation saying something about saving what the ptr points to (a body), which in my case is serialized before the ptr. Do i still need to do this? and does this tip apply to all pointers in box2D? such as b2ContactEdge and b2JointEdge?
      singulars
    2. CORight. Box2D was not really made with this kind of feature in mind, it's just lucky that there are not so many changes necessary. I don't know anything about boost I'm afraid (ah, just took a quick look now) - even if the 'deep pointer save' worked out, I doubt it would give you a working Box2D world and it raises issues about what to do with user data, and how new versions of Box2D could be supported. Unless I'm mistaken you need to add a function void serialize(Archive & ar, const unsigned int version) to everything you want to serialize anyway, requiring changing way more changing of Box2D.
      singulars
    3. COoh sorry... there is a non-intrusive version too, and it seems to take care of duplicate pointers. I'm pretty sure you will not get working world without using the proper CreateBody method though, which means after deserialization you still need to get the data in your first post somehow. I'm not sure why you would want to save contact data... when you add a body to the world this info will be automatically updated. Basically you want to recreate the world in the same way you initially created it, that is with CreateBody, CreateFixture etc. Other internals like b2ContactEdge can be left out.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload