1 | |
---|
2 | #include "OgreOdePrecompiledHeaders.h" |
---|
3 | |
---|
4 | #include "OgreOdeBody.h" |
---|
5 | #include "OgreOdeSpace.h" |
---|
6 | #include "OgreOdeGeometry.h" |
---|
7 | #include "OgreOdeWorld.h" |
---|
8 | |
---|
9 | #include "OgreOdeCollision.h" |
---|
10 | |
---|
11 | using namespace OgreOde; |
---|
12 | using namespace Ogre; |
---|
13 | |
---|
14 | //------------------------------------------------------------------------------------------------ |
---|
15 | Space::Space(World *world, const Space* space) : |
---|
16 | _world(world), |
---|
17 | _internal_collisions (true) |
---|
18 | { |
---|
19 | } |
---|
20 | //------------------------------------------------------------------------------------------------ |
---|
21 | dSpaceID Space::getSpaceID() const |
---|
22 | { |
---|
23 | return _space; |
---|
24 | } |
---|
25 | //------------------------------------------------------------------------------------------------ |
---|
26 | dSpaceID Space::getSpaceID(const Space* space) const |
---|
27 | { |
---|
28 | if(space) |
---|
29 | return space->getSpaceID(); |
---|
30 | return 0; |
---|
31 | } |
---|
32 | //------------------------------------------------------------------------------------------------ |
---|
33 | void Space::setAutoCleanup(bool on) |
---|
34 | { |
---|
35 | dSpaceSetCleanup(_space,(on)?1:0); |
---|
36 | } |
---|
37 | //------------------------------------------------------------------------------------------------ |
---|
38 | bool Space::getAutoCleanup() |
---|
39 | { |
---|
40 | return ((dSpaceGetCleanup(_space))?true:false); |
---|
41 | } |
---|
42 | //------------------------------------------------------------------------------------------------ |
---|
43 | void Space::addGeometry(const Geometry& geometry) |
---|
44 | { |
---|
45 | dSpaceAdd(_space,geometry.getGeometryID()); |
---|
46 | } |
---|
47 | //------------------------------------------------------------------------------------------------ |
---|
48 | void Space::removeGeometry(const Geometry& geometry) |
---|
49 | { |
---|
50 | dSpaceRemove(_space,geometry.getGeometryID()); |
---|
51 | } |
---|
52 | //------------------------------------------------------------------------------------------------ |
---|
53 | bool Space::containsGeometry(const Geometry& geometry) |
---|
54 | { |
---|
55 | return ((dSpaceQuery(_space,geometry.getGeometryID()))?true:false); |
---|
56 | } |
---|
57 | //------------------------------------------------------------------------------------------------ |
---|
58 | int Space::getGeometryCount() |
---|
59 | { |
---|
60 | return dSpaceGetNumGeoms(_space); |
---|
61 | } |
---|
62 | //------------------------------------------------------------------------------------------------ |
---|
63 | Geometry* Space::getGeometry(int index) |
---|
64 | { |
---|
65 | return (Geometry*) _world->getGeometryList().findItem((long unsigned int)dSpaceGetGeom(_space,index)); |
---|
66 | } |
---|
67 | //------------------------------------------------------------------------------------------------ |
---|
68 | void Space::registerSpace() |
---|
69 | { |
---|
70 | _world->getSpaceList().registerItem(this); |
---|
71 | dGeomSetData((dGeomID)_space,(void*)this); |
---|
72 | } |
---|
73 | //------------------------------------------------------------------------------------------------ |
---|
74 | void Space::collide(void* data) |
---|
75 | { |
---|
76 | if(_internal_collisions) |
---|
77 | { |
---|
78 | dSpaceCollide(_space,data,World::collisionCallback); |
---|
79 | } |
---|
80 | } |
---|
81 | //------------------------------------------------------------------------------------------------ |
---|
82 | void Space::collide(Space* space,void* data) |
---|
83 | { |
---|
84 | dSpaceCollide2((dGeomID)_space,(dGeomID)(space->getSpaceID()),data,World::collisionCallback); |
---|
85 | } |
---|
86 | //------------------------------------------------------------------------------------------------ |
---|
87 | void Space::collide(Geometry* geometry,void* data) |
---|
88 | { |
---|
89 | dSpaceCollide2((dGeomID)_space,geometry->getGeometryID(),data,World::collisionCallback); |
---|
90 | } |
---|
91 | |
---|
92 | //------------------------------------------------------------------------------------------------ |
---|
93 | void Space::collide(CollisionCallback* colCallback) |
---|
94 | { |
---|
95 | if(_internal_collisions) |
---|
96 | { |
---|
97 | void* data = colCallback; |
---|
98 | dSpaceCollide(_space,data, CollisionCallback::collisionCallback); |
---|
99 | } |
---|
100 | } |
---|
101 | //------------------------------------------------------------------------------------------------ |
---|
102 | void Space::collide(CollisionCallback* colCallback, Space* space) |
---|
103 | { |
---|
104 | void* data = colCallback; |
---|
105 | dSpaceCollide2((dGeomID)_space,(dGeomID)(space->getSpaceID()),data, CollisionCallback::collisionCallback); |
---|
106 | } |
---|
107 | //------------------------------------------------------------------------------------------------ |
---|
108 | void Space::collide(CollisionCallback* colCallback, Geometry* geometry) |
---|
109 | { |
---|
110 | void* data = colCallback; |
---|
111 | dSpaceCollide2((dGeomID)_space, geometry->getGeometryID(), data, CollisionCallback::collisionCallback); |
---|
112 | } |
---|
113 | |
---|
114 | //------------------------------------------------------------------------------------------------ |
---|
115 | void Space::setInternalCollisions(bool collide) |
---|
116 | { |
---|
117 | _internal_collisions = collide; |
---|
118 | } |
---|
119 | //------------------------------------------------------------------------------------------------ |
---|
120 | bool Space::getInternalCollisions() |
---|
121 | { |
---|
122 | return _internal_collisions; |
---|
123 | } |
---|
124 | //------------------------------------------------------------------------------------------------ |
---|
125 | const AxisAlignedBox& Space::getAxisAlignedBox() |
---|
126 | { |
---|
127 | dReal aabb[6]; |
---|
128 | dGeomGetAABB((dGeomID)_space,aabb); |
---|
129 | _bounding_box.setExtents((Real)aabb[0],(Real)aabb[2],(Real)aabb[4],(Real)aabb[1],(Real)aabb[3],(Real)aabb[5]); |
---|
130 | return _bounding_box; |
---|
131 | } |
---|
132 | //------------------------------------------------------------------------------------------------ |
---|
133 | unsigned long Space::getID() |
---|
134 | { |
---|
135 | return (unsigned long)_space; |
---|
136 | } |
---|
137 | //------------------------------------------------------------------------------------------------ |
---|
138 | Space::~Space() |
---|
139 | { |
---|
140 | _world->getSpaceList().unregisterItem((unsigned long)_space); |
---|
141 | dSpaceDestroy(_space); |
---|
142 | } |
---|
143 | //------------------------------------------------------------------------------------------------ |
---|
144 | SimpleSpace::SimpleSpace(World *world, const Space* space) : Space (world, space) |
---|
145 | { |
---|
146 | _space = dSimpleSpaceCreate(getSpaceID(space)); |
---|
147 | registerSpace(); |
---|
148 | } |
---|
149 | //------------------------------------------------------------------------------------------------ |
---|
150 | SimpleSpace::~SimpleSpace() |
---|
151 | { |
---|
152 | } |
---|
153 | //------------------------------------------------------------------------------------------------ |
---|
154 | HashTableSpace::HashTableSpace(World *world, const Space* space) : Space (world, space) |
---|
155 | { |
---|
156 | _space = dHashSpaceCreate(getSpaceID(space)); |
---|
157 | registerSpace(); |
---|
158 | } |
---|
159 | //------------------------------------------------------------------------------------------------ |
---|
160 | void HashTableSpace::setLevels(int min_level,int max_level) |
---|
161 | { |
---|
162 | dHashSpaceSetLevels(_space,min_level,max_level); |
---|
163 | } |
---|
164 | //------------------------------------------------------------------------------------------------ |
---|
165 | int HashTableSpace::getMinimumLevel() |
---|
166 | { |
---|
167 | int min_level,max_level; |
---|
168 | dHashSpaceGetLevels(_space,&min_level,&max_level); |
---|
169 | return min_level; |
---|
170 | } |
---|
171 | //------------------------------------------------------------------------------------------------ |
---|
172 | int HashTableSpace::getMaximumLevel() |
---|
173 | { |
---|
174 | int min_level,max_level; |
---|
175 | dHashSpaceGetLevels(_space,&min_level,&max_level); |
---|
176 | return max_level; |
---|
177 | } |
---|
178 | //------------------------------------------------------------------------------------------------ |
---|
179 | HashTableSpace::~HashTableSpace() |
---|
180 | { |
---|
181 | } |
---|
182 | //------------------------------------------------------------------------------------------------ |
---|
183 | QuadTreeSpace::QuadTreeSpace(const Ogre::Vector3& center,const Ogre::Vector3& extents,int depth,World *world, const Space* space) : Space (world, space) |
---|
184 | { |
---|
185 | dVector3 c,e; |
---|
186 | |
---|
187 | c[0] = (dReal)center.x; |
---|
188 | c[1] = (dReal)center.y; |
---|
189 | c[2] = (dReal)center.z; |
---|
190 | |
---|
191 | e[0] = (dReal)extents.x; |
---|
192 | e[1] = (dReal)extents.y; |
---|
193 | e[2] = (dReal)extents.z; |
---|
194 | |
---|
195 | _space = dQuadTreeSpaceCreate(getSpaceID(space),c,e,depth); |
---|
196 | registerSpace(); |
---|
197 | } |
---|
198 | //------------------------------------------------------------------------------------------------ |
---|
199 | QuadTreeSpace::~QuadTreeSpace() |
---|
200 | { |
---|
201 | } |
---|
202 | //------------------------------------------------------------------------------------------------ |
---|
203 | OgreSceneManagerSpace::OgreSceneManagerSpace(const Ogre::Vector3& center, |
---|
204 | const Ogre::Vector3& extents, |
---|
205 | int depth, |
---|
206 | Ogre::SceneManager *_scn_mgr, |
---|
207 | World *world, const Space* space) : |
---|
208 | Space (world, space), |
---|
209 | _scn_mgr(_scn_mgr) |
---|
210 | { |
---|
211 | _intersection_query = _scn_mgr->createIntersectionQuery(); |
---|
212 | |
---|
213 | const std::set<Ogre::SceneQuery::WorldFragmentType> *supportedQueryTypes = _intersection_query->getSupportedWorldFragmentTypes(); |
---|
214 | std::set<Ogre::SceneQuery::WorldFragmentType>::const_iterator it = |
---|
215 | supportedQueryTypes->find (Ogre::SceneQuery::WFT_PLANE_BOUNDED_REGION); |
---|
216 | if (it != supportedQueryTypes->end()) |
---|
217 | { |
---|
218 | _intersection_query->setWorldFragmentType(Ogre::SceneQuery::WFT_PLANE_BOUNDED_REGION); |
---|
219 | _scene_geometry = true; |
---|
220 | } |
---|
221 | else |
---|
222 | { |
---|
223 | _intersection_query->setWorldFragmentType(Ogre::SceneQuery::WFT_NONE); |
---|
224 | _scene_geometry = false; |
---|
225 | } |
---|
226 | |
---|
227 | // for now register a dummy space in ode. |
---|
228 | // perhaps this step can be avoided.dVector3 c,e; |
---|
229 | |
---|
230 | dVector3 c; |
---|
231 | c[0] = (dReal)center.x; |
---|
232 | c[1] = (dReal)center.y; |
---|
233 | c[2] = (dReal)center.z; |
---|
234 | |
---|
235 | dVector3 e; |
---|
236 | e[0] = (dReal)extents.x; |
---|
237 | e[1] = (dReal)extents.y; |
---|
238 | e[2] = (dReal)extents.z; |
---|
239 | |
---|
240 | _space = dQuadTreeSpaceCreate(getSpaceID(space), c, e,depth); |
---|
241 | registerSpace(); |
---|
242 | } |
---|
243 | //------------------------------------------------------------------------------------------------ |
---|
244 | OgreSceneManagerSpace::~OgreSceneManagerSpace() |
---|
245 | { |
---|
246 | delete _intersection_query; |
---|
247 | } |
---|
248 | //------------------------------------------------------------------------------------------------ |
---|
249 | void OgreSceneManagerSpace::collide(void* data) |
---|
250 | { |
---|
251 | if(_internal_collisions) |
---|
252 | { |
---|
253 | // Collision detection |
---|
254 | Ogre::IntersectionSceneQueryResult& results = _intersection_query->execute(); |
---|
255 | |
---|
256 | Body *body1, *body2; |
---|
257 | Geometry *geom; |
---|
258 | Ogre::UserDefinedObject *uo1, *uo2; |
---|
259 | |
---|
260 | // Movables to Movables |
---|
261 | Ogre::SceneQueryMovableIntersectionList::iterator it, itend; |
---|
262 | itend = results.movables2movables.end(); |
---|
263 | for (it = results.movables2movables.begin(); it != itend; ++it) |
---|
264 | { |
---|
265 | /* debugging |
---|
266 | MovableObject *mo1, *mo2; |
---|
267 | mo1 = it->first; |
---|
268 | mo2 = it->second; |
---|
269 | */ |
---|
270 | |
---|
271 | // Get user defined objects (generic in OGRE) |
---|
272 | uo1 = it->first->getUserObject(); |
---|
273 | uo2 = it->second->getUserObject(); |
---|
274 | |
---|
275 | // Only perform collision if we have UserDefinedObject links |
---|
276 | if (uo1 && uo2) |
---|
277 | { |
---|
278 | bool isBody1 = false; |
---|
279 | if (uo1->getTypeName () == "Body") |
---|
280 | isBody1 = true; |
---|
281 | |
---|
282 | bool isBody2 = false; |
---|
283 | if (uo2->getTypeName () == "Body") |
---|
284 | isBody2 = true; |
---|
285 | if (isBody1 || isBody2) |
---|
286 | { |
---|
287 | if (isBody2 && isBody1) |
---|
288 | { |
---|
289 | // Both are dynamic object |
---|
290 | body1 = static_cast<Body*>(uo1); |
---|
291 | body2 = static_cast<Body*>(uo2); |
---|
292 | |
---|
293 | // Do detailed collision test |
---|
294 | body1->collide (data, body2); |
---|
295 | } |
---|
296 | else |
---|
297 | { |
---|
298 | // Both are dynamic object |
---|
299 | if (isBody1) |
---|
300 | { |
---|
301 | body1 = static_cast<Body*> (uo1); |
---|
302 | geom = static_cast<Geometry*> (uo2); |
---|
303 | } |
---|
304 | else |
---|
305 | { |
---|
306 | geom = static_cast<Geometry*> (uo1); |
---|
307 | body1 = static_cast<Body*> (uo2); |
---|
308 | } |
---|
309 | |
---|
310 | // Do detailed collision test |
---|
311 | body1->collide(data, geom); |
---|
312 | } |
---|
313 | } |
---|
314 | } |
---|
315 | } |
---|
316 | |
---|
317 | // Movables to World |
---|
318 | if (_scene_geometry) |
---|
319 | { |
---|
320 | Ogre::MovableObject *mo; |
---|
321 | Ogre::SceneQuery::WorldFragment *wf; |
---|
322 | |
---|
323 | Ogre::SceneQueryMovableWorldFragmentIntersectionList::iterator wit, witend; |
---|
324 | witend = results.movables2world.end(); |
---|
325 | for (wit = results.movables2world.begin(); wit != witend; ++wit) |
---|
326 | { |
---|
327 | mo = wit->first; |
---|
328 | wf = wit->second; |
---|
329 | |
---|
330 | // Get user defined objects (generic in OGRE) |
---|
331 | uo1 = mo->getUserObject(); |
---|
332 | // Only perform collision if we have UserDefinedObject link |
---|
333 | // otherwise ... |
---|
334 | if (uo1) |
---|
335 | { |
---|
336 | // Cast to ApplicationObject |
---|
337 | if (uo1->getTypeName () == "Body") |
---|
338 | { |
---|
339 | body1 = static_cast<Body*>(uo1); |
---|
340 | body1->collidePlaneBounds(data, wf); |
---|
341 | } |
---|
342 | // else // static objects don't collide against Scene Geometry |
---|
343 | // { |
---|
344 | // geom = static_cast<Geometry*>(uo); |
---|
345 | // // Do detailed collision test |
---|
346 | // } |
---|
347 | } |
---|
348 | } |
---|
349 | } |
---|
350 | // no need to use that one. |
---|
351 | // dSpaceCollide(_space,data, World::collisionCallback); |
---|
352 | } |
---|
353 | } |
---|