adam
adam

Reputation: 373

Bullet physics boxes don't rotate

I'm using the following code to set up a simple world using the bullet physics engine. When I run it, the boxes don't behave realistically because they don't rotate. It's like their rotation is locked. Has anyone seen something like this before or know what could be causing it? Thank you.

World::World()
{
mBroadphase = new btDbvtBroadphase();
mCollisionConfiguration = new btDefaultCollisionConfiguration();
mDispatcher = new btCollisionDispatcher(mCollisionConfiguration);
mSolver = new btSequentialImpulseConstraintSolver();
mWorld = new btDiscreteDynamicsWorld(mDispatcher, mBroadphase, mSolver, mCollisionConfiguration);
mWorld->setGravity(btVector3(0, -10, 0));

mGroundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 1);
mBoxShape = new btBoxShape(btVector3(4, 1.5, 1));

mGroundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, -10, 0)));
btRigidBody::btRigidBodyConstructionInfo groundRBCI(0, mGroundMotionState, mGroundShape, btVector3(0, 0, 0));

mGroundRigidBody = new btRigidBody(groundRBCI);
mGroundRigidBody->setUserIndex(1);
mWorld->addRigidBody(mGroundRigidBody);

btScalar mass = 1;
btVector3 boxInertia(0, 0, 0);
mBoxShape->calculateLocalInertia(mass, boxInertia);

mBoxMotionState1 = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 35, -20)));
btRigidBody::btRigidBodyConstructionInfo BoxRBCI1(mass, mBoxMotionState1, mBoxShape, btVector3(0, 0, 0));
mBoxRigidBody1 = new btRigidBody(BoxRBCI1);
mBoxRigidBody1->setUserIndex(2);

mBoxMotionState2 = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(2, 20, -20)));
btRigidBody::btRigidBodyConstructionInfo BoxRBCI2(mass, mBoxMotionState2, mBoxShape, btVector3(0, 0, 0));
mBoxRigidBody2 = new btRigidBody(BoxRBCI2);
mBoxRigidBody2->setUserIndex(3);

mBoxMotionState3 = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(-5, 12, -20)));
btRigidBody::btRigidBodyConstructionInfo BoxRBCI3(mass, mBoxMotionState3, mBoxShape, btVector3(0, 0, 0));
mBoxRigidBody3 = new btRigidBody(BoxRBCI3);
mBoxRigidBody3->setUserIndex(4);

mWorld->addRigidBody(mBoxRigidBody1);
mWorld->addRigidBody(mBoxRigidBody2);
mWorld->addRigidBody(mBoxRigidBody3);



}

Upvotes: 1

Views: 1436

Answers (1)

Gyan
Gyan

Reputation: 12410

Aha! I had this problem and I couldn't find an answer for ages so I'm happy to help!

The line

mBoxShape->calculateLocalInertia(mass, boxInertia);

modifies boxInertia and you then need to pass it to the constructors for btRigidBody::btRigidBodyConstructionInfo like this:

btRigidBody::btRigidBodyConstructionInfo BoxRBCI1(mass, mBoxMotionState1, mBoxShape, boxInertia);

Then your boxes should rotate :)

Upvotes: 5

Related Questions