For turning off the effects of gravity on some bodies while not on others, this tutorial covers the necessary steps to add an “isGravitated” property to the b2BodyDef struct.
The result:
// turn off gravity
bodyDef.isGravitated = false;
The steps (16):
1) Locate “b2Body.h” file (found in the “Source/Dynamics” folder).
2) Locate the following code (try Ctrl+F or Command+F for this step):
bool isBullet;
3) Just below the above line of code, add a new variable:
bool isGravitated;
4) Locate the following code (towards the top of the file):
b2BodyDef()
{
massData.center.SetZero();
massData.mass = 0.0f;
massData.I = 0.0f;
userData = NULL;
position.Set(0.0f, 0.0f);
angle = 0.0f;
linearDamping = 0.0f;
angularDamping = 0.0f;
allowSleep = true;
isSleeping = false;
fixedRotation = false;
isBullet = false;
}
5) In the b2BodyDef constructor (i.e. the above code), add a default value for isGravitated:
isGravitated = true;
6) Also in the b2Body.h file, locate the following code:
enum
{
e_frozenFlag = 0×0002,
e_islandFlag = 0×0004,
e_sleepFlag = 0×0008,
e_allowSleepFlag = 0×0010,
e_bulletFlag = 0×0020,
e_fixedRotationFlag = 0×0040,
}
7) Add a value just below “e_fixedRotationFlag” to represent our new “IsGravitated” attribute:
e_gravitatedFlag = 0×0200,
8) Also in the b2Body.h file, locate the following code:
/// Is this body treated like a bullet for continuous collision detection?
bool IsBullet() const;
9) Just below the “IsBullet()” function declaration, declare our “IsGravitated()” function by adding:
/// Is this body affected by gravity?
bool IsGravitated() const;
10) Still in the b2Body.h file, locate the following code:
inline bool b2Body::IsBullet() const
11) Just above the line of code (the placement is merely for organizational purposes), add the following code:
inline bool b2Body::IsGravitated() const
{
return (m_flags & e_gravitatedFlag) == e_gravitatedFlag;
}
12) Locate the b2Island.cpp file (also found in the “Source/Dynamics” folder).
13) In the b2Island.cpp file, find the following line of code:
b->m_linearVelocity += step.dt * (gravity + b->m_invMass * b->m_force);
14) Separation application of gravity and applied forces, and add an “if” condition to test for the “isGravitated” property so the code now looks like (i.e. replace the above line of code with the following):
if (b->IsGravitated())
{
b->m_linearVelocity += step.dt * gravity;
}
b->m_linearVelocity += step.dt * (b->m_invMass * b->m_force);
15) Finally, in the b2Body.cpp file, locate the following code:
if (bd->isBullet)
{
m_flags |= e_bulletFlag;
}
16) Just below the above code, add the following:
if (bd->isGravitated)
{
m_flags |= e_gravitatedFlag;
}
All 16 steps complete.
If you arrived here to find your answer with difficulty because you were using different search terms than those associated with this article, please contribute to the below list via comment.
how do i turn off gravity for box2d? in box2d, how do i turn off gravity for some objects and not others? can i make my box2d objects not fall? how do i implement a bullet or projectile that’s not affected by gravity? i want some b2body instances to fall and others to not fall. can i adjust the b2bodydef struct to accommodate the toggling of gravity? nullifying gravity for some objects in box2d.