Dynamic object not moving (Noob alert)

Hydrael
Posts: 7
Joined: Sat Sep 19, 2009 3:20 pm

Dynamic object not moving (Noob alert)

Post by Hydrael »

Hello everyone,

I'm trying to implement Bullet as the physics engine within my project, and I seem to have problems using it correctly:
I have a static object (ground) and a dynamic object (sphere).
I was following some tutorials/example codes and implemented the following:

Bullet initialization:

Code: Select all

          mCollisionConfiguration = new btDefaultCollisionConfiguration();
          mCollisionDispatcher = new btCollisionDispatcher(mCollisionConfiguration);
          btVector3 worldAabbMin(-150, -150, -150);
          btVector3 worldAabbMax(150, 150, 150);
          mWorldDimension = new btAxisSweep3(worldAabbMin,worldAabbMax);
          mSolver = new btSequentialImpulseConstraintSolver;
          mPhysicsWorld = new btDiscreteDynamicsWorld(mCollisionDispatcher, mWorldDimension, mSolver, mCollisionConfiguration);

          if ( 0 == mPhysicsWorld )
          {
              std::string msg = "CGameSession::CGameSession: Error creating physics world";
              MESSAGELOGGER.Log(msg);
              throw std::exception(msg.c_str());
          }

          mPhysicsWorld->setGravity(btVector3(0, -9.81f, 0));
          DEBUGLOGGER.Log("CGameSession::CGameSession: Successfully initialized physics");
Rigid body creation function:

Code: Select all

  btRigidBody* CGameSession::CreateRigidBody(float mass, const btTransform& startTransform, btCollisionShape* shape)
  {
      btAssert((!shape || shape->getShapeType() != INVALID_SHAPE_PROXYTYPE));

      //rigidbody is dynamic if and only if mass is non zero, otherwise static
      bool isDynamic = (mass != 0.f);

      btVector3 localInertia(0,0,0);
      if (isDynamic)
          shape->calculateLocalInertia(mass,localInertia);

      btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);

      btRigidBody::btRigidBodyConstructionInfo cInfo(mass,myMotionState,shape,localInertia);

      btRigidBody* body = new btRigidBody(cInfo);
      mPhysicsWorld->addRigidBody(body);

      return body;
  }
Ground initialization:

Code: Select all

btTransform groundTransform;
      groundTransform.setIdentity();
      groundTransform.setOrigin(btVector3(0, 0, 0));
      mPlayingFieldGroundBodyShape = new btBoxShape(btVector3(groundSizeX, groundSizeY, groundSizeZ));
      mPlayingFieldGroundBody = CreateRigidBody(mPlayingFieldMass, groundTransform, mPlayingFieldGroundBodyShape);
Sphere initialization:

Code: Select all

btTransform transform;
      transform.setIdentity();
      transform.setOrigin(btVector3(mSphereStartPosX, mSphereStartPosY, mSphereStartPosZ));
      mSphereBodyShape = new btSphereShape(mSphereRadius);
      mSphereBody = CreateRigidBody(mSphereMass, transform, mSphereBodyShape);
Update code:

Code: Select all

void UpdatePhysics(float ms) { mPhysicsWorld->stepSimulation(ms / 1000.0f , 60); }
I excpeted the sphere to be falling downwards and bounce off the ground, but it isn't moving at all.
mPlayingFieldMass is 0 and mSphereMass is 1.0f
What am I doing wrong?

Thanks a lot in advance for any help.

Greets,
Chris
shogun
Posts: 34
Joined: Tue Mar 04, 2008 3:16 pm

Re: Dynamic object not moving (Noob alert)

Post by shogun »

Try to give your sphere some gravity.
Hydrael
Posts: 7
Joined: Sat Sep 19, 2009 3:20 pm

Re: Dynamic object not moving (Noob alert)

Post by Hydrael »

Thanks for your help, but unfortunately that doesn't work either :?

I altered my rigid body creation function in the following way:

Code: Select all

  btRigidBody* CGameSession::CreateRigidBody(float mass, const btTransform& startTransform, btCollisionShape* shape)
  {
      btAssert((!shape || shape->getShapeType() != INVALID_SHAPE_PROXYTYPE));

      //rigidbody is dynamic if and only if mass is non zero, otherwise static
      bool isDynamic = (mass != 0.f);

      btVector3 localInertia(0,0,0);
      if (isDynamic)
          shape->calculateLocalInertia(mass,localInertia);

      btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);

      btRigidBody::btRigidBodyConstructionInfo cInfo(mass,myMotionState,shape,localInertia);

      btRigidBody* body = new btRigidBody(cInfo);
      if (isDynamic)
      {
          body->setGravity(btVector3(0,-9.81f,0));
          body->activate();
          printf("%d\n", body->isActive());
      }
      mPhysicsWorld->addRigidBody(body);

      return body;
  }
That printf will tell, that the body is active, which seems to be correct, but later on in my processing loop, after calling ->stepSimulation, another printf will tell that the body became inactive.
RBD
Posts: 141
Joined: Tue Sep 16, 2008 11:31 am

Re: Dynamic object not moving (Noob alert)

Post by RBD »

Hydrael wrote:Update code:

Code: Select all

void UpdatePhysics(float ms) { mPhysicsWorld->stepSimulation(ms / 1000.0f , 60); }
May be obvious, but just in case: ms != microseconds?
Hydrael
Posts: 7
Joined: Sat Sep 19, 2009 3:20 pm

Re: Dynamic object not moving (Noob alert)

Post by Hydrael »

RBD wrote:
Hydrael wrote:Update code:

Code: Select all

void UpdatePhysics(float ms) { mPhysicsWorld->stepSimulation(ms / 1000.0f , 60); }
May be obvious, but just in case: ms != microseconds?
Nope, it was milliseconds - thanks for the advice!
The printf now says the object is active throughout runtime.
It still isn't moving though, but that could also be a renderer problem.

I'm gonna check that out tommorow.
Hydrael
Posts: 7
Joined: Sat Sep 19, 2009 3:20 pm

Re: Dynamic object not moving (Noob alert)

Post by Hydrael »

Ok, unfortunately this didn't work either :?

I'm polling position updates with this code:

Code: Select all

btTransform t;
      mSphereBody->getMotionState()->getWorldTransform(t);
But t.m_origin always stays the same.

Any other ideas anyone?
rrck
Posts: 26
Joined: Mon Sep 21, 2009 7:30 am

Re: Dynamic object not moving (Noob alert)

Post by rrck »

Hello.

If I fully understand your code, wouldn't you need to multiply ms by 1000 instead of dividing? As I understand if you divide ms by 1000 you get very small number.
Hydrael
Posts: 7
Joined: Sat Sep 19, 2009 3:20 pm

Re: Dynamic object not moving (Noob alert)

Post by Hydrael »

rrck wrote:Hello.

If I fully understand your code, wouldn't you need to multiply ms by 1000 instead of dividing? As I understand if you divide ms by 1000 you get very small number.
I fixed that already. Multiplying the timestep value by 1000 caused the object to stay active - which is correct. But it still wouldn't move
rrck
Posts: 26
Joined: Mon Sep 21, 2009 7:30 am

Re: Dynamic object not moving (Noob alert)

Post by rrck »

Hm I think, and understood that when you have miliseconds it is correct to divide them by 1000 when passing to bullet, because bullet gets time in seconds.
Hydrael
Posts: 7
Joined: Sat Sep 19, 2009 3:20 pm

Re: Dynamic object not moving (Noob alert)

Post by Hydrael »

I've been playing around with that value pretty much.
But no matter how high or low I scale the timestep value, I will get no movement :?
Hydrael
Posts: 7
Joined: Sat Sep 19, 2009 3:20 pm

Re: Dynamic object not moving (Noob alert)

Post by Hydrael »

No more ideas? :?