Physics Simulation Forum

 

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Sun May 23, 2010 6:16 am 
Offline

Joined: Sun Jul 19, 2009 5:24 am
Posts: 10
I made this today, its only one file(main.cpp) (so its more understandable for the novice)
Hint *if you want the class version look at the bottom of this post*

Code:
#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;
}
}

Hint : the plane isn't really drawed...

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group