Box2D Forums

It is currently Sat May 25, 2013 3:50 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Wed Aug 05, 2009 5:29 pm 
Offline

Joined: Sun May 24, 2009 7:43 am
Posts: 107
Location: Quebec, Canada
I converted my code to use revision 228. I'm having some trouble with body creation and the center of mass.

Before, I used :
Code:
    mBody->SetPosition(b2Vec2(mShip->getPosition().x, mShip->getPosition().y));


The SetPosition function is no longer there, so I now use :
Code:
    lBodyDef.position.x = mShip->getPosition().x;
    lBodyDef.position.y = mShip->getPosition().y;


When I call SetMassFromShapes(), this sets the center of mass in world space instead of local space. Maybe it has always been that way and since I was setting the body's position after SetMassFromShapes(), I never saw it. This causes my ships to move in wide circles when I apply torque since their center of mass is outside the body.

Is this the intended behaviour? I could always substract the position from the center of mass, but it doesn't seem right to me.


Top
 Profile  
 
PostPosted: Thu Aug 06, 2009 1:12 am 
Offline
Site Admin

Joined: Thu Sep 06, 2007 12:34 am
Posts: 2931
When you set the position, you are not setting the position of the center of mass. You are setting the position of the body origin.

body position == body origin


Top
 Profile  
 
PostPosted: Thu Aug 06, 2009 6:16 am 
Offline

Joined: Sun May 24, 2009 7:43 am
Posts: 107
Location: Quebec, Canada
Yeah this is what I was expecting, but apparently it's not the case. I tried to quickly reproduce it in the TestBed, but with no success. A bug might have crept in when I changed the code for the new version.

Thanks!


Top
 Profile  
 
PostPosted: Thu Aug 06, 2009 6:57 pm 
Offline

Joined: Sun May 24, 2009 7:43 am
Posts: 107
Location: Quebec, Canada
I found the problem. This is caused when I reset the mass data after adjusting the calculated value based on the actual ship's mass. Here's a working example for the TestBed :

Code:
            b2FixtureDef lFixtureDef;
               
            b2PolygonShape lShape;
            lFixtureDef.shape = &lShape;
           
            lFixtureDef.isSensor = false;

            lFixtureDef.density = 1.0f;
           
            b2Vec2 lVertices[4];
           
            lVertices[0].x = 2.0f;
            lVertices[0].y = 2.0f;
           
            lVertices[1].x = -2.0f;
            lVertices[1].y = 2.0f;
           
            lVertices[2].x = -2.0f;
            lVertices[2].y = -2.0f;
           
            lVertices[3].x = 2.0f;
            lVertices[3].y = -2.0f;
           
            lShape.Set(lVertices, 4);

            b2BodyDef bd;
            bd.position.Set(0.0f, 40.0f);
            b2Body* body = m_world->CreateBody(&bd);
            body->CreateFixture(&lFixtureDef);
            body->SetMassFromShapes();
           
            b2MassData lMassData = body->GetMassData();
           
            body->SetMassData(&lMassData);

            body->ApplyTorque(500.0f);


If I remove
Code:
            b2MassData lMassData = body->GetMassData();
           
            body->SetMassData(&lMassData);


Then it works as it should. I'm not sure why this fails now though as I can't seem to find any difference in the code...


Top
 Profile  
 
PostPosted: Fri Aug 07, 2009 4:49 am 
Offline

Joined: Sun May 24, 2009 7:43 am
Posts: 107
Location: Quebec, Canada
Tiblanc wrote:
Then it works as it should. I'm not sure why this fails now though as I can't seem to find any difference in the code...


I guess I was too tired to figure out that the reason was not a change of code in Box2D, but a change in my code. It's because I need to set the position before setting the mass now, which is why it now fails. Doing the same test in an older version of Box2D fails. So, should GetMassData() return the mass center in local or world space? Right now, it's in world space.


Top
 Profile  
 
PostPosted: Fri Aug 07, 2009 10:31 am 
Offline
Site Admin

Joined: Thu Sep 06, 2007 12:34 am
Posts: 2931
It should return the center in local space.

Change this line:
Code:
   massData.center = GetWorldCenter();


To:
Code:
   massData.center = m_sweep.localCenter;


Top
 Profile  
 
PostPosted: Fri Aug 07, 2009 7:44 pm 
Offline

Joined: Sun May 24, 2009 7:43 am
Posts: 107
Location: Quebec, Canada
That seems to work. I also had to tweak my thrust/turn function to convert the center of mass to world coordinates for it to work properly. Is this something that will become permanent in a future version?

Thanks!


Top
 Profile  
 
PostPosted: Sat Aug 08, 2009 12:32 am 
Offline
Site Admin

Joined: Thu Sep 06, 2007 12:34 am
Posts: 2931
Yeah, it was a bug. I'll fix it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group