Box2D Forums

It is currently Sun May 19, 2013 8:50 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Implementing own gravity
PostPosted: Sat Jan 21, 2012 11:57 pm 
Offline

Joined: Thu Jan 12, 2012 11:45 am
Posts: 3
I have gravity needs that are better served through per-object gravity (beyond the supported gravity scale), so I am attempting to implement gravity myself. The obvious thing to do is to apply a force to each b2_dynamicBody each frame. Unfortunately, bodies with higher mass looked like they were dragging through the air.

I traced it down to b2Island::Solve's velocity integration:
Quote:
v += h * (b->m_gravityScale * gravity + b->m_invMass * b->m_force);


So then, gravity is not scaled by 1/mass like the rest of the forces being applied to the body. My solution is to counteract the effect of the m_invMass by prescaling the gravity force.

The loop looks like this:

Quote:
b2Vec2 gravityVector( ... );

// Apply gravity equally to each rigid body.
for ( b2Body *b = world->GetBodyList(); b; b = b->GetNext() )
{
if ( b->GetType() != b2_dynamicBody )
continue;

float32 invMass = 1 / b->GetMass();

b2Vec2 g( gravityVector.x / invMass, gravityVector.y / invMass );
b->ApplyForceToCenter( g );
}


Another problem is that calling ApplyForceToCenter() calls SetAwake(). This means no dynamic bodies are ever resting. I am going to look into coding a workaround to this.

Any suggestion on how to avoid all of this useless computation just to take control over the gravity processing is welcome.

Michael Labbé


Top
 Profile  
 
PostPosted: Thu Feb 02, 2012 1:11 pm 
Offline
Site Admin

Joined: Thu Sep 06, 2007 12:34 am
Posts: 2931
You can use b2BodyDef::gravityScale.


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

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


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