RUBE save scene after destroying and rebuilding joint

General discussion about the R.U.B.E editor
Post Reply
Voptiplex
Posts: 35
Joined: Thu Nov 14, 2013 1:07 pm

RUBE save scene after destroying and rebuilding joint

Post by Voptiplex »

Hi Chris,

I am using the b2dJson::writeToFile of b2dJson to write out the current world state.
That has worked quite well till now, but now I do get problems after I have destroyed and then rebuild several joints with custom properties.
Basically, I do get the custom properties, as well as the name of the destroyed joint attached to another joint (which I did destroy and rebuild as well). In my case, the mixup seems to happen even across joint type boundaries, so a former weld joint is now a prismatic joint. (Both joint types were part of the destroy and rebuild cycle.)


Example of the mixup:
Original:

Code: Select all


"joint" : 
[

	{
		"anchorA" : 
		{
			"x" : 2.384185791015625e-07,
			"y" : -5.960464477539062e-07
		},
		"anchorB" : 
		{
			"x" : 0.2602210044860840,
			"y" : -0.01068103313446045
		},
		"bodyA" : 9,
		"bodyB" : 28,
		"customProperties" : 
		[
			
			{
				"int" : 16,
				"name" : "custJointNum"
			},
			
			{
				"name" : "constraints",
				"string" : "((E;T;11)|(L;LL;0)|(L;UL;40)|(FR;J;16)|(ME;J;15)|(FR;J;25)|(M;J;80)|(M;J;81)|(M;B;80)|(ML;J;25)|(ST;B;70)|(ST;B;23))"
			},
			
			{
				"name" : "custConnections",
				"string" : "(Connection_1;1;0.032;192.192.192;10;2;1;11;15)"
			}
		],
		"dampingRatio" : 1,
		"frequency" : 0,
		"name" : "MeasurePoint_1",
		"refAngle" : 0,
		"type" : "weld"
	},
After destroy and rebuild cycle involving several joints:

Code: Select all

"joint" : 
[

	{
	   "anchorA" : 
	   {
	      "x" : "3E996200",
	      "y" : "BDFDC780"
	   },
	   "anchorB" : 
	   {
	      "x" : "BBA41B04",
	      "y" : "38B82480"
	   },
	   "bodyA" : 3,
	   "bodyB" : 20,
	   "customProperties" : 
	   [
	      
	      {
	         "int" : 16,
	         "name" : "custJointNum"
	      },
	      
	      {
	         "name" : "constraints",
	         "string" : "((E;T;11)|(L;LL;0)|(L;UL;40)|(FR;J;16)|(ME;J;15)|(FR;J;25)|(M;J;80)|(M;J;81)|(M;B;80)|(ML;J;25)|(ST;B;70)|(ST;B;23))"
	      },
	      
	      {
	         "name" : "custConnections",
	         "string" : "(Connection_1;1;0.032;192.192.192;10;2;1;11;15)"
	      }
	   ],
	   "enableLimit" : true,
	   "enableMotor" : false,
	   "localAxisA" : 
	   {
	      "x" : "3F64FA62",
	      "y" : "3EE4F45E"
	   },
	   "lowerLimit" : "3C23D70A",
	   "maxMotorForce" : 0,
	   "motorSpeed" : 0,
	   "name" : "MeasurePoint_1",
	   "refAngle" : "3FF38222",
	   "type" : "prismatic",
	   "upperLimit" : "3D23D564"
	},



Therefore my questions are:
1) Is the b2dJson::writeToFile functionality at all useful to save a copy of the current b2world ?
2) Is destroying and rebuilding of joints allowed in conjunction with b2dJson::writeToFile ?
3) Is there some housekeeping inside of b2dJson to be done by me to enable the software to keep track of the sutom properties and the joint name after destroying and rebuilding ?

If the answer to point 1) aready is negative, do you have a suggestion on how to save a state of the current b2world with all the custom properties ?

Sorry for the long mail and thanks for your help,
Thomas
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: RUBE save scene after destroying and rebuilding joint

Post by iforce2d »

Presumably you meant to say "I do not get the custom properties"?

The writeToFile function saves the state of a Box2D world, given a b2World* pointer. The Box2D world itself does not have any notion of custom properties, so it does not contain them anywhere.

When you do readFromFile, the custom properties found in the .json are stored separately, using an STL map of pointers to classes which in turn contain some more STL maps of strings to the actual properties (search for "custom" in b2dJson.h to see what I mean).

To save custom properties in items, you can use b2dJson::setCustomInt( void*, int ) etc before calling writeToFile. There are also setCustomFloat, setCustomString, etc functions for each variable type.

For example:

Code: Select all

b2dJson json;
json.setCustomInt( myjoint, 123 );
json.writeToFile( myb2world, "myfile.json" );
Giving NULL as the first parameter to setCustomXXX will set the property in the 'world' element of the .json.

While this should work ok, I get the feeling there might be a better way to do what you're doing...

About the type of the joint being changed, that could perhaps be a bug. Are you using the latest version of b2dJson from github?
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: RUBE save scene after destroying and rebuilding joint

Post by iforce2d »

Sorry, forgot to mention about names for items, which are also stored separately to the b2World in another mapping structure. You can do that in a very similar way, for bodies/fixtures/joints/images:

Code: Select all

b2dJson json;
json.setBodyName( mybody, "mybodyname" );
json.writeToFile( myb2world, "myfile.json" );
Check b2dJson.h for the exact names of these functions.
Voptiplex
Posts: 35
Joined: Thu Nov 14, 2013 1:07 pm

Re: RUBE save scene after destroying and rebuilding joint

Post by Voptiplex »

Hi Chris,

thanks for your help, it is quite some work, but I move all the custom properties now from the old joint to the new joint.

Thanks,
Thomas
Post Reply