Body Export Order

General discussion about the R.U.B.E editor
Post Reply
njguy281
Posts: 4
Joined: Sun Oct 12, 2014 12:56 am

Body Export Order

Post by njguy281 »

Is there a way to sort, or rearrange the order in which the bodies are exported? This also effects the draw order. I am aware of the image render order feature but it doesn't work if you create a node system based upon the bodies.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Body Export Order

Post by iforce2d »

No, there is no way to do that as a standard feature because bodies do not have any inherent ordering. Ideally bodies are supposed to be an un-ordered collection - for example, when you kick a ball, there is no objective ordering that says your foot is first and the ball is second somehow. In a software simulation of course the physics engine does process them in the order that they exist in a list, but this is not really intentional or desired, it's just a consequence of having to put them in some kind of data structure and iterate through that container to process them.

I would suggest giving the bodies a custom integer or float property, and use that to sort the bodies after importing. Example for C++, where bodies have a "nodeOrder" custom float property:

Code: Select all

// somewhere in global scope
b2dJson* jsonForSort = NULL;
bool compareBodiesByCustomProperty(const b2dJsonImage* a, const b2dJsonImage* b)
{
    return json->getCustomFloat(a, "nodeOrder") < json->getCustomFloat(b, "nodeOrder");
}

// in your loading routine
string errorMsg;
b2dJson json;
b2World* myWorld = json.readFromFile("myfile.json", errorMsg);

std::vector<b2Body*>& bodies;
json.getAllBodies(bodies);

jsonForSort = &json;
std::sort(bodies.begin(), bodies.end(), compareBodiesByCustomProperty);

// bodies will now be in ascending order by the custom float property "nodeOrder"

I am a bit puzzled about how a node structure could be generated from a flat ordering though, I would have expected using string names as unique identifiers would be more useful. But hopefully this helps answer your question. I have not actually tried that code, let me know if there are any problems.
njguy281
Posts: 4
Joined: Sun Oct 12, 2014 12:56 am

Re: Body Export Order

Post by njguy281 »

Basically, the engine I created loads each body into a node container. These nodes are in a list. On the draw method the list loops through and the first bodies/nodes that were loaded get drawn first, therefore on the bottom of whatever gets drawn next. I may have to rework it.
Post Reply