Very basic question on applying a force to a dynamic body

ireg
Posts: 3
Joined: Thu Feb 17, 2011 8:38 pm

Very basic question on applying a force to a dynamic body

Post by ireg »

I just started working with Bullet, and I'm confused about how I should apply forces to an object. I've basically got the HelloWorld code from the wiki working and I'm trying to make it so that when I press some key, the ball will "jump". I tried following the code given in the tick call back article, but it's not working correctly. The ball falls down as if no force is being exerted.

What's confusing me is that in that article the code in the callback looks like this:

Code: Select all

btCollisionObjectArray objects = world->getCollisionObjectArray();
    world->clearForces();
    for (int i = 0; i < objects.size(); i++) {
        btRigidBody *rigidBody = btRigidBody::upcast(objects[i]);
        if (!rigidBody) {
            continue;
        }
        rigidBody->applyGravity();
        rigidBody->applyForce(btVector3(0., 20., 0.), btVector3(0., 0., 0.));
    }
    return;
But even when I comment out the line about applying gravity, the object still falls down. Where is that gravity coming from? What's the correct way of applying a force to an object? If anyone has any links to tutorials or can point me to a Bullet demo that can help I would appreciate it. So far I have found no basic help online other than the wiki and the only Demo that I found to be close uses DynamicCharacterController, which according to the code is now obsolete.
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: Very basic question on applying a force to a dynamic bod

Post by dphil »

It sounds like either:

a) Your callback is not even being used. Make sure it is actually set up correctly and being called (for ex, put a printf statement inside it).
or
b) You may have set it up as a post-tick callback rather than a pre-tick callback. From the wiki:
"activate the pre-tick call of the callback function by passing true as third argument of setInternalTickCallback. (The reason for this is that Bullet will clear all forces after the usual (post-tick) call.)"
Either one would explain why both your "jump" action and commenting out applyGravity() do anything. Otherwise, not sure what the problem is.