00001 #include "PhysicsManager.h"
00002 #include "Application.h"
00003 #include "Shapes/OgreBulletCollisionsStaticPlaneShape.h"
00004 #include "Shapes/OgreBulletCollisionsBoxShape.h"
00005 #include "Shapes/OgreBulletCollisionsTrimeshShape.h"
00006 #include "Shapes/OgreBulletCollisionsCapsuleShape.h"
00007 #include "Utils/OgreBulletCollisionsMeshToShapeConverter.h"
00008 #include "Craft.h"
00009 #include <string>
00010
00011 PhysicsManager::PhysicsManager()
00012 {
00013
00014 m_gravity = Ogre::Vector3(0.0, -9.80665, 0.0);
00015 m_boundarySize = 10000.0;
00016
00017 Ogre::Vector3 extent = Ogre::Vector3(m_boundarySize,m_boundarySize,m_boundarySize);
00018
00019 m_worldBoundary = Ogre::AxisAlignedBox((extent * -1.0),extent);
00020
00021
00022 m_dynamicsWorld = new OgreBulletDynamics::DynamicsWorld(Application::getSingleton()->getViewerManager(), m_worldBoundary, m_gravity);
00023
00024 m_debugDrawer = new OgreBulletCollisions::DebugDrawer();
00025 m_dynamicsWorld->setDebugDrawer(m_debugDrawer);
00026
00027 Ogre::SceneNode *node = Application::getSingleton()->getViewerManager()->getRootSceneNode()->createChildSceneNode("debugDrawer", Ogre::Vector3::ZERO);
00028 node->attachObject(static_cast<Ogre::SimpleRenderable*>(m_debugDrawer));
00029
00030
00031 setDebug(false);
00032
00033 }
00034
00035 PhysicsManager::~PhysicsManager()
00036 {
00037 {
00038 std::list<OgreBulletDynamics::RigidBody*>::iterator iterator;
00039 for (iterator = m_rigidBodies.begin(); iterator != m_rigidBodies.end(); ++iterator)
00040 {
00041 delete (*iterator);
00042 }
00043 m_rigidBodies.clear();
00044 }
00045 {
00046 std::list<OgreBulletCollisions::CollisionShape*>::iterator iterator;
00047 for (iterator = m_collisionShapes.begin(); iterator != m_collisionShapes.end(); ++iterator)
00048 {
00049 delete (*iterator);
00050 }
00051 m_collisionShapes.clear();
00052 }
00053 {
00054 std::list<btTypedConstraint*>::iterator iterator;
00055 for (iterator = m_constraints.begin(); iterator != m_constraints.end(); ++iterator)
00056 {
00057 delete (*iterator);
00058 }
00059 m_constraints.clear();
00060 }
00061 delete m_dynamicsWorld->getDebugDrawer();
00062 m_dynamicsWorld->setDebugDrawer(0);
00063 delete m_dynamicsWorld;
00064 }
00065
00066 void PhysicsManager::setDebug(bool _val)
00067 {
00068 m_debugDrawer->setDrawWireframe(_val);
00069 m_debugDrawer->setDrawAabb(_val);
00070 m_debugDrawer->setDrawContactPoints(_val);
00071 m_debugDrawer->setDrawFeaturesText(_val);
00072 m_debugDrawer->setDrawText(_val);
00073 m_dynamicsWorld->getBulletDynamicsWorld()->debugDrawWorld();
00074 m_dynamicsWorld->getBulletCollisionWorld()->debugDrawWorld();
00075 m_dynamicsWorld->setShowDebugShapes(_val);
00076 m_dynamicsWorld->setShowDebugContactPoints(_val);
00077 }
00078
00079 void PhysicsManager::createGroundPlane()
00080 {
00081 Ogre::Entity *ent;
00082 Ogre::Plane plane(Ogre::Vector3::UNIT_Y,0.0);
00083 Ogre::MeshManager::getSingleton().createPlane("GroundPlane", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane, 200000, 200000, 20, 20, true, 1, 9000, 9000, Ogre::Vector3::UNIT_Z);
00084
00085 ent = Application::getSingleton()->getViewerManager()->createEntity("floor", "GroundPlane");
00086 ent->setMaterialName("Examples/Rocky");
00087 Application::getSingleton()->getViewerManager()->getRootSceneNode()->createChildSceneNode()->attachObject(ent);
00088
00089 OgreBulletCollisions::CollisionShape *Shape;
00090 Shape = new OgreBulletCollisions::StaticPlaneCollisionShape(Ogre::Vector3(0,1,0), 0);
00091 OgreBulletDynamics::RigidBody *defaultPlaneBody = new OgreBulletDynamics::RigidBody("BasePlane", m_dynamicsWorld);
00092 defaultPlaneBody->setStaticShape(Shape, 0.1, 0.8);
00093
00094 m_collisionShapes.push_back(Shape);
00095 m_rigidBodies.push_back(defaultPlaneBody);
00096 }
00097
00098 void PhysicsManager::boxWall()
00099 {
00100 Ogre::Vector3 size;
00101 Ogre::Vector3 position;
00102
00103 for (int i = 0; i < 10; ++i)
00104 {
00105 for (int j = 0; j < 5; ++j)
00106 {
00107 size = Ogre::Vector3::ZERO;
00108 position = Ogre::Vector3(100,j + 0.5, i);
00109
00110 SelectableObject* box = new SelectableObject();
00111
00112 Ogre::Entity *entity = Application::getSingleton()->getViewerManager()->createEntity("Box" + Ogre::StringConverter::toString(box->getID()),Application::getSingleton()->getViewerManager()->PT_CUBE);
00113 entity->setCastShadows(true);
00114
00115
00116 Ogre::AxisAlignedBox bbox = entity->getBoundingBox();
00117 size = bbox.getSize() * 0.5f;
00118
00119
00120
00121 size *= 0.96f;
00122
00123 Ogre::SceneNode* node = Application::getSingleton()->getViewerManager()->getRootSceneNode()->createChildSceneNode();
00124 node->scale(0.01f, 0.01f, 0.01f);
00125 size *= 0.01f;
00126
00127 box->setSceneNode(node);
00128 box->setEntity(entity);
00129 box->setMoveable(true);
00130 Application::getSingleton()->getObjectManager()->addObject(box);
00131
00132
00133 OgreBulletCollisions::BoxCollisionShape* sceneBoxShape = new OgreBulletCollisions::BoxCollisionShape(size);
00134
00135 OgreBulletDynamics::RigidBody *defaultBody = new OgreBulletDynamics::RigidBody("defaultBoxRigid" + Ogre::StringConverter::toString(box->getID()),m_dynamicsWorld);
00136 defaultBody->setShape( node,
00137 sceneBoxShape,
00138 0.6f,
00139 0.6f,
00140 1.0f,
00141 position,
00142 Ogre::Quaternion(0,0,0,1));
00143
00144 m_collisionShapes.push_back(sceneBoxShape);
00145 m_rigidBodies.push_back(defaultBody);
00146 }
00147 }
00148 }
00149 void PhysicsManager::update(Ogre::Real _time)
00150 {
00151 m_dynamicsWorld->stepSimulation(_time);
00152 }
00153
00154 void PhysicsManager::addPair(OgreBulletDynamics::RigidBody* _rigid, OgreBulletCollisions::CollisionShape* _shape)
00155 {
00156 addRigidBody(_rigid);
00157 addCollisionShape(_shape);
00158 }
00159
00160 void PhysicsManager::addRigidBody(OgreBulletDynamics::RigidBody* _val)
00161 {
00162 if (_val != 0)
00163 {
00164 m_rigidBodies.push_back(_val);
00165 }
00166 }
00167
00168 void PhysicsManager::addCollisionShape(OgreBulletCollisions::CollisionShape* _val)
00169 {
00170 if (_val != 0)
00171 {
00172 m_collisionShapes.push_back(_val);
00173 }
00174 }
00175
00176 OgreBulletDynamics::RigidBody* PhysicsManager::createRigidBody(std::string _val)
00177 {
00178 return new OgreBulletDynamics::RigidBody(_val,m_dynamicsWorld);
00179 }
00180
00181 OgreBulletCollisions::CollisionShape* PhysicsManager::createCollisionShape(const Ogre::Real& _radius, const Ogre::Real& _height, const Ogre::Vector3& _axis)
00182 {
00183 return new OgreBulletCollisions::CapsuleCollisionShape(_radius, _height, _axis);
00184 }
00185
00186 OgreBulletCollisions::CollisionShape* PhysicsManager::createCollisionShape(Ogre::Entity* _val)
00187 {
00188 OgreBulletCollisions::StaticMeshToShapeConverter* converter = new OgreBulletCollisions::StaticMeshToShapeConverter(_val);
00189
00190 OgreBulletCollisions::TriangleMeshCollisionShape* meshShape = converter->createTrimesh();
00191
00192 delete converter;
00193 return meshShape;
00194 }
00195
00196 Ogre::Vector3 PhysicsManager::getGravity()
00197 {
00198 return m_gravity;
00199 }
00200
00201 void PhysicsManager::setGravity(Ogre::Vector3 _val)
00202 {
00203 m_gravity = _val;
00204 }
00205
00206 void PhysicsManager::addConstraint(btTypedConstraint* _val)
00207 {
00208
00209 m_dynamicsWorld->getBulletDynamicsWorld()->addConstraint(_val);
00210 m_constraints.push_back(_val);
00211 }
00212
00213 OgreBulletDynamics::DynamicsWorld* PhysicsManager::getDynamicsWorld()
00214 {
00215 return m_dynamicsWorld;
00216 }