torque not working

jackskelyton
Posts: 32
Joined: Mon Nov 05, 2007 3:52 pm

torque not working

Post by jackskelyton »

We are having trouble causing objects to dynamically rotate based on collisions. We've set up a simple Ccd world as detailed below, and we have no trouble loading objects, applying force, and making them move. There's a translation interface between Bullet vectors and our own proprietary vector class, but we know the translation is working because we can use SetAngularVelocity or any other explicit property change and the object responds appropriately. What we can't do is get one object to start spinning when it glances off another: all our btRigidBodies maintain their orientation no matter how they collide. We've tried messing with the values for sleeping, damping, friction, and restitution with no luck. Can anyone help us?

Initial values for the physics world:

Code: Select all

   // Initialize Bullet configurations
   mPhysicsConfig = new btDefaultCollisionConfiguration();
   mPhysicsDispatcher = new btCollisionDispatcher(mPhysicsConfig);
   mPhysicsSolver = new btSequentialImpulseConstraintSolver;
   mPhysicsBroadphase = new btAxisSweep3(worldAABBMin,worldAABBMax,maxProxies);

   // Create Bullet physics world
     mPhysicsWorld = new btDiscreteDynamicsWorld(mPhysicsDispatcher,mPhysicsBroadphase,mPhysicsSolver);

   // Set initial values
   mPhysicsWorld->getDispatchInfo().m_enableSPU = true;
   mPhysicsWorld->setGravity(btVector3(0,-0.5,0));
Our translation function between btVector and our vector:

Code: Select all

SFMatrix4f PhysicsObject::GetMatrix() {
   float data[16];
   mMotionState->getWorldTransform(mTransform);
   mTransform.getOpenGLMatrix(data);
   SFMatrix4f matrix = new SFMatrix4f(data, 1);
   return matrix;
}
And the function to add a rigidBody with the previously described variables:

Code: Select all

void PhysicsObject::BuildCollision(btCollisionShape* collisionShape) {
   mMotionState = new btDefaultMotionState(mTransform);
   mRigidBody = new btRigidBody(mMass, mMotionState, collisionShape, mInertia, 0.1f, 0.1f, 1.0f, 0.8f);

   GAME->GetPhysicsSystem()->GetPhysicsWorld()->addRigidBody(mRigidBody);
   mRigidBody->setCcdSquareMotionThreshold(btScalar(CALCULATION_THRESHOLD));
   mRigidBody->setSleepingThresholds(0.1f, 0.1f);
   mRigidBody->setCenterOfMassTransform(mTransform);
   mRigidBody->setActivationState(4);

}
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: torque not working

Post by Erwin Coumans »

How do you initialize mInertia?

Code: Select all

	btVector3 localInertia(0,0,0);
	if (isDynamic)
		shape->calculateLocalInertia(mass,localInertia);
Every dynamic object needs to have non-zero mass and non-zero inertia. We should put an assert for this case.

Could you compare the setup with the CcdPhysicsDemo more in detail?

Hope this helps,
Erwin
jackskelyton
Posts: 32
Joined: Mon Nov 05, 2007 3:52 pm

Re: torque not working

Post by jackskelyton »

mInertia is initialized as a new btVector3 of 0,0,0. When the game loop starts, the objects have no inertia. I'm not sure what else I can say to compare it to CcdPhysicsDemo: all the init code is right there. I copied and pasted it almost line for line, just changing the variables to my member variables in our Physics Object class. We initialize two btSphereShapes with radius of 2, give them mass of 1 and 0 inertia and place them in world 20 game units apart. Then I call AddCentralForce on one to send it into the other. It hits and bounces fine, but doesn't rotate based on the angle of attack. Plus, if I call addTorque or addTorqueImpulse, it doesn't appear to respond to those at all. The GetMatrix function shown above is the interface between the physics object and our Mesh Actor, and we know that interface is working because, as described above, we can call SetAngularVelocity or SetLinearVelocity and the mesh responds correctly: it is successfully grabbing the right data from the btWorldTransform. We think it must be a problem with the btDynamicWorld, some setting or something that is overriding torqued rotation.
bone
Posts: 231
Joined: Tue Feb 20, 2007 4:56 pm

Re: torque not working

Post by bone »

You're going to need a non-zero inertia.
jackskelyton
Posts: 32
Joined: Mon Nov 05, 2007 3:52 pm

Re: torque not working

Post by jackskelyton »

So I do. Why is that? Does bullet assume that anything with non-zero inertia is not dynamic?
bone
Posts: 231
Joined: Tue Feb 20, 2007 4:56 pm

Re: torque not working

Post by bone »

To simulate rigid bodies, one needs both mass (for translational movement) and an inertia tensor (for rotational movement). Just like the mass describes how hard it is to push the object in a particular direction, the inertia tensor describes how hard it is to rotate the object.

The reason you need a non-zero inertia is that Bullet and many other physics engines use zero (for both mass and inertia) to indicate a static object. Technically, it should be the inverse math and inverse inertia tensor that should be zero to make the math easier, but that's beside the point. If you haven't described how hard it is to rotate the object, the physics engine can't determine how much the object should rotate during collisions.

There's various methods to calculate appropriate and/or useable inertia values, searching around this board will help.
jackskelyton
Posts: 32
Joined: Mon Nov 05, 2007 3:52 pm

Re: torque not working

Post by jackskelyton »

That makes perfect sense. Thank you very much, that was the answer :)