I’m trying to serialize a b2World, and due to Box2D’s many private objects that are used, made, and deleted internally, the issue becomes much more complicated. I decided that i should use these internal objects get() functions to get the data i need, and create a “b2Definition” object based off it at save time, and use the definition with the global factories “create” methods to recreate the objects.
I have encountered a few problems i need help on however: In the following code is all the JointDef data i need, but can’t figure out how to get from the pointer to the correct type of object.
I also am wondering if i should even attempt to save contact data…. Is it automatically made in the factory create metods?
b2WeldJointDef JointDef; //QUESTION: how do i get this
//JointDef.referenceAngle= Joint-> ?????
b2GearJointDef JointDef; //QUESTION: how do i get these
//JointDef.joint1= Joint-> ??????
//JointDef.joint2= Joint-> ??????
b2LineJointDef JointDef; //QUESTION: how do i get these??
//JointDef.localAxisA= Joint-> ????
//JointDef.lowerTranslation= Joint-> ????
//JointDef.upperTranslation= Joint-> ????
b2MouseJointDef JointDef; //No problems
b2PrismaticJointDef JointDef; //QUESTION: how do i get these??
//JointDef.referenceAngle= Joint-> ????
//JointDef.localAxis1= Joint-> ????
//JointDef.lowerTranslation= Joint-> ???? //JointDef.upperTranslation= Joint-> ????
//JointDef.maxMotorForce= Joint-> ????
b2PulleyJointDef JointDef; //QUESTION: how do i get these?
//JointDef.maxLengthA= Joint-> ????
//JointDef.maxLengthB= Joint-> ????
b2RevoluteJointDef JointDef; //QUESTION: how do i get these?
//JointDef.maxMotorTorque= Joint-> ????
//JointDef.referenceAngle Joint-> ????
//JointDef.lowerAngle= Joint-> ????
//JointDef.upperAngle= Joint-> ????
b2JointDef JointDef;
//JointDef.collideConnected= ????
Do i need the data above? Is there a way to get it?
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 http://www.iforce2d.net/b2djson Scroll towards the bottom and you can see the source code, look at the function b2dJson::b2j(b2Joint* joint)
It’s not complicated, you just need to check the joint type and cast to a pointer of that type to access the contents:
A few things to note though: