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.
if anyone knows how i can present code in my posts in a clean and readable way, please leave me a comment.
That seems like a really good idea! I would try itbut I’m having a little trouble with box2d and glut together. Can you help me out. If you can, please check the general discution and my thread is “Simple Problems”.
By the way, that’s a lot for the anti gravity post I’m sure it will be very helpful to many people.
you have to separate force and gravity in step 12.
e.g.:
b->m_linearVelocity += step.dt * (b->m_invMass * b->m_force);
if (b->IsGravitated())
b->m_linearVelocity += step.dt * gravity;
@redspider
Thank you for that catch! I don’t directly apply forces in my applications and so didn’t notice the difference. I’ve updated the post accordingly.
Ok I just tried it, but I made the changes exactly like you said, but a whole lot of errors occurred, and that was it.
Can you suggest anything?
Oh nvm it worked. BTW you need to add the function declaration in the b2Body class, and in the actual function it should be:
return (m_flags & e_gravitatedFlag) == e_gravitatedFlag;
instead of:
eturn (m_flags & e_gravitateFlag) == e_gravitatedFlag;
Your missing a d in gravitatedFlag
Also when I add:
bodyDef.isGravitated = false;
it says isGravitated is not a part of the b2BodyDef struct…which it clearly is
@kcaz
Thanks for catching the typo (the missing “d”), and the function declaration (I’ve made the changes in the post accordingly). As for the message generated with “bodyDef.isGravitated = false” I don’t get that error/warning, so I’m not sure how to help you there, but it sounds to me like it’s just a warning and everything is working fine?
Also I think you need to add:
if (bd->isGravitated)
{
m_flags |= e_gravitatedFlag;
}
in b2Body.cpp at the top of the file in the constructor with all the other flags.
And no, btw I’m still getting the error, and it’s not a warning, it stops the program. It says that b2BodyDef has no member named isGravitated. Im sure I’m spelling everything right.
@kcaz
Sorry I keep forgetting steps; I’ll be sure to use svn next time to track the changes I’ve made.
As for your error, I can’t imagine what would cause that if you’ve done Step 3 properly (which, from your expertise on the matter, I assume you have).
If I were you, the first thing I’d try is making some ridiculous changes to the b2BodyDef struct and try compile/running it (just to make sure you’re dealing with the correct copy of the file being linked/compiled).
Ok well I’ve checked over all my code multiple times, and I included all the steps. Also I made drastic changes, and it effected the program.
Oh… hehe wait a sec, I was trying to organize the project, and had some problems with the source files. I think your right and the problem is that it’s not using the correct files. I’m gonna see what I can do…
WOW that was funny. Well thanks, I got it to work. Like i said the problem was that I was using the wrong files…hehe.
Thanks for all the help,
Kcaz
@kcaz (as if there’s anyone else here..)
Nice! Glad I could help.