Code: Select all
void PhysicManager::setRigidBoxBody(Ogre::SceneNode *node, Ogre::Vector3 shapeDim)
{
shape = new btBoxShape(btVector3(shapeDim.x, shapeDim.y, shapeDim.z));
shape->calculateLocalInertia(10, inertia);
MyMotionState* motionState = new MyMotionState(node);
body = new btRigidBody(10, motionState, shape, inertia);
body->setUserPointer((void *) (node));
mBodyList.push_back(body);
mShapeList.push_back(shape);
mWorld->addRigidBody(body);
}
The world initialisation is quiet simple :
Code: Select all
mBroadphase = new btDbvtBroadphase();
mCollisionConfiguration = new btDefaultCollisionConfiguration();
mDispatcher = new btCollisionDispatcher(mCollisionConfiguration);
mSolver = new btSequentialImpulseConstraintSolver;
mWorld = new btDiscreteDynamicsWorld(mDispatcher, mBroadphase, mSolver, mCollisionConfiguration);
EDIT : The whole class for more info :
Code: Select all
#include "PhysicManager.h"
PhysicManager* PhysicManager::_singleton = NULL;
PhysicManager::PhysicManager()
{
}
PhysicManager::~PhysicManager()
{
shutdown();
}
void PhysicManager::start(Ogre::SceneManager* sceneMgr)
{
mSceneMgr = sceneMgr;
mBroadphase = new btDbvtBroadphase();
mCollisionConfiguration = new btDefaultCollisionConfiguration();
mDispatcher = new btCollisionDispatcher(mCollisionConfiguration);
mSolver = new btSequentialImpulseConstraintSolver;
mWorld = new btDiscreteDynamicsWorld(mDispatcher, mBroadphase, mSolver, mCollisionConfiguration);
shape = NULL;
body = NULL;
mWorld->setGravity(btVector3(0,-10,0));
}
void PhysicManager::shutdown()
{
mBodyList.clear();
mShapeList.clear();
delete shape;
delete body;
//Pointeurs pour bullet physic
delete mWorld;
delete mSolver;
delete mDispatcher;
delete mCollisionConfiguration;
delete mBroadphase;
}
PhysicManager* PhysicManager::getInstance()
{
if (NULL == _singleton)
{
_singleton = new PhysicManager;
}
return _singleton;
}
bool PhysicManager::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
mWorld->stepSimulation(evt.timeSinceLastFrame, 1);
return true;
}
void PhysicManager::setRigidBoxBody(Ogre::SceneNode *node, Ogre::Vector3 shapeDim)
{
shape = new btBoxShape(btVector3(shapeDim.x, shapeDim.y, shapeDim.z));
shape->calculateLocalInertia(10, inertia);
MyMotionState* motionState = new MyMotionState(node);
body = new btRigidBody(10, motionState, shape, inertia);
body->setUserPointer((void *) (node));
mBodyList.push_back(body);
mShapeList.push_back(shape);
//mWorld->addRigidBody(body);
}
Code: Select all
#pragma once
#include <Ogre.h>
#include <vector>
#include <btBulletDynamicsCommon.h>
#include <Bullet-C-Api.h>
#include <btBulletCollisionCommon.h>
#include "MyMotionState.h"
#include "../Console.h"
class PhysicManager : public Ogre::FrameListener
{
public:
//Constructeur
~PhysicManager();
static PhysicManager* getInstance();
void start(Ogre::SceneManager* sceneMgr);
void shutdown();
//Vérification des entrées
bool frameRenderingQueued(const Ogre::FrameEvent& evt);
void setRigidBoxBody(Ogre::SceneNode *node, Ogre::Vector3 shape);
private:
PhysicManager();
static PhysicManager* _singleton;
Ogre::SceneManager* mSceneMgr;
btDbvtBroadphase* mBroadphase;
btDefaultCollisionConfiguration* mCollisionConfiguration;
btCollisionDispatcher* mDispatcher;
btSequentialImpulseConstraintSolver* mSolver;
btDiscreteDynamicsWorld* mWorld;
btCollisionShape* shape;
btRigidBody* body;
btVector3 inertia;
std::vector<btRigidBody*> mBodyList;
std::vector<btCollisionShape*> mShapeList;
};
Code: Select all
#pragma once
#include <btBulletDynamicsCommon.h>
#include <Bullet-C-Api.h>
#include <btBulletCollisionCommon.h>
#include <Ogre.h>
class MyMotionState : public btMotionState
{
public:
MyMotionState(Ogre::SceneNode* node)
{
mNode = node;
mTrans.setIdentity();
}
virtual ~MyMotionState()
{ }
virtual void getWorldTransform(btTransform &worldTrans) const
{
worldTrans = mTrans;
}
virtual void setWorldTransform(const btTransform &worldTrans)
{
mTrans = worldTrans;
btQuaternion ori = mTrans.getRotation();
btVector3 pos = mTrans.getOrigin();
mNode->setPosition(Ogre::Vector3(pos.x(),pos.y(),pos.z()));
mNode->setOrientation(Ogre::Quaternion(ori.w(),ori.x(),ori.y(),ori.z()));
}
protected:
Ogre::SceneNode* mNode;
btTransform mTrans;
};