Monday, March 26, 2012

Box2d Apply Gravity to One Object

Often times when making a game certain objects need to weigh more than others.  You can try this with the density property of a b2Body, but the most effective way is to have that object have it's own gravity rules.  To do this, it's pretty simple.  In you world's step function, just apply a force to the object in question.

For example:

m_body_with_custom_gravity->applyForce(CUSTOM_GRAVITY * m_body_with_custom_gravity->getMass())

Where CUSTOM_GRAVITY is a b2Vec2 similar to how the world's inital gravity was set.  It's also important to multiply it by the body's mass.

Source: http://stackoverflow.com/questions/7871028/box2d-gravity-to-specific-object

No comments:

Post a Comment