[1985] | 1 | /*************************************************************************** |
---|
| 2 | |
---|
| 3 | This source file is part of OGREBULLET |
---|
| 4 | (Object-oriented Graphics Rendering Engine Bullet Wrapper) |
---|
| 5 | For the latest info, see http://www.ogre3d.org/phpBB2addons/viewforum.php?f=10 |
---|
| 6 | |
---|
| 7 | Copyright (c) 2007 tuan.kuranes@gmail.com (Use it Freely, even Statically, but have to contribute any changes) |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | This program is free software; you can redistribute it and/or modify it under |
---|
| 12 | the terms of the GPL General Public License with runtime exception as published by the Free Software |
---|
| 13 | Foundation; either version 2 of the License, or (at your option) any later |
---|
| 14 | version. |
---|
| 15 | |
---|
| 16 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
| 17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
| 18 | FOR A PARTICULAR PURPOSE. See the GPL General Public License with runtime exception for more details. |
---|
| 19 | |
---|
| 20 | You should have received a copy of the GPL General Public License with runtime exception along with |
---|
| 21 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
| 22 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
| 23 | http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
| 24 | ----------------------------------------------------------------------------- |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | #include "OgreBulletCollisions.h" |
---|
| 28 | |
---|
| 29 | #include "OgreBulletCollisionsWorld.h" |
---|
| 30 | #include "Utils/OgreBulletConverter.h" |
---|
| 31 | |
---|
| 32 | #include "OgreBulletCollisionsObject.h" |
---|
| 33 | #include "Debug/OgreBulletCollisionsDebugShape.h" |
---|
| 34 | #include "OgreBulletCollisionsRay.h" |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | using namespace Ogre; |
---|
| 38 | using namespace OgreBulletCollisions; |
---|
| 39 | |
---|
| 40 | namespace OgreBulletCollisions |
---|
| 41 | { |
---|
| 42 | // ------------------------------------------------------------------------- |
---|
| 43 | CollisionsWorld::CollisionsWorld(SceneManager *scn, const AxisAlignedBox &bounds, bool init, bool set32bitsAxisSweep): |
---|
| 44 | mScnMgr(scn), |
---|
| 45 | mBounds(bounds), |
---|
| 46 | mShowDebugShapes(false), |
---|
| 47 | mShowDebugContactPoints(false), |
---|
| 48 | mDebugContactPoints(0) |
---|
| 49 | { |
---|
| 50 | mDispatcher = new btCollisionDispatcher(&mDefaultCollisionConfiguration); |
---|
| 51 | |
---|
| 52 | if (set32bitsAxisSweep) |
---|
| 53 | { |
---|
| 54 | mBroadphase = new bt32BitAxisSweep3( |
---|
| 55 | OgreBtConverter::to(bounds.getMinimum()), |
---|
| 56 | OgreBtConverter::to(bounds.getMaximum())); |
---|
| 57 | } |
---|
| 58 | else |
---|
| 59 | { |
---|
| 60 | mBroadphase = new btAxisSweep3( |
---|
| 61 | OgreBtConverter::to(bounds.getMinimum()), |
---|
| 62 | OgreBtConverter::to(bounds.getMaximum())); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | // if not called by a inherited class |
---|
| 66 | if (init) |
---|
| 67 | { |
---|
| 68 | mWorld = new btCollisionWorld(mDispatcher, mBroadphase, &mDefaultCollisionConfiguration); |
---|
| 69 | |
---|
| 70 | btCollisionDispatcher * dispatcher = static_cast<btCollisionDispatcher *>(mWorld->getDispatcher()); |
---|
| 71 | btGImpactCollisionAlgorithm::registerAlgorithm(dispatcher); |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | // ------------------------------------------------------------------------- |
---|
| 75 | CollisionsWorld::~CollisionsWorld() |
---|
| 76 | { |
---|
| 77 | delete mWorld; |
---|
| 78 | delete mBroadphase; |
---|
| 79 | delete mDispatcher; |
---|
| 80 | } |
---|
| 81 | // ------------------------------------------------------------------------- |
---|
| 82 | void CollisionsWorld::setShowDebugContactPoints(bool show) |
---|
| 83 | { |
---|
| 84 | if (show && !mShowDebugContactPoints) |
---|
| 85 | { |
---|
| 86 | assert (mDebugContactPoints == 0); |
---|
| 87 | |
---|
| 88 | mShowDebugContactPoints = true; |
---|
| 89 | return; |
---|
| 90 | } |
---|
| 91 | if (!show && mShowDebugContactPoints) |
---|
| 92 | { |
---|
| 93 | assert (mDebugContactPoints != 0); |
---|
| 94 | |
---|
| 95 | mShowDebugContactPoints = false; |
---|
| 96 | return; |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | // ------------------------------------------------------------------------- |
---|
| 100 | void CollisionsWorld::setShowDebugShapes(bool show) |
---|
| 101 | { |
---|
| 102 | if (show && !mShowDebugShapes) |
---|
| 103 | { |
---|
| 104 | //assert (mDebugShapes == 0); |
---|
| 105 | std::deque<Object*>::iterator it = mObjects.begin(); |
---|
| 106 | while (it != mObjects.end()) |
---|
| 107 | { |
---|
| 108 | (*it)->showDebugShape(show); |
---|
| 109 | ++it; |
---|
| 110 | } |
---|
| 111 | mShowDebugShapes = true; |
---|
| 112 | return; |
---|
| 113 | } |
---|
| 114 | if (!show && mShowDebugShapes) |
---|
| 115 | { |
---|
| 116 | //assert (mDebugShapes != 0); |
---|
| 117 | std::deque<Object*>::iterator it = mObjects.begin(); |
---|
| 118 | while (it != mObjects.end()) |
---|
| 119 | { |
---|
| 120 | (*it)->showDebugShape(show); |
---|
| 121 | ++it; |
---|
| 122 | } |
---|
| 123 | mShowDebugShapes = false; |
---|
| 124 | return; |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | // ------------------------------------------------------------------------- |
---|
| 128 | void CollisionsWorld::addObject(Object *obj) |
---|
| 129 | { |
---|
| 130 | mObjects.push_back (obj); |
---|
| 131 | mWorld->addCollisionObject(obj->getBulletObject()); |
---|
| 132 | } |
---|
| 133 | //------------------------------------------------------------------------- |
---|
| 134 | bool CollisionsWorld::removeObject(Object *obj) |
---|
| 135 | { |
---|
| 136 | std::deque<Object*>::iterator it = find(mObjects.begin(),mObjects.end(), obj); |
---|
| 137 | if (it == mObjects.end()) |
---|
| 138 | return false; |
---|
| 139 | mObjects.erase(it); |
---|
| 140 | return true; |
---|
| 141 | } |
---|
| 142 | // ------------------------------------------------------------------------- |
---|
| 143 | bool CollisionsWorld::isObjectregistered(Object *obj) const |
---|
| 144 | { |
---|
| 145 | std::deque<Object *>::const_iterator itRes = std::find(mObjects.begin(), mObjects.end(), obj); |
---|
| 146 | if (itRes != mObjects.end()) |
---|
| 147 | return true; |
---|
| 148 | return false; |
---|
| 149 | } |
---|
| 150 | // ------------------------------------------------------------------------- |
---|
| 151 | Object *CollisionsWorld::findObject(btCollisionObject *object) const |
---|
| 152 | { |
---|
| 153 | std::deque<Object *>::const_iterator it = mObjects.begin(); |
---|
| 154 | while (it != mObjects.end()) |
---|
| 155 | { |
---|
| 156 | if ((*it)->getBulletObject() == object) |
---|
| 157 | return (*it); |
---|
| 158 | ++it; |
---|
| 159 | } |
---|
| 160 | return 0; |
---|
| 161 | } |
---|
| 162 | // ------------------------------------------------------------------------- |
---|
| 163 | Object *CollisionsWorld::findObject(SceneNode *node) const |
---|
| 164 | { |
---|
| 165 | std::deque<Object *>::const_iterator it = mObjects.begin(); |
---|
| 166 | while (it != mObjects.end()) |
---|
| 167 | { |
---|
| 168 | //if ((*it)->getParentNode() == node) |
---|
| 169 | if((*it)->getRootNode() == node) |
---|
| 170 | return (*it); |
---|
| 171 | ++it; |
---|
| 172 | } |
---|
| 173 | return 0; |
---|
| 174 | } |
---|
| 175 | // ------------------------------------------------------------------------- |
---|
| 176 | void CollisionsWorld::discreteCollide() |
---|
| 177 | { |
---|
| 178 | mWorld->performDiscreteCollisionDetection(); |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | ///one way to draw all the contact points is iterating over contact manifolds / points: |
---|
| 182 | const unsigned int numManifolds = mWorld->getDispatcher()->getNumManifolds(); |
---|
| 183 | for (unsigned int i=0;i < numManifolds; i++) |
---|
| 184 | { |
---|
| 185 | btPersistentManifold* contactManifold = mWorld->getDispatcher()->getManifoldByIndexInternal(i); |
---|
| 186 | |
---|
| 187 | btCollisionObject* obA = static_cast<btCollisionObject*>(contactManifold->getBody0()); |
---|
| 188 | btCollisionObject* obB = static_cast<btCollisionObject*>(contactManifold->getBody1()); |
---|
| 189 | |
---|
| 190 | contactManifold->refreshContactPoints(obA->getWorldTransform(),obB->getWorldTransform()); |
---|
| 191 | |
---|
| 192 | const unsigned int numContacts = contactManifold->getNumContacts(); |
---|
| 193 | for (unsigned int j = 0;j < numContacts; j++) |
---|
| 194 | { |
---|
| 195 | btManifoldPoint& pt = contactManifold->getContactPoint(j); |
---|
| 196 | |
---|
| 197 | if (mShowDebugContactPoints) |
---|
| 198 | { |
---|
| 199 | btVector3 ptA = pt.getPositionWorldOnA(); |
---|
| 200 | btVector3 ptB = pt.getPositionWorldOnB(); |
---|
| 201 | |
---|
| 202 | mDebugContactPoints->addLine(ptA.x(),ptA.y(),ptA.z(), |
---|
| 203 | ptB.x(),ptB.y(),ptB.z()); |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | //you can un-comment out this line, and then all points are removed |
---|
| 207 | //contactManifold->clearManifold(); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | /* |
---|
| 211 | if (mShowDebugShapes) |
---|
| 212 | { |
---|
| 213 | std::deque<Object*>::iterator it = mObjects.begin(); |
---|
| 214 | while (it != mObjects.end()) |
---|
| 215 | { |
---|
| 216 | |
---|
| 217 | //(*it)->getBulletObject()->getWorldTransform().getOpenGLMatrix( m ); |
---|
| 218 | mShowDebugDrawShapes-> |
---|
| 219 | //GL_ShapeDrawer::drawOpenGL(m,objects[i].getCollisionShape(),btVector3(1,1,1),getDebugMode()); |
---|
| 220 | |
---|
| 221 | ++it; |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | */ |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | // ------------------------------------------------------------------------- |
---|
| 228 | void CollisionsWorld::launchRay(CollisionRayResultCallback &rayresult, short int collisionFilterMask) |
---|
| 229 | { |
---|
| 230 | mWorld->rayTest( |
---|
| 231 | OgreBtConverter::to(rayresult.getRayStartPoint()), |
---|
| 232 | OgreBtConverter::to(rayresult.getRayEndPoint()), |
---|
| 233 | *rayresult.getBulletRay () |
---|
| 234 | //, collisionFilterMask |
---|
| 235 | ); |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|