1 | |
---|
2 | #include "OgreOdePrecompiledHeaders.h" |
---|
3 | |
---|
4 | #include "OgreOdeWorld.h" |
---|
5 | #include "OgreOdeBody.h" |
---|
6 | #include "OgreOdeJoint.h" |
---|
7 | #include "OgreOdeGeometry.h" |
---|
8 | #include "OgreOdeSpace.h" |
---|
9 | #include "OgreOdeDebugObject.h" |
---|
10 | |
---|
11 | using namespace OgreOde; |
---|
12 | using namespace Ogre; |
---|
13 | |
---|
14 | CollisionListener* World::_collision_listener = 0; |
---|
15 | //------------------------------------------------------------------------------------------------ |
---|
16 | void World::setCollisionListener(CollisionListener* collision_listener) |
---|
17 | { |
---|
18 | _collision_listener = collision_listener; |
---|
19 | } |
---|
20 | //------------------------------------------------------------------------------------------------ |
---|
21 | CollisionListener* World::getCollisionListener() |
---|
22 | { |
---|
23 | return _collision_listener; |
---|
24 | } |
---|
25 | //------------------------------------------------------------------------------------------------ |
---|
26 | World::World(SceneManager* manager) : |
---|
27 | _show_debug_geoms (false), |
---|
28 | _show_debug_contact (false), |
---|
29 | _manager (manager) |
---|
30 | { |
---|
31 | _world = dWorldCreate(); |
---|
32 | _contacts = dJointGroupCreate(0); |
---|
33 | |
---|
34 | _default_space = new HashTableSpace(this); |
---|
35 | _default_space->setAutoCleanup(false); |
---|
36 | _default_space->setInternalCollisions(true); |
---|
37 | |
---|
38 | setDamping(0.0,0.0); |
---|
39 | setHistorySize (1); |
---|
40 | } |
---|
41 | //------------------------------------------------------------------------------------------------ |
---|
42 | void World::setHistorySize(size_t historySize) |
---|
43 | { |
---|
44 | _history_size = historySize; |
---|
45 | |
---|
46 | MaintainedItemIterator<Body> it = _body_list.getIterator(); |
---|
47 | while(!it.end ()) |
---|
48 | { |
---|
49 | Body * const b = (Body *) (it.getNext ()); |
---|
50 | b->_historyResize(historySize); |
---|
51 | } |
---|
52 | } |
---|
53 | //------------------------------------------------------------------------------------------------ |
---|
54 | size_t World::getHistorySize() const |
---|
55 | { |
---|
56 | return _history_size; |
---|
57 | } |
---|
58 | //------------------------------------------------------------------------------------------------ |
---|
59 | void World::setGravity(const Ogre::Vector3& gravity) |
---|
60 | { |
---|
61 | dWorldSetGravity(_world,(dReal)gravity.x,(dReal)gravity.y,(dReal)gravity.z); |
---|
62 | } |
---|
63 | //------------------------------------------------------------------------------------------------ |
---|
64 | const Ogre::Vector3& World::getGravity() |
---|
65 | { |
---|
66 | dVector3 g; |
---|
67 | dWorldGetGravity(_world,g); |
---|
68 | _gravity.x = (Real)g[0]; |
---|
69 | _gravity.y = (Real)g[1]; |
---|
70 | _gravity.z = (Real)g[2]; |
---|
71 | return _gravity; |
---|
72 | } |
---|
73 | //------------------------------------------------------------------------------------------------ |
---|
74 | void World::setERP(Real erp) |
---|
75 | { |
---|
76 | dWorldSetERP(_world,(dReal)erp); |
---|
77 | } |
---|
78 | //------------------------------------------------------------------------------------------------ |
---|
79 | Real World::getERP() |
---|
80 | { |
---|
81 | return (Real)dWorldGetERP(_world); |
---|
82 | } |
---|
83 | //------------------------------------------------------------------------------------------------ |
---|
84 | void World::setCFM(Real cfm) |
---|
85 | { |
---|
86 | dWorldSetCFM(_world,(dReal)cfm); |
---|
87 | } |
---|
88 | //------------------------------------------------------------------------------------------------ |
---|
89 | Real World::getCFM() |
---|
90 | { |
---|
91 | return (Real)dWorldGetCFM(_world); |
---|
92 | } |
---|
93 | //------------------------------------------------------------------------------------------------ |
---|
94 | void World::setAutoSleep(bool auto_sleep) |
---|
95 | { |
---|
96 | dWorldSetAutoDisableFlag(_world,(auto_sleep)?1:0); |
---|
97 | } |
---|
98 | //------------------------------------------------------------------------------------------------ |
---|
99 | bool World::getAutoSleep() |
---|
100 | { |
---|
101 | return (dWorldGetAutoDisableFlag(_world))?true:false; |
---|
102 | } |
---|
103 | //------------------------------------------------------------------------------------------------ |
---|
104 | void World::setAutoSleepLinearThreshold(Real linear_threshold) |
---|
105 | { |
---|
106 | dWorldSetAutoDisableLinearThreshold(_world,(dReal)linear_threshold); |
---|
107 | } |
---|
108 | //------------------------------------------------------------------------------------------------ |
---|
109 | Real World::getAutoSleepLinearThreshold() |
---|
110 | { |
---|
111 | return (Real)dWorldGetAutoDisableLinearThreshold(_world); |
---|
112 | } |
---|
113 | //------------------------------------------------------------------------------------------------ |
---|
114 | void World::setAutoSleepAngularThreshold(Real angular_threshold) |
---|
115 | { |
---|
116 | dWorldSetAutoDisableAngularThreshold(_world,(dReal)angular_threshold); |
---|
117 | } |
---|
118 | //------------------------------------------------------------------------------------------------ |
---|
119 | Real World::getAutoSleepAngularThreshold() |
---|
120 | { |
---|
121 | return (Real)dWorldGetAutoDisableAngularThreshold(_world); |
---|
122 | } |
---|
123 | //------------------------------------------------------------------------------------------------ |
---|
124 | void World::setAutoSleepSteps(int steps) |
---|
125 | { |
---|
126 | dWorldSetAutoDisableSteps(_world,steps); |
---|
127 | } |
---|
128 | //------------------------------------------------------------------------------------------------ |
---|
129 | int World::getAutoSleepSteps() |
---|
130 | { |
---|
131 | return dWorldGetAutoDisableSteps(_world); |
---|
132 | } |
---|
133 | //------------------------------------------------------------------------------------------------ |
---|
134 | void World::setAutoSleepTime(Real time) |
---|
135 | { |
---|
136 | dWorldSetAutoDisableTime(_world,(dReal)time); |
---|
137 | } |
---|
138 | //------------------------------------------------------------------------------------------------ |
---|
139 | Real World::getAutoSleepTime() |
---|
140 | { |
---|
141 | return (Real)dWorldGetAutoDisableTime(_world); |
---|
142 | } |
---|
143 | //------------------------------------------------------------------------------------------------ |
---|
144 | void World::setAutoSleepAverageSamplesCount(size_t time) |
---|
145 | { |
---|
146 | dWorldSetAutoDisableAverageSamplesCount(_world, time); |
---|
147 | } |
---|
148 | //------------------------------------------------------------------------------------------------ |
---|
149 | size_t World::getAutoSleepAverageSamplesCount() |
---|
150 | { |
---|
151 | return (Real)dWorldGetAutoDisableAverageSamplesCount(_world); |
---|
152 | } |
---|
153 | //------------------------------------------------------------------------------------------------ |
---|
154 | void World::setContactCorrectionVelocity(Real velocity) |
---|
155 | { |
---|
156 | dWorldSetContactMaxCorrectingVel(_world,(dReal)velocity); |
---|
157 | } |
---|
158 | //------------------------------------------------------------------------------------------------ |
---|
159 | void World::setContactSurfaceLayer(Real layer) |
---|
160 | { |
---|
161 | dWorldSetContactSurfaceLayer(_world,(dReal)layer); |
---|
162 | } |
---|
163 | |
---|
164 | //------------------------------------------------------------------------------------------------ |
---|
165 | void World::collisionCallback(void *data,dGeomID geom_a,dGeomID geom_b) |
---|
166 | { |
---|
167 | const bool a_space = (dGeomIsSpace(geom_a))?true:false; |
---|
168 | const bool b_space = (dGeomIsSpace(geom_b))?true:false; |
---|
169 | |
---|
170 | void* const ptr_a = dGeomGetData(geom_a); |
---|
171 | void* const ptr_b = dGeomGetData(geom_b); |
---|
172 | |
---|
173 | if(a_space || b_space ) |
---|
174 | { |
---|
175 | // Collide a space with a space |
---|
176 | if(a_space && b_space) |
---|
177 | static_cast<Space*>(ptr_a)->collide(static_cast<Space*>(ptr_b),data); |
---|
178 | else if(a_space) |
---|
179 | static_cast<Space*>(ptr_a)->collide(static_cast<Geometry*>(ptr_b),data); |
---|
180 | else |
---|
181 | static_cast<Space*>(ptr_b)->collide(static_cast<Geometry*>(ptr_a),data); |
---|
182 | |
---|
183 | // Collide geometries internal to the spaces |
---|
184 | if(a_space) |
---|
185 | static_cast<Space*>(ptr_a)->collide(data); |
---|
186 | |
---|
187 | if(b_space) |
---|
188 | static_cast<Space*>(ptr_b)->collide(data); |
---|
189 | } |
---|
190 | else |
---|
191 | { |
---|
192 | // Collide a geom with a geom, i.e. generate contacts |
---|
193 | static_cast<Geometry*>(ptr_a)->collide(static_cast<Geometry*>(ptr_b),_collision_listener); |
---|
194 | } |
---|
195 | } |
---|
196 | //------------------------------------------------------------------------------------------------ |
---|
197 | Body* World::findBody(SceneNode* node) |
---|
198 | { |
---|
199 | Body* body = 0; |
---|
200 | for(int i = 0;i < node->numAttachedObjects();i++) |
---|
201 | { |
---|
202 | MovableObject* obj = node->getAttachedObject(i); |
---|
203 | if(obj) |
---|
204 | { |
---|
205 | if(obj->getMovableType() == Ogre::String("OgreOde::Body")) |
---|
206 | { |
---|
207 | body = static_cast<Body*>(obj); |
---|
208 | break; |
---|
209 | } |
---|
210 | } |
---|
211 | } |
---|
212 | return body; |
---|
213 | } |
---|
214 | //------------------------------------------------------------------------------------------------ |
---|
215 | Body* World::findBody(const Ogre::String& name) |
---|
216 | { |
---|
217 | Body *b = 0; |
---|
218 | MaintainedItemIterator<Body> it = _body_list.getIterator(); |
---|
219 | while(!it.end ()) |
---|
220 | { |
---|
221 | b = (Body*) (it.getNext ()); |
---|
222 | if(b->getName() == name) |
---|
223 | return b; |
---|
224 | } |
---|
225 | return 0; |
---|
226 | } |
---|
227 | //------------------------------------------------------------------------------------------------ |
---|
228 | void World::setDefaultSpace(Space* space) |
---|
229 | { |
---|
230 | delete _default_space; |
---|
231 | _default_space = space; |
---|
232 | } |
---|
233 | //------------------------------------------------------------------------------------------------ |
---|
234 | void World::setQuickStepIterations(int iterations) |
---|
235 | { |
---|
236 | dWorldSetQuickStepNumIterations(_world,iterations); |
---|
237 | } |
---|
238 | //------------------------------------------------------------------------------------------------ |
---|
239 | int World::getQuickStepIterations() |
---|
240 | { |
---|
241 | return dWorldGetQuickStepNumIterations(_world); |
---|
242 | } |
---|
243 | //------------------------------------------------------------------------------------------------ |
---|
244 | void World::setShowDebugGeometries(bool show) |
---|
245 | { |
---|
246 | _body_list.setDebug(show); |
---|
247 | _geometry_list.setDebug(show); |
---|
248 | _show_debug_geoms = show; |
---|
249 | } |
---|
250 | //------------------------------------------------------------------------------------------------ |
---|
251 | void World::setShowDebugContact(bool show) |
---|
252 | { |
---|
253 | _geometry_list.setDebugContact(show); |
---|
254 | _show_debug_contact = show; |
---|
255 | } |
---|
256 | //------------------------------------------------------------------------------------------------ |
---|
257 | void World::setDamping(Real linear_damping,Real angular_damping) |
---|
258 | { |
---|
259 | _linear_damping = -(dReal)linear_damping; |
---|
260 | _angular_damping = -(dReal)angular_damping; |
---|
261 | } |
---|
262 | //------------------------------------------------------------------------------------------------ |
---|
263 | Real World::getLinearDamping() |
---|
264 | { |
---|
265 | return -(Real)_linear_damping; |
---|
266 | } |
---|
267 | //------------------------------------------------------------------------------------------------ |
---|
268 | Real World::getAngularDamping() |
---|
269 | { |
---|
270 | return -(Real)_angular_damping; |
---|
271 | } |
---|
272 | //------------------------------------------------------------------------------------------------ |
---|
273 | World::~World() |
---|
274 | { |
---|
275 | delete _default_space; |
---|
276 | dJointGroupDestroy(_contacts); |
---|
277 | dWorldDestroy(_world); |
---|
278 | dCloseODE(); |
---|
279 | } |
---|