noob question: body doesn't fall

Post Reply
chutoi
Posts: 16
Joined: Fri Aug 10, 2012 4:21 pm

noob question: body doesn't fall

Post by chutoi »

Good morning,
I'm new to using bullet. While trying to use it I can't get gravity to apply to my avatar.
The poor guy just hangs there suspended in mid air.

Here's a screen capture of the world with debug draw turned on:
ScreenCapture.2012.8.10.12.17.51.jpg
ScreenCapture.2012.8.10.12.17.51.jpg (74.36 KiB) Viewed 11483 times
I'm using C++ with mingw-tdm. I use ogre and the btOgre wrapper to display the world.

Here's how I initialize bullet

Code: Select all

   collisionConfiguration = new btDefaultCollisionConfiguration();
   CollisionDispatcher = new btCollisionDispatcher( collisionConfiguration );
   /// btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep.
   overlappingPairCache = new btDbvtBroadphase();
   /// the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
   solver = new btSequentialImpulseConstraintSolver;
   dynamicsWorld = new btDiscreteDynamicsWorld( CollisionDispatcher, overlappingPairCache, solver, collisionConfiguration );
   dynamicsWorld->setGravity( btVector3( 0, -10, 0 ) );

   mDebugDrawer = new OgreDebugDrawer( mSceneMgr );
   mDebugDrawer->setDebugMode( btIDebugDraw::DBG_DrawWireframe | btIDebugDraw::DBG_DrawAabb | btIDebugDraw::DBG_DrawContactPoints ); //
   dynamicsWorld->setDebugDrawer( mDebugDrawer );

Here's my initialization for the avatar:

Code: Select all

      BtOgre::AnimatedMeshToShapeConverter* anConvert = new BtOgre::AnimatedMeshToShapeConverter( Entity, scale );
      Shape = anConvert->createTrimesh();
      //Calculate inertia.
      btScalar mass = 1.0f;
      btVector3 inertia;
      Shape->calculateLocalInertia( mass, inertia );

      // motion state tracks movement caused by physics
      State = new BtOgre::RigidBodyState( Node );

      Body = new btRigidBody( mass, State, Shape, inertia );

      Body->setCollisionFlags( Body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT );
      Body->setActivationState( DISABLE_DEACTIVATION );

      // reset interpolation between frames
      Body->setInterpolationWorldTransform( Body->getWorldTransform() );
      Body->setInterpolationLinearVelocity( btVector3( 0, 0, 0 ) );
      Body->setInterpolationAngularVelocity( btVector3( 0, 0, 0 ) );
I scale and rotate the avatar to fit my world before creating the mesh.
I think debug draw shows I have the visible mesh and the physics mesh overlaid correctly.

Once the program is fully initialized I set his initial position like this:

Code: Select all

      btTransform xf;
      xf.setIdentity();
      xf.setOrigin( btVector3( x, y, z ) );
      Body->getMotionState()->setWorldTransform( xf );

      // reset interpolation between frames
      Body->setInterpolationWorldTransform( Body->getWorldTransform() );
      Body->setInterpolationLinearVelocity( btVector3( 0, 0, 0 ) );
      Body->setInterpolationAngularVelocity( btVector3( 0, 0, 0 ) );
I have a timer that runs every 16ms to update the world. In the timer code
it does the physics update like this:

Code: Select all

               struct timeval time;
               gettimeofday( &time, 0 );

               float TimeStep = ( time.tv_sec - LastPhysicsUpdateTime.tv_sec )
                  + float( time.tv_usec - LastPhysicsUpdateTime.tv_usec ) / 1000000.0f
                  ;

               // It's important that timeStep is always less than maxSubSteps*fixedTimeStep, otherwise you are losing time.
               int maxSubSteps = (int)( 60 * TimeStep ) + 1;
               dynamicsWorld->stepSimulation( TimeStep, maxSubSteps );
               LastPhysicsUpdateTime = time;
I have checked that the timer is calling stepSimulation().

I've hooked up a button to try to debug motion. That button does this:

Code: Select all

Body->applyCentralImpulse( btVector3( 0.0f, 0.0f, 200.0f ) );
This has no visible effect.

What does the red for the avatar indicate?

Any suggestions?

Thanks
khoowaikeong
Posts: 43
Joined: Fri Jun 15, 2012 7:11 am

Re: noob question: body doesn't fall

Post by khoowaikeong »

did you 'activate' him?
i am pretty much a noob myself, but what i do when i create a new character is clear the cache and activate the body.

if (pDynamicsWorld->getBroadphase()->getOverlappingPairCache())
pDynamicsWorld->getBroadphase()->getOverlappingPairCache()->cleanProxyFromPairs(pRigidBody->getBroadphaseHandle(),pDynamicsWorld->getDispatcher());

pRigidBody->forceActivationState(ACTIVE_TAG);
pRigidBody->activate();
chutoi
Posts: 16
Joined: Fri Aug 10, 2012 4:21 pm

Re: noob question: body doesn't fall

Post by chutoi »

khoowaikeong wrote:did you 'activate' him?
Thanks for taking the time to help.
I didn't activate the avatar. I added your code but it didn't fix the issue.
When I turned on DebugDraw mode the wireframe of the avatar is now green instead of red.
I'm assuming that's good... :?
zarlox
Posts: 31
Joined: Tue Apr 26, 2011 5:52 pm

Re: noob question: body doesn't fall

Post by zarlox »

chutoi wrote: Thanks for taking the time to help.
I didn't activate the avatar. I added your code but it didn't fix the issue.
When I turned on DebugDraw mode the wireframe of the avatar is now green instead of red.
I'm assuming that's good... :?
If the debug drawer is showing green, it means that the rigid body is in fact activated and should be affected by the gravity and other forces/impulses. Red means the body is deactivated

Have you tried pushing it with an impulse to see if it does react properly.
chutoi
Posts: 16
Joined: Fri Aug 10, 2012 4:21 pm

Re: noob question: body doesn't fall

Post by chutoi »

zarlox wrote: If the debug drawer is showing green, it means that the rigid body is in fact activated and should be affected by the gravity and other forces/impulses. Red means the body is deactivated
That helps a lot. For some reason I couldn't find this information using a search engine. The force isn't very strong with me today...
zarlox wrote: Have you tried pushing it with an impulse to see if it does react properly.

Code: Select all

      // get force vector along current orientation
      btVector3 relativeForce = btVector3( 0.0f, 20000.0f, 0.0f );
      btMatrix3x3& boxRot = Body->getWorldTransform().getBasis();
      btVector3 correctedForce = boxRot * relativeForce;
//      Body->applyCentralForce( correctedForce );
      Body->applyCentralImpulse( correctedForce );
Yes. I tried both applyCentralForce() and applyCentralImpulse().
I increased the amount of the force just in case it was too small to make a visible difference.
I tried calculating the force along the current orientation (debug shows that as the same as I was using before).

Doesn't seem to make any difference.
chutoi
Posts: 16
Joined: Fri Aug 10, 2012 4:21 pm

Re: noob question: body doesn't fall

Post by chutoi »

I added a simulation tick callback and enumerated the collisions:

Code: Select all

   void OgreWorld::PhysicsCallback( btScalar timeStep )
   {
      btCollisionObjectArray objects = getCollisionObjectArray();
      for ( int i = 0; i < objects.size(); i++ )
      {
         btRigidBody* rigidBody = btRigidBody::upcast( objects[i] );
         if ( !rigidBody )
         {
            continue;
         }
         Ogre::LogManager::getSingleton().logMessage( "OgreWorld::PhysicsCallback() "
            + Ogre::String( " rigid body contact" )
         );
      }
   }
That tells me there are five collisions. One for each of the four terrain patches (they overlap to eliminate "seams") and one for the avatar. So it looks like bullet thinks the avatar has already collided with the terrain. When looking at the screen cap of the debug output it looks like the bounding box for the terrain is much lower though. Maybe it's the terrain that's the problem?
quzox
Posts: 7
Joined: Wed Aug 01, 2012 9:08 pm

Re: noob question: body doesn't fall

Post by quzox »

Going by the code provided it doesn't look like you're inserting the btRigidBody into the dynamics world! :lol: So somewhere in your code there should be a:

dynamicsWorld->addRigidBody(Body);
chutoi
Posts: 16
Joined: Fri Aug 10, 2012 4:21 pm

Re: noob question: body doesn't fall

Post by chutoi »

quzox wrote:Going by the code provided it doesn't look like you're inserting the btRigidBody into the dynamics world! :lol: So somewhere in your code there should be a:

dynamicsWorld->addRigidBody(Body);
That would certainly explain it but I think I'm doing that in the center of the second code block.
The BtOgre::AnimatedMeshToShapeConverter class creates a triangle mesh shape from the ogre entity.
I set his mass to 1.0 and add him to the world.
khoowaikeong
Posts: 43
Joined: Fri Jun 15, 2012 7:11 am

Re: noob question: body doesn't fall

Post by khoowaikeong »

have you try not setting btCollisionObject::CF_KINEMATIC_OBJECT and treating it as a default object?
also is the callback for the position being called?
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: noob question: body doesn't fall

Post by xexuxjy »

Have you tried representing your avatar as just a simple BoxShape or Capsule for now rather than a tri-mesh?
chutoi
Posts: 16
Joined: Fri Aug 10, 2012 4:21 pm

Re: noob question: body doesn't fall

Post by chutoi »

khoowaikeong wrote:have you try not setting btCollisionObject::CF_KINEMATIC_OBJECT and treating it as a default object?
also is the callback for the position being called?
Interesting. The figure does move if I comment out the code that sets it to a kinematic object.

I will have to research how bullet treats those kind of objects. I read something in a forum post that said bullet pushed motion updates to rigid bodies but that it read motion updates from kinematic ones. Is there a discussion on how it does this?
chutoi
Posts: 16
Joined: Fri Aug 10, 2012 4:21 pm

Re: noob question: body doesn't fall

Post by chutoi »

xexuxjy wrote:Have you tried representing your avatar as just a simple BoxShape or Capsule for now rather than a tri-mesh?
Thanks for the idea. I think I'm doing motion control with kinematic objects incorrectly.
It appears the simulation is working at least... :)
chutoi
Posts: 16
Joined: Fri Aug 10, 2012 4:21 pm

Re: noob question: body doesn't fall

Post by chutoi »

The problem was that I marked the avatar as a kinematic object.
A careful rereading of the docs now makes more sense.
With kinematic objects bullet doesn't apply forces to the object, it only applies forces from the object.
So my avatar would push aside any other objects but not be affected by gravity.
I'm guessing this is why the character controller class was created.

Thanks for the help all! :)
Post Reply