Hint *if you want the class version look at the bottom of this post*
Code: Select all
#include <irrlicht.h>
#include <btBulletDynamicsCommon.h>
class MotionState : public btMotionState
{
public:
MotionState(const btTransform& initalTransformation, irr::scene::ISceneNode* const node) :
node(node), initalTransformation(initalTransformation)
{
}
void getWorldTransform(btTransform& worldTrans) const
{
worldTrans = this->initalTransformation;
}
void setWorldTransform(const btTransform& worldTrans)
{
worldTrans.getOpenGLMatrix(matr.pointer());
this->node->setRotation(matr.getRotationDegrees());
this->node->setPosition(matr.getTranslation());
}
private:
irr::scene::ISceneNode* const node;
irr::core::matrix4 matr;
btTransform initalTransformation;
};
int main()
{
irr::IrrlichtDevice* device = irr::createDevice();
irr::video::IVideoDriver* driver = device->getVideoDriver();
irr::scene::ISceneManager* smgr = device->getSceneManager();
btDbvtBroadphase* broadphase = new btDbvtBroadphase;
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration;
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
dynamicsWorld->setGravity(btVector3(0, -9.80665, 0));
irr::scene::ICameraSceneNode* camera = smgr->addCameraSceneNode();
camera->setPosition(irr::core::vector3df(0.0f, 5.0f, -15.0f));
btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0.0, 1.0, 0.0), 1.0);
btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1),btVector3(0,-1,0)));
btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0,groundMotionState,groundShape,btVector3(0.0, 0.0, 0.0));
btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);
dynamicsWorld->addRigidBody(groundRigidBody);
irr::scene::IMeshSceneNode* sphere = smgr->addSphereSceneNode(1.0f);
sphere->setMaterialFlag(irr::video::EMF_LIGHTING, false);
btCollisionShape* fallShape = new btSphereShape(1.0);
MotionState* fallMotionState = new MotionState(btTransform(btQuaternion(0.0, 0.0, 0.0, 1.0), btVector3(0.0, 50.0, 0.0)), sphere);
btScalar mass = 1.0;
btVector3 inertia(0.0, 0.0, 0.0);
fallShape->calculateLocalInertia(mass, inertia);
btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, fallMotionState, fallShape, inertia);
btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);
dynamicsWorld->addRigidBody(fallRigidBody);
irr::ITimer* const timer = device->getTimer();
irr::u32 then = timer->getTime();
while (device->run())
{
const irr::u32 now = timer->getTime();
const btScalar frameDeltaTime = (btScalar)(now - then)*0.001; // Time in seconds
then = now;
dynamicsWorld->stepSimulation(frameDeltaTime, 10);
driver->beginScene(true, true, irr::video::SColor(255, 133, 133, 133));
smgr->drawAll();
driver->endScene();
}
// Clean up behind ourselves like good little programmers
dynamicsWorld->removeRigidBody(fallRigidBody);
delete fallRigidBody->getMotionState();
delete fallRigidBody;
dynamicsWorld->removeRigidBody(groundRigidBody);
delete groundRigidBody->getMotionState();
delete groundRigidBody;
delete fallShape;
delete groundShape;
delete dynamicsWorld;
delete solver;
delete dispatcher;
delete collisionConfiguration;
delete broadphase;
device->drop();
return 0;
}
}
Credit : credit to RandomMesh for giving me a better code then i had previous.
If you have any suggestions for improvement let me know.
I hope this helps someone.

** admin ** LINK to spam upload site removed. Please attach a zipped .c++ file next time