Forces work on bodies sometimes... but only sometimes

UPDATE: with some messing around, I have replicated the bug, but the bodies can rotate in space if another body hits them. Almost as if they have a revolute joint pinning them to the background. I haven't created that joint, but have heard this sort of thing can happen when a dynamic body overlaps a static one. Only problem is these dynamic bodies are NOT overlapping a static body...

Hi folks. I have an issue that I just can't seem to sort out. I have a Box2D project that I've been working on for a while and up until now all my bodies, fixtures, joints, etc., have behaved as I would have expected. Now, I have created two bodies and sometimes when I run my program, they will not move, no matter the size of the force applied to them.

Other times I run my program and they work great, responding to all the forces and impulses that I apply to them.

I for the life of me cannot figure out why this is. I create the bodies in very typical Box2D form, they aren't terribly big, no out of the ordinary values for friction, density, etc. They each have one, non-sensor fixture. They have no joints attached to them. They are pretty darned vanilla.

I have made sure that the magnitude of the forces I am applying is non-zero, and have tried using extremely large forces to get them to move, but to no avail. I have set them to not allow sleep, I have tried waking them before applying forces, nothing works.

I'm imagining that there must be some property of the bodies that I am not explicitly setting, so my compiler is assigning it an arbitrary value at runtime, but I don't know what it would be.

I would post code, but I'm not sure which parts are relevant, and it's all within wrapper classes anyway. If anyone has any ideas, I can of course post relevant bits.

Thanks for reading and I appreciate any help you can give!
I suspect you've checked this already but have you confirmed that these unmovable bodies are indeed setup as b2_dynamicBody bodies? By default bodies are setup as b2_staticBody which can't be moved by forces.

Does this help any?
I have tried explicitly setting them to dynamic, but it doesn't solve the issue :/ Good thought, though. Thank you!
How is your code managing the memory associated with the data to ensure everything is in-scope/valid when it needs to be? Can you post a culled-down version of your source code that demonstrates the problem?

Your theory of having memory not set as you intended does seem consistent with the problem description. The latest version of Box2D (as found on github) has default initialization of all b2FixtureDef and b2BodyDef member fields however. So those don't seem likely to sometimes do different things unless the memory that you're passing to a create method is somehow invalid when it's passed as you've probably considered.

Maybe just copy+paste the snippets of your source code that calls the world and body create methods for those two bodies that sometimes don't move so we can take a look at this together.
EDIT: I realize I forgot to include the creation of the Body's fixture in my code. Added it.

Louis Langholtz wrote:How is your code managing the memory associated with the data to ensure everything is in-scope/valid when it needs to be? Can you post a culled-down version of your source code that demonstrates the problem?

Your theory of having memory not set as you intended does seem consistent with the problem description. The latest version of Box2D (as found on github) has default initialization of all b2FixtureDef and b2BodyDef member fields however. So those don't seem likely to sometimes do different things unless the memory that you're passing to a create method is somehow invalid when it's passed as you've probably considered.

Maybe just copy+paste the snippets of your source code that calls the world and body create methods for those two bodies that sometimes don't move so we can take a look at this together.


I'm pretty positive nothing is going out of scope. The b2Body's (which are actually held in a wrapper class I wrote) are member variables that are quite strictly managed, and as they still are existing within the world, can be rotated by forces acting upon them, and are not causing any segfaults, I'm going to guess that the data I'm passing to create them isn't going out of scope. Plus, the whole management system I've set up has not failed me yet, and there are literally thousands of other b2Body's in this world that are acting correctly.

It could be that there are variables I'm not setting at all that are getting assigned problematic values by the compiler, though I'm not sure what they would be. You mention that the latest version of box2d assigns default values... but I'm actually using 2.2.1. So that could be the issue. I suppose I could try upgrading box2d.

In any case, here are the create methods in question. Because of wrapper classes, etc., they require a bit of explanation but I'll do my best.

Code: Select all

Funzo::Funzo(b2Vec2 position, float angle)
{
    // jl::TextureDesc is a wrapper class for SFML textures.
    // It's not going out of scope (I promise) as it's a smart pointer and being
    // managed by textureManager
    jl::TextureDesc::Ptr lhandTex;
    lhandTex = this->level()->game()->textureManager()->textureDescIs("resources/zorbitsorbits/funzoboss/funzolhand.png",
            sf::Vector2f(150, 150), 300, 309);

    b2BodyDef bodyDef;
    bodyDef.allowSleep = false;
    bodyDef.awake = true;
    bodyDef.active = true;
    bodyNew("FunzoLeftHand", "foreground", lhandTex, &bodyDef, position, angle);
   
    defineFixtures();
}

Body::Ptr Funzo::bodyNew(string name, string layerName, TextureDesc::Ptr tex,
        b2BodyDef * bodyDef, b2Vec2 initPos, int initAngle)
{
    bodyDef->userData = this;
    bodyDef->type = b2_dynamicBody;
    bodyDef->angle = initAngle;
    bodyDef->position.Set(initPos.x, initPos.y);

    // _body is an std::map<std::string, b2Body>. It is a member variable.  Yes I know that's a weird naming convention.
    _body[name] = new Body(name, this, tex, bodyDef);
    return _body[name];
}

Funzo::defineFixtures()
{
   b2FixtureDef fixDef;
      b2PolygonShape fixShape;
      fixShape.SetAsBox(2.25,2.75, b2Vec2(),0);
        fixDef.shape = &fixShape;
        fixDef.density = 5.0;
        fixDef.friction = 1.0;
        fixDef.isSensor = false;

   // FixtureUserData is another wrapper class that for our purposes
   // is functionally equivalent to box2D's fixture user data.
        jl::FixtureUserData::Ptr fixUD = new jl::FixtureUserData("LeftHand");
        // This fixtureNew method is also, for our purposes, functionally
        // equivalent to box2D's fixture creation method.
        body("FunzoLeftHand")->fixtureNew(fixUD3, &fixDef);
}

// Last but not least
Body::Body(string name, Entity * entity, TextureDesc::Ptr tex, b2BodyDef *
        bodyDef) : fwk::NamedInterface(name), _entity(entity), _textureDesc(tex)
{
    // Again, _b2body is a member variable.
    // the b2world() method returns a standard pointer to its level's member variable _b2world, which is
    // also a pointer.
    _b2body = _entity->level()->b2world()->CreateBody(bodyDef);
    _b2body->SetLinearDamping(1.0f);
    _b2body->SetAngularDamping(1.0f);
}


If you can think of other code that would be helpful to see I can post it, of course. Thanks for your help!
So I've decided to fix this problem by not using b2Body's for the boss's hands. I'm just going to manually move Sprites.

Still, in the interest of understanding the problem and in case anyone else ever runs into this problem, I'm happy to keep working on it, but perhaps without the same urgency. I'll continue to update as anything progresses.
In the Funzo::defineFixtures() method, the code passes a variable named fixUD3 to an invocation of the fixtureNew method. The variable named fixUD is defined just before that. Did a typo occur in the paste to the forum? Or is there perhaps some variable named fixUD3 that's getting passed while fixUD was actually intended to be passed?
Louis Langholtz wrote:In the Funzo::defineFixtures() method, the code passes a variable named fixUD3 to an invocation of the fixtureNew method. The variable named fixUD is defined just before that. Did a typo occur in the paste to the forum? Or is there perhaps some variable named fixUD3 that's getting passed while fixUD was actually intended to be passed?


Actually I think it's just a result of copying and pasting my code. There are other bodies with different fixUD's and this one happened to get number 3. Good catch, but unfortunately I don't think that's the problem.

I was able to fix the whole thing, though, by manually directing a sprite to move around the screen and syncing a b2Body's position with the sprite's position. That way I get exactly the movement I desire and still get the benefit of box2d's collision detection.
Yes, there are soft limits on sizes of bodies (as well as positions, speeds, masses, and really anything else that's an MKS based unit).

Soft limits on sizes are mentioned within chapter 1 section 7 "Units" portion of the manual (at least the 2.3.0 manual):
1.7 Units
Box2D works with floating point numbers and tolerances have to be used to make Box2D perform well. These tolerances have been tuned to work well with meters-kilogram-second (MKS) units. In particular, Box2D has been tuned to work well with moving shapes between 0.1 and 10 meters. So this means objects between soup cans and buses in size should work well. Static shapes may be up to 50 meters long without trouble.


There's also mention about limits in chapter 13 "Limitations" portion, though that section is more specific to mass ratios rather than sizes.
Fixed it! Here's the rundown.

They key problem turned out to be linearDamping. I had forgotten (because I'd done it so long ago), that every body in my gameworld checks whether or not it is within a gravity field on each "update" loop. If it is, it sets its linearDamping to a pre-determined value, and if not, it sets it to zero. This is basically so that bodys in space would just float on forever and ever, as one would expect.

Here's the problem: For these problematic bodys, I never set the pre-determined value for linearDamping so at compile time, the compiler would assign the linearDamping a random value. It could be so monstrously big that the bodies wouldn't move, or it could be so small that they would, and anywhere in between.

Here's a gif of the behavior that lead me to my discovery.
http://imgur.com/a/7qyxa

Original thread: https://box2d.org/forum/viewtopic.php?t=10224