How to modify the original position of body safely?

General discussion about the R.U.B.E editor
Post Reply
sijieshuai
Posts: 13
Joined: Tue May 14, 2013 1:51 pm

How to modify the original position of body safely?

Post by sijieshuai »

Excuse me, I'm back again. :D

The general steps to create a game world by R.U.B.E, I think, are setup every thing in R.U.B.E scene and import them to customer's project. Now my requirement is, for example, create a character A(A contains anything, such as bodies, fixtures, images and joints) in R.U.B.E scene at position x0, when I load the corresponding JSON file into my project, I want to translate A to some other position by a vector t, that is x0 + t position, and I want to create A at x0 + t, not create it at x0 and translate it to x0 + t. In other words, I want see R.U.B.E coordinate system as a local coordinate system, and my project's scene coordinate system is the world coordinate system. So, if it is possible, how to modify the code safety?
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: How to modify the original position of body safely?

Post by iforce2d »

You could do something like this:

Code: Select all

json.readFromFile( "enemy.json", errorMsg, world );

vector<b2Body*> allBodies;
json.getAllBodies( allBodies ); // all bodies in enemy.json

b2Vec2 offset( x, y );
for (int i = 0; i < allBodies.size(); i++) {
    b2Body* b = allBodies[i];
    b->SetTransform( b->GetPosition() + offset, b->GetAngle() );
}
While you could also get bodies by name, custom property etc., and offset some and not others, I think it would be easier to manage if each file was considered to be a full object for which all bodies will be offset. Also, if there are joints, all bodies connected by joints will need to be moved by the same offset to keep them from going crazy when the simulation starts.

In a future update, I'm planning to allow "instantiable objects", which is pretty much what you're talking about. These threads might be of interest to you too:
viewtopic.php?f=8&t=23
viewtopic.php?f=6&t=11
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: How to modify the original position of body safely?

Post by iforce2d »

wtf...?? There is no getAllBodies function?? hm... I will have to update that I suppose. In the meantime, you could add this to b2dJson:

Code: Select all

int b2dJson::getAllBodies(vector<b2Body*> &bodies)
{
    bodies.insert( bodies.begin(), m_bodies.begin(), m_bodies.end() );
    return bodies.size();
}
sijieshuai
Posts: 13
Joined: Tue May 14, 2013 1:51 pm

Re: How to modify the original position of body safely?

Post by sijieshuai »

I am sorry, maybe you misunderstood me because of my incorrect description. Actually, I want to let JSON loader create the bodies at x0 + t, not create at x0 and then translates them to x0 + t, Because "create and translate" is low performance.

Can this one works safety?:
bodyDef.position = jsonToVec("position", bodyValue) + t;
//at b2Body* b2dJson::j2b2Body(b2World* world, Json::Value bodyValue)
//atb2djson.cpp
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: How to modify the original position of body safely?

Post by iforce2d »

Yes, that would work too, and it will be bit faster (if you have a huge number of fixtures on the body).
Post Reply