How to extract/create a JointDef based off a b2Joint instantiation
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:
switch ( joint->GetType() )
{
case e_revoluteJoint:
{
b2RevoluteJoint* revoluteJoint = (b2RevoluteJoint*)joint;
...
}
}
A few things to note though:
- 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)
- 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
- 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
- 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
- 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
精彩评论