# Box2D Forums

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

 All times are UTC - 8 hours [ DST ]

 Page 1 of 1 [ 2 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Implementing own gravityPosted: Sat Jan 21, 2012 11:57 pm

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.

Top

 Post subject: Re: Implementing own gravityPosted: Thu Feb 02, 2012 1:11 pm

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

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 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 forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

 Search for: