As an overview, the cuboid is of dimensions 2 x 4 x 1.
So here's how I declared the shape:
Code: Select all
btCollisionShape *blockShape = new btBoxShape(btVector3(1, 2, 0.5));
Code: Select all
//Initialisation code:
//We'll need to set up a proper broadphase interface so it can quickly detect
//the obvious collisions that won't happen. The btDvbtBroadphase algorithm is the best for this.
btBroadphaseInterface *broadphase = new btDbvtBroadphase();
//Collision configs allow us to fine tune the collisions. The default one should be good enough
btDefaultCollisionConfiguration *collisionConfiguration = new btDefaultCollisionConfiguration();
btCollisionDispatcher *dispatcher = new btCollisionDispatcher(collisionConfiguration);
//The "solver" is what actually resolves the interaction of objects and the environmental
//parameters
btSequentialImpulseConstraintSolver *solver = new btSequentialImpulseConstraintSolver;
//The actual game world initialised!
btDiscreteDynamicsWorld *dynamicWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
dynamicWorld->setGravity(btVector3(0, -9.81, 0));
//Great, now we set up the floor...
btCollisionShape *floorShape = new btStaticPlaneShape(btVector3(0, 1, 0), 1);
btDefaultMotionState *floorMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 0)));
btRigidBody::btRigidBodyConstructionInfo
//First and last parameters are mass and inertia repectively. Bullet detects a mass of 0
//as immovable. Do we really want a floor to move?
floorRigidBodyCI(0, floorMotionState, floorShape, btVector3(0, 0, 0));
btRigidBody *floorRigidBody = new btRigidBody(floorRigidBodyCI);
dynamicWorld->addRigidBody(floorRigidBody);
//Now, we set up the block (but we won't add it to the world. That's for another method).
btCollisionShape *blockShape = new btBoxShape(btVector3(1, 2, 0.5));
//For the purposes of the demo, we will just use one block.
btDefaultMotionState *blockMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 100, 0)));
btScalar mass = 50;
btVector3 blockIntertia(0, 0, 0);
blockShape->calculateLocalInertia(mass, blockIntertia);
btRigidBody::btRigidBodyConstructionInfo
blockRigidBodyCI(mass, blockMotionState, blockShape, blockIntertia);
btRigidBody *blockRigidBody = new btRigidBody(blockRigidBodyCI);
dynamicWorld->addRigidBody(blockRigidBody);
//Code to step the simulation (I'm using Apple's GLKit and iOS. Should still be easy to use)
- (GLKVector3)getBLockLocation {
dynamicWorld->stepSimulation(1/60.f, 10);
btTransform trans;
blockRigidBody->getMotionState()->getWorldTransform(trans);
return GLKVector3Make(trans.getOrigin().getX(), trans.getOrigin().getY(), trans.getOrigin().getZ());
}