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 "OgreBulletDynamics.h" |
---|
28 | |
---|
29 | #include "OgreBulletCollisionsShape.h" |
---|
30 | |
---|
31 | #include "OgreBulletDynamicsWorld.h" |
---|
32 | #include "OgreBulletDynamicsObjectState.h" |
---|
33 | #include "OgreBulletDynamicsRigidBody.h" |
---|
34 | #include "OgreBulletDynamicsConstraint.h" |
---|
35 | |
---|
36 | #include "Constraints/OgreBulletDynamicsRaycastVehicle.h" |
---|
37 | |
---|
38 | #include "BulletCollision/GIMPACT/btGImpactShape.h" |
---|
39 | #include "BulletCollision/GIMPACT/btGImpactCollisionAlgorithm.h" |
---|
40 | |
---|
41 | using namespace Ogre; |
---|
42 | using namespace OgreBulletCollisions; |
---|
43 | |
---|
44 | namespace OgreBulletDynamics |
---|
45 | { |
---|
46 | |
---|
47 | DynamicsWorld::DynamicsWorld(Ogre::SceneManager *mgr, |
---|
48 | const Ogre::AxisAlignedBox &bounds, |
---|
49 | const Ogre::Vector3 &gravity, |
---|
50 | bool init) : |
---|
51 | CollisionsWorld(mgr, bounds, false), |
---|
52 | mDebugDrawer(0) |
---|
53 | { |
---|
54 | //btSequentialImpulseConstraintSolver |
---|
55 | //btSequentialImpulseConstraintSolver3 |
---|
56 | mConstraintsolver = new btSequentialImpulseConstraintSolver(); |
---|
57 | |
---|
58 | //only if init is true, otherwise you have to create mWorld manually later on |
---|
59 | if (init) { |
---|
60 | mWorld = new btDiscreteDynamicsWorld(mDispatcher, mBroadphase, mConstraintsolver, &mDefaultCollisionConfiguration); |
---|
61 | static_cast <btDiscreteDynamicsWorld *> (mWorld)->setGravity(btVector3(gravity.x,gravity.y,gravity.z)); |
---|
62 | |
---|
63 | btCollisionDispatcher * dispatcher = static_cast<btCollisionDispatcher *>(mWorld->getDispatcher()); |
---|
64 | btGImpactCollisionAlgorithm::registerAlgorithm(dispatcher); |
---|
65 | } |
---|
66 | |
---|
67 | } |
---|
68 | // ------------------------------------------------------------------------- |
---|
69 | DynamicsWorld::~DynamicsWorld() |
---|
70 | { |
---|
71 | delete mConstraintsolver; |
---|
72 | } |
---|
73 | |
---|
74 | // ------------------------------------------------------------------------- |
---|
75 | void DynamicsWorld::addRigidBody (RigidBody *rb, short collisionGroup, short collisionMask) |
---|
76 | { |
---|
77 | mObjects.push_back (static_cast <Object *> (rb)); |
---|
78 | |
---|
79 | if (collisionGroup == 0 && collisionMask == 0) |
---|
80 | { |
---|
81 | // use default collision group/mask values (dynamic/kinematic/static) |
---|
82 | static_cast <btDiscreteDynamicsWorld *> (mWorld)->addRigidBody(rb->getBulletRigidBody()); |
---|
83 | } |
---|
84 | else |
---|
85 | { |
---|
86 | static_cast <btDiscreteDynamicsWorld *> (mWorld)->addRigidBody(rb->getBulletRigidBody(), collisionGroup, collisionMask); |
---|
87 | } |
---|
88 | } |
---|
89 | // ------------------------------------------------------------------------- |
---|
90 | void DynamicsWorld::stepSimulation(const Ogre::Real elapsedTime, int maxSubSteps, const Ogre::Real fixedTimestep) |
---|
91 | { |
---|
92 | // Reset Debug Lines |
---|
93 | if (mDebugDrawer) |
---|
94 | mDebugDrawer->clear (); |
---|
95 | |
---|
96 | static_cast <btDiscreteDynamicsWorld *> (mWorld)->stepSimulation(elapsedTime, maxSubSteps, fixedTimestep); |
---|
97 | |
---|
98 | if (mDebugDrawer) |
---|
99 | { |
---|
100 | // draw lines that step Simulation sent. |
---|
101 | mDebugDrawer->draw(); |
---|
102 | |
---|
103 | const bool drawFeaturesText = (mDebugDrawer->getDebugMode () & btIDebugDraw::DBG_DrawFeaturesText) != 0; |
---|
104 | if (drawFeaturesText) |
---|
105 | { |
---|
106 | // on all bodies we have |
---|
107 | // we get all shapes and draw more information |
---|
108 | //depending on mDebugDrawer mode. |
---|
109 | std::deque<Object*>::iterator it = mObjects.begin(); |
---|
110 | while (it != mObjects.end()) |
---|
111 | { |
---|
112 | //(*it)->drawFeaturesText(); |
---|
113 | ++it; |
---|
114 | } |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | // ------------------------------------------------------------------------- |
---|
119 | void DynamicsWorld::removeConstraint(TypedConstraint *constraint) |
---|
120 | { |
---|
121 | getBulletDynamicsWorld()->removeConstraint(constraint->getBulletTypedConstraint()); |
---|
122 | std::deque <TypedConstraint*>::iterator it = mConstraints.begin(); |
---|
123 | while (it != mConstraints.end()) |
---|
124 | { |
---|
125 | if ((*it) == constraint) |
---|
126 | { |
---|
127 | mConstraints.erase (it); |
---|
128 | break; |
---|
129 | } |
---|
130 | ++it; |
---|
131 | } |
---|
132 | } |
---|
133 | // ------------------------------------------------------------------------- |
---|
134 | void DynamicsWorld::addConstraint(TypedConstraint *constraint) |
---|
135 | { |
---|
136 | getBulletDynamicsWorld()->addConstraint(constraint->getBulletTypedConstraint()); |
---|
137 | mConstraints.push_back(constraint); |
---|
138 | } |
---|
139 | // ------------------------------------------------------------------------- |
---|
140 | void DynamicsWorld::addVehicle(RaycastVehicle *v) |
---|
141 | { |
---|
142 | getBulletDynamicsWorld()->addVehicle(v->getBulletVehicle ()); |
---|
143 | mConstraints.push_back(static_cast <TypedConstraint *> (v)); |
---|
144 | |
---|
145 | //mVehicles.push_back(v); |
---|
146 | } |
---|
147 | // ------------------------------------------------------------------------- |
---|
148 | bool DynamicsWorld::isConstraintRegistered(TypedConstraint *constraint) const |
---|
149 | { |
---|
150 | std::deque <TypedConstraint*>::const_iterator it = mConstraints.begin(); |
---|
151 | while (it != mConstraints.end()) |
---|
152 | { |
---|
153 | if ((*it) == constraint) |
---|
154 | return true; |
---|
155 | ++it; |
---|
156 | } |
---|
157 | return false; |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|