Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<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>
 

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