I made this today, its only one file(main.cpp) and it doesn't use classes(so its more understandable for the novice)
Code: Select all
#include <irrlicht.h>
#include <btBulletDynamicsCommon.h>
#include <iostream>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
void QuaternionToEuler(const btQuaternion &TQuat, btVector3 &TEuler)
{
btScalar W = TQuat.getW();
btScalar X = TQuat.getX();
btScalar Y = TQuat.getY();
btScalar Z = TQuat.getZ();
float WSquared = W * W;
float XSquared = X * X;
float YSquared = Y * Y;
float ZSquared = Z * Z;
TEuler.setX(atan2f(2.0f * (Y * Z + X * W), -XSquared - YSquared + ZSquared + WSquared));
TEuler.setY(asinf(-2.0f * (X * Z - Y * W)));
TEuler.setZ(atan2f(2.0f * (X * Y + Z * W), XSquared - YSquared - ZSquared + WSquared));
TEuler *= core::RADTODEG;
}
int main(int argc, char** argv)
{
//Creating the irrlicht device
IrrlichtDevice *device =createDevice(EDT_OPENGL, dimension2d<u32>(640, 480), 16, false, false, false, 0);
//setting pointers to the device!
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IGUIEnvironment* guienv = device->getGUIEnvironment();
//creating the 3d ball
ISceneNode *irrball = smgr->addSphereSceneNode();
irrball->setMaterialTexture( 0, driver->getTexture("../../media/wall.jpg") );
irrball->setMaterialFlag(EMF_LIGHTING,false);
//creating the camera
ICameraSceneNode *irrcam = smgr->addCameraSceneNode();
irrcam->setPosition(vector3df(0,0,-70));
//creating the broadphase
btBroadphaseInterface* broadphase = new btDbvtBroadphase();
//creating the physics properties configuration
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
//creating the solvers
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
//Making the dynamic world
btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration);
//setting the gravity
dynamicsWorld->setGravity(btVector3(0,-10,0));
//Its time to create the shapes for our collision
//Creating the ground( a simple plane )
btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),1);
//Creating the ball shape
btCollisionShape* fallShape = new btSphereShape(1);
//its time to create the ground object
btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,-1,0)));
//making the ground rigidody
btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0,groundMotionState,groundShape,btVector3(0,0,0));
btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);
//adding the ground to the physics world
dynamicsWorld->addRigidBody(groundRigidBody);
//its time to create the ball object!!!
btDefaultMotionState* fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,50,0)));
//adding and calculating the ball sphere mas
btScalar mass = 1;
btVector3 fallInertia(0,0,0);
fallShape->calculateLocalInertia(mass,fallInertia);
//constructing the rigidody(adding info,etc)
btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass,fallMotionState,fallShape,fallInertia);
//Creating the ball rigidbody
btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);
//And offcourse add it to the world
dynamicsWorld->addRigidBody(fallRigidBody);
btTransform trans;
btVector3 rot;
while (device->run())
{
//getting the world positions(x,y,z)
fallRigidBody->getMotionState()->getWorldTransform(trans);
//converting the bullet rotation axes so we can use it with our irrlicht node
QuaternionToEuler(trans.getRotation(),rot);
//adding the position and rotation to the node
irrball->setPosition(vector3df(trans.getOrigin().getX(),trans.getOrigin().getY(),trans.getOrigin().getZ()));
irrball->setRotation(vector3df(rot.getX(),rot.getY(),rot.getZ()));
//stepping the simulation
dynamicsWorld->stepSimulation(1/60.f,10);
std::cout << "sphere height: " << trans.getOrigin().getY() << std::endl;
driver->beginScene(true, true, SColor(0,200,200,200));
smgr->drawAll();
guienv->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 collisionConfiguration;
delete dispatcher;
delete broadphase;
device->drop();
return 0;
}
If you have any suggestions for improvement let me know.
And i am also going to make a object oriented style example when i get the chance.
[edit]If you know how to do debug drawing with irrlicht and bullet send me an example or another example which uses it so i can take a look on how its done else i will have to do the bloody researches myself
I hope this helps someone.
New formated code in oop style format : Click here!