Get body by name

General discussion about the R.U.B.E editor
Oscar
Posts: 14
Joined: Sun Dec 23, 2012 3:56 pm

Get body by name

Post by Oscar »

Hi,

First off all, thanks for a wonderful editor. It's already proven to be really useful (like the name suggests) for my game development :)

I've run into my first question: I think I read somewhere pre-release date that you'd be able to select a body by the name you gave it in Rube. I want to select the "foot" body for my rag doll character. I'm not sure if this is only possible by parsing json or if it's possible by copy pasting C++ code, (which I am doing).

Thanks.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Get body by name

Post by iforce2d »

Hi Oscar

The C++ code export uses the standard 'dump' feature that is in the Box2D source code, so it only recreates the Box2D world itself, and does not know about any of the extra metadata related to each body/fixture etc. To store all the related information for the items requires a much larger set of name maps etc which the JSON format holds. Also, the C++ export does not contain any information about images.

The C++ export was really only added as an afterthought, because it was already available and quite easy to add, and could perhaps be useful for somebody, but the recommended way of loading the scene is to load the JSON file. For C++ you can just add a handful of files to your project to have JSON parsing support, look for the 'rubestuff' folder in the box2d-testbed-(C++) sample loader (under src/Box2D/Testbed/Tests). For an example of using these, check out the loadrube.h test which loads the tank with images scene.

The overhead of this initial setup is not too great, but it gives you the huge advantage of being able to have your game and the editor running side-by-side, and you can edit the scene in the editor, and then load the scene in your game to test it in a few seconds. I think it would give you a much more efficient workflow than exporting to C++ source code, pasting, compiling, running etc.
Oscar
Posts: 14
Joined: Sun Dec 23, 2012 3:56 pm

Re: Get body by name

Post by Oscar »

I understand there are limits of merely copy pasting C++ code, it makes sense.

I do make the actual "levels" by parsing json though. The reason I wanted to use C++ code was to add some rag doll functionality to my "player" class, it seemed as the simplest way of doing it. I didn't realize you could choose to not include the world settings etc (those should be level specific) in a separate json file. I assume this is what you're suggesting, am I right?
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Get body by name

Post by iforce2d »

Currently the json file is always an entire world. It sounds like you want to have your player ragdoll defined separately to the level file so that you can load it at any time later, or load the same ragdoll into different levels without having to actually save it as part of each level?

The concept of having a group of bodies and joints as a coherent and instantiable object is something that will hopefully be added in a future version of RUBE. For now, what could be done is to slightly re-arrange the loading function in b2dJson, to take an already existing world and put the loaded bodies and joints into it. So instead of the current:

Code: Select all

b2World* b2dJson::readFromFile(const char* filename, std::string& errorMsg)
...you would have something like this:

Code: Select all

bool b2dJson::loadIntoExistingWorld(b2World* world, const char* filename, std::string& errorMsg)
You would load your level as normal to get a b2World* to work with, and then pass that to this new function to add the ragdoll from another file. I think you could use the same b2dJson instance, as long as the names did not collide it should work out ok. Bear in mind that you would probably have to move the ragdoll bodies to the right place after loading.

The full function is attached - I checked that it compiles, but I did not check that it works :) Let me know if there are any problems and I will check it out properly. Or, if I have completely misunderstood what you're trying to do, let me know about that too :D
Attachments
loadFromExistingWorld.zip
(764 Bytes) Downloaded 1017 times
Oscar
Posts: 14
Joined: Sun Dec 23, 2012 3:56 pm

Re: Get body by name

Post by Oscar »

You're spot on correct about what I'm trying to do :)

Thanks a lot, I'll try the code out for sure! That kind of function was exactly what I was looking for.
Oscar
Posts: 14
Joined: Sun Dec 23, 2012 3:56 pm

Re: Get body by name

Post by Oscar »

I was about to test your code but ran into some trouble. in b2dJson.cpp I pasted your attached function but I seem to get some xcode issues.

I assume I have to declare some functions and vars in the .h file as well? my C++ knowledge is low. I use Objective-C++ for my iPhone game.

in b2dJson.h:
Tried this to declare the function:

Code: Select all

bool loadIntoExistingWorld(b2World* world, const char* filename, std::string& errorMsg);
Seemed to work fine. But I now have a few errors, you can view them in my screenshot.

Thanks for the help
Attachments
json-parsing.jpg
json-parsing.jpg (168.57 KiB) Viewed 26002 times
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Get body by name

Post by iforce2d »

Yes you would need to add a declaration in the header like that.
I'm not sure why those errors would happen.... would you be able to attach/pastebin the b2dJson.h file?
metalbass_92
Posts: 18
Joined: Wed Dec 26, 2012 12:27 pm

Re: Get body by name

Post by metalbass_92 »

As I can see in the image, the method cannot access fields in the class instance.
Maybe you forgot to write the name of the class before the name of the method in the cpp file?

a method in a cpp class can be decared this way:
ExampleClass.h:

Code: Select all

#ifndef __EXAMPLECLASS_H__
#define __EXAMPLECLASS_H__

class ExampleClass
{
public:
    void exampleMethod();
};

#endif
ExampleClass.cpp:

Code: Select all

#include "ExampleClass.h"

void ExampleClass::exampleMethod()
{
    // Implementation
}
Xavier Arias
Cocos2d-x Game Programmer
Oscar
Posts: 14
Joined: Sun Dec 23, 2012 3:56 pm

Re: Get body by name

Post by Oscar »

I've attached how the .cpp and .h looks for me at the moment with errors.

@metalbass_92 – I'm not sure. I thought just pasted it to be one of the functions within the existing class.
Attachments
b2djson.zip
(9.4 KiB) Downloaded 888 times
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Get body by name

Post by iforce2d »

It looks like you are using the source code from this page: http://www.iforce2d.net/b2djson/ which only handles the JSON produced by the older free version of RUBE which does not know anything about images.

Use the b2dJson sources included with the RUBE download, they include the support for images. Look for the 'rubestuff' folder in the box2d-testbed-(C++) sample loader (under src/Box2D/Testbed/Tests). For an example of using these, check out the loadrube.h test which loads the tank with images scene.

Sorry that this is a bit confusing. I am working on custom properties now, and this will cause another update to the b2dJson sources, to load the custom properties. After that maybe I will update the download links so that all sources are the same (although, for the free version it will be a lot of redundant code).
Post Reply