pyBox2D Json loader

General discussion about the R.U.B.E editor
Post Reply
c0ffee
Posts: 4
Joined: Fri Aug 16, 2013 11:19 am

pyBox2D Json loader

Post by c0ffee »

Update:
Link to script https://github.com/c0ffeeartc/rubeJsonToPyBox2D
Check "readme" for usage. :)

Previous post:
Can someone with experience specify what steps would take to create a python Box2D json loader?
I'm used to python and would be able to make one if it's within reach of my knowledge.
So far I'm seeing it as creating pyBox2d to json converter to specify content. And then it would be either conversion of C++ json file to python json or some else.
Last edited by c0ffee on Fri Sep 06, 2013 7:49 pm, edited 1 time in total.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: pyBox2D Json loader

Post by iforce2d »

JSON is just text, so there is no C++ JSON or python JSON. For example, this is JSON:

Code: Select all

{
    "name" : "chris"
}
What you need is something to read in the text file, and hold it in memory. For example with the 'jsoncpp' library used in the C++ sample loaders you would do something like this:

Code: Select all

Json::Value bodyValue; // this will hold the file data in memory
Json::Reader reader; // used to read and parse the file

std::ifstream ifs; //input stream
ifs.open( "myfile.json", std::ios::in ); //open the file
reader.parse( ifs, worldValue ); //read the file contents and parse it into memory
ifs.close();

//now you can use the data, eg:
string name = bodyValue.get("name"); // returns 'chris'
Then you would use the data to recreate the Box2D world. Of course, you're dealing with a much more complex JSON file than this example, but the process is the same. I have not used python much to help with it, but I think the first thing you need to do is google "python json parser", to find something to do the parsing step.
c0ffee
Posts: 4
Joined: Fri Aug 16, 2013 11:19 am

Re: pyBox2D Json loader

Post by c0ffee »

Hello, Box2D json loads into memory fine in python, yet for example pyBox2D.b2World object has different set of variables compared to Box2D.b2World.
Then you would use the data to recreate the Box2D world.
So remapping values straight from Box2d json data to pyBox2D fields would be a usual practice, or might there be a tried and better way to get pyBox2D world running.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: pyBox2D Json loader

Post by iforce2d »

Right. Writing a loader is mostly a process of mapping the fields needed to recreate the scene using the target language. To be honest it's a pretty boring job, which I think is why people tend to wait for somebody else to do it :D If I was experienced in Python I might have done it myself by now, but I'm placing priority on languages and frameworks that I actually know, and adding features to the editor itself. There's also a good chance that things like this are better done by people who know the target (eg. pyBox2D) in more detail than I do anyway.
c0ffee
Posts: 4
Joined: Fri Aug 16, 2013 11:19 am

Re: pyBox2D Json loader

Post by c0ffee »

Hello, the link to script is https://github.com/c0ffeeartc/rubeJsonToPyBox2D
All is done and should work well, everyone is welcome to try it :)

Check "readme" for usage.
Post Reply