~ubuntu-branches/debian/sid/ember/sid

« back to all changes in this revision

Viewing changes to src/components/ogre/ogreopcode/src/OgreCollisionContext.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2009-10-22 23:21:17 UTC
  • mfrom: (1.1.1 upstream) (2.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20091022232117-isr8u3402qmu7ilo
Tags: 0.5.7-1
* New upstream release.
  - Compile against current ogre (Closes: #551431)
  - Removed debian/patches/ember-gcc4.4.patch. Merged upstream.
  - Updated Depends on ember-media.
* Add libboost-thread-dev tp Build-Depends.
* Make debian/rules independent from upstream version.
* Updated watch file to allow automatic download of new upstream
  tarballs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
///////////////////////////////////////////////////////////////////////////////
2
 
///  @file OgreCollisionContext.cpp
3
 
///  @brief <TODO: insert file description here>
4
 
///
5
 
///  @author The OgreOpcode Team
6
 
///  
7
 
///////////////////////////////////////////////////////////////////////////////
8
 
///  
9
 
///  This file is part of OgreOpcode.
10
 
///  
11
 
///  A lot of the code is based on the Nebula Opcode Collision module, see docs/Nebula_license.txt
12
 
///  
13
 
///  OgreOpcode is free software; you can redistribute it and/or
14
 
///  modify it under the terms of the GNU Lesser General Public
15
 
///  License as published by the Free Software Foundation; either
16
 
///  version 2.1 of the License, or (at your option) any later version.
17
 
///  
18
 
///  OgreOpcode is distributed in the hope that it will be useful,
19
 
///  but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 
///  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21
 
///  Lesser General Public License for more details.
22
 
///  
23
 
///  You should have received a copy of the GNU Lesser General Public
24
 
///  License along with OgreOpcode; if not, write to the Free Software
25
 
///  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 
///  
27
 
///////////////////////////////////////////////////////////////////////////////
28
 
#include "OgreCollisionContext.h"
29
 
#include "OgreCollisionObject.h"
30
 
#include "OgreOpcodeMath.h"
31
 
#include "OgreCollisionManager.h"
32
 
#include "BP_Scene.h"
33
 
 
34
 
using namespace Ogre;
35
 
using namespace OgreOpcode::Details;
36
 
 
37
 
namespace OgreOpcode
38
 
{
39
 
        namespace Details
40
 
        {
41
 
                inline bool intervalOverlap(Real a0, Real a1, Real b0, Real b1)
42
 
                {
43
 
                        // Only two ways for intervals to not overlap -- 
44
 
                        //  a's max less than b's min, or a's min greater than b's max.
45
 
                        // Otherwise they overlap.
46
 
                        // return !(a1<b0 || a0>b1);
47
 
                        // I just applied the De Morgan's law here in order to obtain short-circuit
48
 
                        return (a1>=b0) && (a0<=b1);
49
 
                }
50
 
        } // Details
51
 
 
52
 
        // release all owned collide objects
53
 
        CollisionContext::~CollisionContext()
54
 
        {
55
 
                // remove collision objects
56
 
                while (!owned_list.empty())
57
 
                {
58
 
                        destroyObject(*(owned_list.begin()));
59
 
                }
60
 
                delete mVisualDebugger;
61
 
                delete mBroadPhase;
62
 
        }
63
 
 
64
 
        // Construct a new collide object.
65
 
        CollisionObject *CollisionContext::createObject(const Ogre::String& name)
66
 
        {
67
 
                CollisionObject *co = new CollisionObject(name);
68
 
                co->setId(unique_id++);
69
 
                co->setContext(this);
70
 
                owned_list.push_back(co);
71
 
                return co;
72
 
        }
73
 
 
74
 
        CollisionContext::CollisionContext(const Ogre::String& name) :
75
 
        unique_id(0),
76
 
        mName(name),
77
 
        mRayCulling(true),
78
 
        mIsSAPDirty(true)
79
 
        {
80
 
                mVisualDebugger = new Details::OgreOpcodeDebugger(mName, CollisionManager::getSingletonPtr()->getSceneManager());
81
 
                mRecentContactList.clear();
82
 
                mBroadPhase = new BP_Scene(&proxList, &OgreOpcode::CollisionContext::addPair, &OgreOpcode::CollisionContext::removePair);
83
 
        }
84
 
 
85
 
        // Kill an owned collide object.
86
 
        void CollisionContext::destroyObject(CollisionObject *collObj)
87
 
        {
88
 
                if (collObj != 0)
89
 
                {
90
 
                        if(collObj->isAttached())
91
 
                        {
92
 
                                rw_attached_list_iterator itAttached = find(attached_list.begin(), attached_list.end(), collObj);
93
 
                                if (itAttached != attached_list.end())
94
 
                                {
95
 
                                        attached_list.erase(itAttached);
96
 
                                }
97
 
                        }
98
 
                        
99
 
                        rw_owned_list_iterator itOwned = find(owned_list.begin(), owned_list.end(), collObj);
100
 
                        if (itOwned != owned_list.end())
101
 
                        {
102
 
                                owned_list.erase(itOwned);
103
 
                        }
104
 
                        collObj->setAttached(false);
105
 
                        collObj->remove_broadphase();
106
 
                        collObj->setContext(0);
107
 
                        delete collObj;
108
 
                }
109
 
        }
110
 
 
111
 
        void CollisionContext::addObject(CollisionObject *collObj)
112
 
        {
113
 
                if(collObj == 0)
114
 
                        OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, "Trying to add a null object.", "CollisionContext::addObject");
115
 
                
116
 
                attached_list.push_back(collObj);
117
 
                collObj->setAttached(true);
118
 
        }
119
 
 
120
 
        void CollisionContext::removeObject(CollisionObject *collObj)
121
 
        {
122
 
                if (collObj != 0)
123
 
                {
124
 
                        collObj->setAttached(false);
125
 
                        collObj->remove_broadphase();
126
 
                        rw_attached_list_iterator itAttached = find(attached_list.begin(), attached_list.end(), collObj);
127
 
                        if (itAttached != attached_list.end())
128
 
                        {
129
 
                                attached_list.erase(itAttached);
130
 
                        }
131
 
                }
132
 
        }
133
 
 
134
 
        /// Call collide on each object in the context.
135
 
        /// After this, each object's collision array holds all collisions
136
 
        /// this object was involved with.
137
 
        int CollisionContext::collide(Real dt)
138
 
        {
139
 
                update(dt);
140
 
 
141
 
                
142
 
                
143
 
                // first, clear the collision counters in all collide objects
144
 
                for (attached_list_iterator i = attached_list.begin(); i != attached_list.end(); ++i)
145
 
                {
146
 
                        (*i)->clearCollisions();
147
 
                }
148
 
 
149
 
                // then, run the broadphase 
150
 
                for (attached_list_iterator i = attached_list.begin(); i != attached_list.end(); ++i)
151
 
                {
152
 
                        (*i)->do_broadphase();
153
 
                }
154
 
 
155
 
                // Loop through the Potentially Colliding Set and tell each CollisionObject to test against the other
156
 
                for (ProxList::iterator prox_it = proxList.begin(); prox_it != proxList.end(); ++prox_it) 
157
 
                {
158
 
                        // If the shape is marked as being static, do not add it to the PCS.
159
 
                        if(!(*prox_it).obj1->getShape()->isStatic())
160
 
                                (*prox_it).obj1->addToCollideList((*prox_it).obj2);
161
 
                        if(!(*prox_it).obj2->getShape()->isStatic())
162
 
                        (*prox_it).obj2->addToCollideList((*prox_it).obj1);
163
 
                }
164
 
 
165
 
                // check the collision status for each object
166
 
                collideReportHandler.beginFrame();
167
 
                for (attached_list_iterator i = attached_list.begin(); i != attached_list.end(); ++i)
168
 
                {
169
 
                        //if ( (*i)->needsUpdate() )
170
 
                                (*i)->collide();
171
 
                }
172
 
                collideReportHandler.endFrame();
173
 
 
174
 
                int num_coll = collideReportHandler.getNumCollisions();
175
 
                return num_coll;
176
 
        }
177
 
 
178
 
        /// Get all collisions an object is involved in.
179
 
        /// Returns pointer to an internal collision array and
180
 
        /// the number of collisions.
181
 
        int CollisionContext::getCollisions(CollisionObject *collObj, CollisionPair **&cpPtr)
182
 
        {
183
 
                if (collObj->getNumCollisions() > 0)
184
 
                {
185
 
                        return collideReportHandler.getCollisions(collObj,cpPtr);
186
 
                } else
187
 
                {
188
 
                        cpPtr = 0;
189
 
                        return 0;
190
 
                }
191
 
        }
192
 
 
193
 
        CollisionObject* CollisionContext::getAttachedObject(Ogre::String name)
194
 
        {
195
 
                for (attached_list_iterator i = attached_list.begin(); i != attached_list.end(); ++i)
196
 
                {
197
 
                        if( (*i)->getName() == name )
198
 
                                return (*i);
199
 
                }
200
 
                OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "'" + name + "' is not attached! Does it even exsist?", "CollisionContext::getAttachedObject");
201
 
        }
202
 
 
203
 
 
204
 
        /// 
205
 
        const std::list<CollisionObject*>& CollisionContext::getPotentialColliders(const CollisionObject* collidee)
206
 
        {
207
 
                return collidee->collideList;
208
 
        }
209
 
 
210
 
        /// get reporter for for last collide() call.
211
 
        const CollisionReporter& CollisionContext::getCollisionReport()
212
 
        {
213
 
                return collideReportHandler;
214
 
        }
215
 
 
216
 
        /// 
217
 
        const int CollisionContext::getNumCollisions()
218
 
        {
219
 
                return collideReportHandler.getNumCollisions();
220
 
        }
221
 
 
222
 
        /// get reporter for for last Check...() call.
223
 
        const CollisionReporter& CollisionContext::getCheckReport()
224
 
        {
225
 
                return checkReportHandler;
226
 
        }
227
 
 
228
 
        /// visualize all objects in the context.
229
 
        void CollisionContext::visualize(bool doVisualize, bool doRadii, bool doContacts, bool doBBs, bool doShapes, bool doAABBs)
230
 
        {
231
 
                // if the debugger is down, just return
232
 
                if(!mVisualDebugger) return;
233
 
 
234
 
                mVisualDebugger->clearAll();
235
 
                for (attached_list_iterator i = attached_list.begin(); i != attached_list.end(); ++i)
236
 
                {
237
 
                        (*i)->getShape()->clearViz();
238
 
                }
239
 
 
240
 
                if(doVisualize)
241
 
                {
242
 
                        if (!attached_list.empty())
243
 
                        {
244
 
                                if(doRadii)
245
 
                                {
246
 
                                        mVisualDebugger->beginRadii();
247
 
                                        for (attached_list_iterator i = attached_list.begin(); i != attached_list.end(); ++i)
248
 
                                        {
249
 
                                                (*i)->visualizeRadii();
250
 
                                        }
251
 
                                        mVisualDebugger->endRadii();
252
 
                                }
253
 
                                if(doContacts)
254
 
                                {
255
 
                                        if( collideReportHandler.getNumCollisions() > 0)
256
 
                                        {
257
 
                                                mVisualDebugger->beginContacts();
258
 
                                                mVisualDebugger->beginContactNormals();
259
 
                                                for (attached_list_iterator i = attached_list.begin(); i != attached_list.end(); ++i)
260
 
                                                {
261
 
                                                        (*i)->visualizeContacts();
262
 
                                                }
263
 
                                                mVisualDebugger->endContactNormals();
264
 
                                                mVisualDebugger->endContacts();
265
 
                                        }
266
 
                                                
267
 
                                        static int contactCount = 0;
268
 
                                        static bool bufferContacts = true;
269
 
 
270
 
                                        if( checkReportHandler.getNumCollisions() > 0)
271
 
                                        {
272
 
                                                CollisionPair **pick_report;
273
 
                                                int num_picks = checkReportHandler.getAllCollisions(pick_report);
274
 
                                                if(num_picks > 0)
275
 
                                                {
276
 
 
277
 
                                                        for(int i = 0; i < num_picks; i++)
278
 
                                                        {
279
 
                                                                for (int currColl = 0; currColl < static_cast<int>(pick_report[i]->collInfos.size()); currColl++)
280
 
                                                                {
281
 
                                                                        if(bufferContacts)
282
 
                                                                                contactCount++;
283
 
                                                                        CollisionInfo collInfo;
284
 
                                                                        collInfo.contact = pick_report[i]->collInfos[currColl].contact;
285
 
                                                                        collInfo.this_normal = pick_report[i]->collInfos[currColl].this_normal * 5;
286
 
                                                                        collInfo.other_normal = pick_report[i]->collInfos[currColl].other_normal * 5;
287
 
                                                                        mRecentContactList.push_back(collInfo);
288
 
                                                                        if(!bufferContacts)
289
 
                                                                                mRecentContactList.pop_front();
290
 
                                                                }
291
 
                                                        }
292
 
                                                }
293
 
 
294
 
                                                if(contactCount > 10)
295
 
                                                        bufferContacts = false;
296
 
 
297
 
                                        }
298
 
 
299
 
                                        // render any collision contact points
300
 
                                        if (mRecentContactList.size() > 0)
301
 
                                        {
302
 
                                                mVisualDebugger->beginContacts();
303
 
                                                mVisualDebugger->beginContactNormals();
304
 
 
305
 
                                                std::list<CollisionInfo>::iterator contactIter;
306
 
                                                for (contactIter = mRecentContactList.begin(); contactIter != mRecentContactList.end(); ++contactIter)
307
 
                                                {
308
 
                                                        Vector3 cnt = (*contactIter).contact;
309
 
                                                        mVisualDebugger->addContactLine(cnt.x-0.5f,cnt.y,cnt.z, cnt.x+0.5f,cnt.y,cnt.z);
310
 
                                                        mVisualDebugger->addContactLine(cnt.x,cnt.y-0.5f,cnt.z, cnt.x,cnt.y+0.5f,cnt.z);
311
 
                                                        mVisualDebugger->addContactLine(cnt.x,cnt.y,cnt.z-0.5f, cnt.x,cnt.y,cnt.z+0.5f);
312
 
 
313
 
                                                        Vector3 n = (*contactIter).this_normal * 5;
314
 
                                                        mVisualDebugger->addContactNormalsLine(cnt.x,cnt.y,cnt.z, cnt.x+n.x,cnt.y+n.y,cnt.z+n.z);
315
 
                                                        n = (*contactIter).other_normal * 5;
316
 
                                                        mVisualDebugger->addContactNormalsLine(cnt.x,cnt.y,cnt.z, cnt.x+n.x,cnt.y+n.y,cnt.z+n.z);
317
 
 
318
 
                                                }
319
 
                                                
320
 
                                                mVisualDebugger->endContactNormals();
321
 
                                                mVisualDebugger->endContacts();
322
 
                                        }
323
 
 
324
 
                                }
325
 
 
326
 
                                if(doBBs)
327
 
                                {
328
 
                                        mVisualDebugger->beginBBs();
329
 
                                        for (attached_list_iterator i = attached_list.begin(); i != attached_list.end(); ++i)
330
 
                                        {
331
 
                                                (*i)->visualizeBoundingBoxes();
332
 
                                        }
333
 
                                        mVisualDebugger->endBBs();
334
 
                                }
335
 
                                if(doShapes)
336
 
                                {
337
 
                                        //mVisualDebugger->beginShapes();
338
 
 
339
 
                                        for (attached_list_iterator i = attached_list.begin(); i != attached_list.end(); ++i)
340
 
                                        {
341
 
//                                              if( (*i)->hasCollisions() || (*i)->hasCheckCollisions() )
342
 
                                                        (*i)->getShape()->visualize(mVisualDebugger);
343
 
                                        }
344
 
                                        //mVisualDebugger->endShapes();
345
 
                                }
346
 
                                if(doAABBs)
347
 
                                {
348
 
                                        mVisualDebugger->beginAABBs();
349
 
 
350
 
                                        for (attached_list_iterator i = attached_list.begin(); i != attached_list.end(); ++i)
351
 
                                        {
352
 
                                                if( (*i)->hasCollisions() || (*i)->hasCheckCollisions() )
353
 
                                                        (*i)->getShape()->visualizeAABBs(mVisualDebugger);
354
 
                                        }
355
 
                                        mVisualDebugger->endAABBs();
356
 
                                }
357
 
                        }
358
 
                }
359
 
        }
360
 
 
361
 
        /// Do an instant check of a moving sphere in the collision volume.
362
 
        /// Fills out the provided collide report array and
363
 
        /// returns number of detected collisions.
364
 
        /// @param p0     [in] starting position
365
 
        /// @param v0     [in] vector to ending position
366
 
        /// @param radius [in] radius
367
 
        /// @param collClass [in] collision class for collision type query
368
 
        /// @param cr_ptr [out] pointer to array of pointers to CollisionPair's
369
 
        /// @return       number of detected contacts (1 per collide object)
370
 
        int CollisionContext::sweptSphereCheck(const Vector3& position, const Vector3& movementVector, Real radius, CollisionClass collClass, CollisionPair **& cpPtr, String ignorename)
371
 
        {
372
 
                // create a bounding box from the start and end position
373
 
                Vector3 endPosition(position + movementVector);
374
 
                Vector3 minv(n_min(position.x, endPosition.x) - radius, 
375
 
                        n_min(position.y, endPosition.y) - radius, 
376
 
                        n_min(position.z, endPosition.z) - radius);
377
 
                Vector3 maxv(n_max(position.x, endPosition.x) + radius, 
378
 
                        n_max(position.y, endPosition.y) + radius, 
379
 
                        n_max(position.z, endPosition.z) + radius);
380
 
 
381
 
                const int own_id = 0xffff;
382
 
 
383
 
                // initialize collision report handler
384
 
                checkReportHandler.beginFrame();
385
 
 
386
 
                // This simply goes through all attached objects, and
387
 
                // checks them for overlap, so ITS SLOW! Every object is
388
 
                // tested exactly once
389
 
                for (attached_list_iterator other = attached_list.begin(); other != attached_list.end(); ++other)
390
 
                {
391
 
                        // see if we have overlaps in all 3 dimensions
392
 
                        if ((minv.x < (*other)->maxv.x) && (maxv.x > (*other)->minv.x) &&
393
 
                                (minv.y < (*other)->maxv.y) && (maxv.y > (*other)->minv.y) &&
394
 
                                (minv.z < (*other)->maxv.z) && (maxv.z > (*other)->minv.z))
395
 
                        {
396
 
                                // see if the candidate is in the ignore types set
397
 
                                CollisionType ct = CollisionManager::getSingletonPtr()->queryCollType(collClass,(*other)->getCollClass());
398
 
                                if (COLLTYPE_IGNORE == ct) continue;
399
 
 
400
 
                                checkReportHandler.mTotalObjObjTests++;
401
 
                                if((*other)->getName() != ignorename)
402
 
                                {
403
 
                                        if (COLLTYPE_QUICK == ct)
404
 
                                        {
405
 
                                                // Trying to extract position information from provided matrices.
406
 
                                                Vector3 p1 = Vector3((*other)->old_matrix[0][3], (*other)->old_matrix[1][3], (*other)->old_matrix[2][3]);
407
 
                                                Vector3 v1 = Vector3(Vector3((*other)->new_matrix[0][3], (*other)->new_matrix[1][3], (*other)->new_matrix[2][3]) - p1);
408
 
 
409
 
                                                // do the contact check between 2 moving spheres
410
 
                                                sphere s0(position,radius);
411
 
                                                sphere s1(p1,(*other)->getRadius());
412
 
                                                float u0,u1;
413
 
                                                checkReportHandler.mTotalBVBVTests++;
414
 
                                                if (s0.intersect_sweep(movementVector,s1,v1,u0,u1))
415
 
                                                {
416
 
                                                        if ((u0>=0.0f) && (u0<1.0f))
417
 
                                                        {
418
 
                                                                // we have contact!
419
 
 
420
 
                                                                // compute the 2 midpoints at the time of collision
421
 
                                                                Vector3 c0(position + movementVector*u0);
422
 
                                                                Vector3 c1(p1 + v1*u0);
423
 
 
424
 
                                                                // compute the collide normal
425
 
                                                                Vector3 d(c1-c0);
426
 
                                                                if (d.length() > TINY)
427
 
                                                                {
428
 
                                                                        d.normalise();
429
 
                                                                } else
430
 
                                                                {
431
 
                                                                        d = Vector3(0.0f, 1.0f, 0.0f);
432
 
                                                                }
433
 
 
434
 
                                                                // fill out a collide report and add to report handler
435
 
                                                                CollisionPair cr;
436
 
                                                                cr.this_object     = (*other);
437
 
                                                                cr.other_object     = (*other);
438
 
                                                                cr.tstamp  = 0.0;
439
 
                                                                CollisionInfo collInfo;
440
 
                                                                collInfo.contact = (d*radius) + c0;
441
 
                                                                collInfo.this_normal = d;
442
 
                                                                collInfo.other_normal = -d;
443
 
                                                                cr.collInfos.push_back(collInfo);
444
 
                                                                checkReportHandler.addCollision(cr,own_id,(*other)->id);
445
 
                                                        }
446
 
                                                }
447
 
                                        }
448
 
                                        else // CONTACT and EXACT
449
 
                                        {
450
 
                                                // do sphere-shape collision check
451
 
                                                ICollisionShape* shape = (*other)->getShape();
452
 
 
453
 
                                                if (shape)
454
 
                                                {
455
 
                                                        CollisionPair cp;
456
 
                                                        cp.this_object = (*other);
457
 
                                                        cp.other_object = (*other);
458
 
                                                        bool ret = shape->sweptSphereCheck(ct, (*other)->getTransform(), position, movementVector, radius, cp);
459
 
                                                        checkReportHandler.mTotalBVBVTests += cp.numBVBVTests;
460
 
                                                        checkReportHandler.mTotalBVPrimTests += cp.numBVPrimTests;
461
 
                                                        if (ret)
462
 
                                                        {
463
 
                                                                cp.this_object = (*other);
464
 
                                                                cp.other_object = (*other);
465
 
                                                                checkReportHandler.addCollision(cp, own_id, (*other)->id);
466
 
                                                        }
467
 
                                                }
468
 
                                        }
469
 
                                }
470
 
 
471
 
                        }
472
 
                }
473
 
                checkReportHandler.endFrame();
474
 
                return checkReportHandler.getAllCollisions(cpPtr);
475
 
        }
476
 
 
477
 
        /// Test a ray against the collide objects in the collide context.
478
 
        /// The collType will be interpreted as follows:
479
 
        /// - COLLTYPE_IGNORE:        illegal (makes no sense)
480
 
        /// - COLLTYPE_QUICK:         occlusion check only
481
 
        /// - COLLTYPE_CONTACT:       return closest contact only
482
 
        /// - COLLTYPE_EXACT:         return all contacts (unsorted)
483
 
        /// @param  line        [in]  the ray to test in global space
484
 
        /// @param  collType    [in]  the collision type
485
 
        /// @param  collClass   [in]  optional coll class (COLLCLASS_ALWAYS_* if no coll class filtering wanted)
486
 
        /// @param  cpPtr       [out] will be filled with pointer to collide report pointers
487
 
        /// @return             number of detected contacts (1 per collide object)
488
 
        int CollisionContext::rayCheck(const Ogre::Ray line, const Real dist, CollisionType collType, CollisionClass collClass, CollisionPair**& cpPtr)
489
 
        {
490
 
                assert(collType != COLLTYPE_IGNORE);
491
 
 
492
 
                // create a bounding box from the line
493
 
                bbox3 bbox;
494
 
                bbox.begin_grow();
495
 
                bbox.grow(line.getOrigin());
496
 
                bbox.grow(line.getPoint(dist));
497
 
                const int ownId = 0xffff;
498
 
 
499
 
                // initialize collision report handler
500
 
                checkReportHandler.beginFrame();
501
 
 
502
 
                // go through all attached collide objects
503
 
                for (attached_list_iterator co = attached_list.begin(); co != attached_list.end(); ++co)
504
 
                {
505
 
                        Vector3 coMin = (*co)->minv;
506
 
                        Vector3 coMax = (*co)->maxv;
507
 
                        // see if we have overlaps in all 3 dimensions
508
 
                        if (intervalOverlap(bbox.vmin.x,bbox.vmax.x,coMin.x,coMax.x) &&
509
 
                                intervalOverlap(bbox.vmin.y,bbox.vmax.y,coMin.y,coMax.y) &&
510
 
                                intervalOverlap(bbox.vmin.z,bbox.vmax.z,coMin.z,coMax.z) )
511
 
                        {
512
 
                                // see if the candidate is in the ignore types set
513
 
                                CollisionType ct = CollisionManager::getSingletonPtr()->queryCollType(collClass, (*co)->getCollClass());
514
 
                                if (COLLTYPE_IGNORE == ct) 
515
 
                                {
516
 
                                        continue;
517
 
                                }
518
 
 
519
 
 
520
 
                                // check collision
521
 
                                ICollisionShape* shape = (*co)->getShape();
522
 
                                if (shape)
523
 
                                {
524
 
                                        checkReportHandler.mTotalObjObjTests++;
525
 
                                        CollisionPair cp;
526
 
                                        bool ret = shape->rayCheck(collType, (*co)->getTransform(), line, dist, cp, mRayCulling);
527
 
                                        checkReportHandler.mTotalBVBVTests += cp.numBVBVTests;
528
 
                                        checkReportHandler.mTotalBVBVTests += cp.numBVPrimTests;
529
 
                                        if (ret)
530
 
                                        {
531
 
                                                cp.this_object = (*co);
532
 
                                                cp.other_object = (*co);
533
 
                                                checkReportHandler.addCollision(cp, ownId, (*co)->id);
534
 
                                                if (COLLTYPE_QUICK == collType)
535
 
                                                {
536
 
                                                        // break out of loop
537
 
                                                        break;
538
 
                                                }
539
 
                                        }
540
 
                                }
541
 
                        }
542
 
                }
543
 
                checkReportHandler.endFrame();
544
 
 
545
 
                //if (COLLTYPE_CONTACT == collType) // FIXME!
546
 
                //{
547
 
                //      // get closest contact only
548
 
                //      return checkReportHandler.getClosestCollision(line.getOrigin(), cpPtr);
549
 
                //}
550
 
                //else
551
 
                //{
552
 
                        // get all contacts (unsorted)
553
 
                        return checkReportHandler.getAllCollisions(cpPtr);
554
 
                //}
555
 
        }
556
 
 
557
 
        /// Test a sphere against the collide objects in the collide context.
558
 
        /// The collType will be interpreted as follows:
559
 
        /// - COLLTYPE_IGNORE:        illegal (makes no sense)
560
 
        /// - COLLTYPE_QUICK:         return all contacts, do sphere-sphere check
561
 
        /// - COLLTYPE_CONTACT:       return closest contact only, sphere-shape
562
 
        /// - COLLTYPE_EXACT:         return all contacts (unsorted), sphere-shape
563
 
        /// @param  theSphere      [in]  the sphere to test in global space
564
 
        /// @param  collType    [in]  the collision type
565
 
        /// @param  collClass   [in]  optional coll class (COLLCLASS_ALWAYS_* if no coll class filtering wanted)
566
 
        /// @param  cpPtr       [out] will be filled with pointer to collide report pointers
567
 
        /// @return             number of detected contacts (1 per collide object)
568
 
        int CollisionContext::sphereCheck(const Sphere& theSphere, CollisionType collType, CollisionClass collClass, CollisionPair**& cpPtr)
569
 
        {
570
 
                assert(collType != COLLTYPE_IGNORE);
571
 
 
572
 
                sphere ball;
573
 
                ball.set(theSphere.getCenter(),theSphere.getRadius());
574
 
                // create a bounding box from the sphere
575
 
                Vector3 vmin(ball.p.x - ball.r, ball.p.y - ball.r, ball.p.z - ball.r);
576
 
                Vector3 vmax(ball.p.x + ball.r, ball.p.y + ball.r, ball.p.z + ball.r);
577
 
                bbox3 bbox(vmin, vmax);
578
 
                const int ownId = 0xffff;
579
 
 
580
 
                // initialize collision report handler
581
 
                checkReportHandler.beginFrame();
582
 
 
583
 
                // go through all attached collide objects
584
 
                sphere s0;
585
 
                for (attached_list_iterator co = attached_list.begin(); co != attached_list.end(); ++co)
586
 
                {
587
 
                        // see if we have overlaps in all 3 dimensions
588
 
                        if ((bbox.vmin.x < (*co)->maxv.x) && (bbox.vmax.x > (*co)->minv.x) &&
589
 
                                (bbox.vmin.y < (*co)->maxv.y) && (bbox.vmax.y > (*co)->minv.y) &&
590
 
                                (bbox.vmin.z < (*co)->maxv.z) && (bbox.vmax.z > (*co)->minv.z))
591
 
                        {
592
 
                                // see if the candidate is in the ignore types set
593
 
                                CollisionType ct = CollisionManager::getSingletonPtr()->queryCollType(collClass, (*co)->getCollClass());
594
 
                                if (COLLTYPE_IGNORE == ct)
595
 
                                {
596
 
                                        continue;
597
 
                                }
598
 
 
599
 
                                checkReportHandler.mTotalObjObjTests++;
600
 
                                if (COLLTYPE_QUICK == ct)
601
 
                                {
602
 
                                        // do sphere-sphere collision check
603
 
                                        const Matrix4 coTrans = (*co)->getTransform();
604
 
                                        //s0.set(coTrans[0][3], coTrans[1][3], coTrans[2][3], (*co)->getRadius());
605
 
                                        s0.set((*co)->getShape()->getCenter(), (*co)->getRadius());
606
 
                                        checkReportHandler.mTotalBVBVTests++;
607
 
                                        if (ball.intersects(s0))
608
 
                                        {
609
 
                                                CollisionPair cp;
610
 
                                                cp.this_object = (*co);
611
 
                                                cp.other_object = (*co);
612
 
                                                checkReportHandler.addCollision(cp, ownId, (*co)->id);
613
 
                                        }
614
 
                                }
615
 
                                else
616
 
                                {
617
 
                                        // do sphere-shape collision check
618
 
                                        ICollisionShape* shape = (*co)->getShape();
619
 
                                        if (shape)
620
 
                                        {
621
 
                                                CollisionPair cp;
622
 
                                                bool ret = shape->sphereCheck(collType, (*co)->getTransform(), theSphere, cp);
623
 
                                                checkReportHandler.mTotalBVBVTests += cp.numBVBVTests;
624
 
                                                checkReportHandler.mTotalBVPrimTests += cp.numBVPrimTests;
625
 
                                                if (ret)
626
 
                                                {
627
 
                                                        cp.this_object = (*co);
628
 
                                                        cp.other_object = (*co);
629
 
                                                        checkReportHandler.addCollision(cp, ownId, (*co)->id);
630
 
                                                }
631
 
                                        }
632
 
                                }
633
 
                        }
634
 
                }
635
 
                checkReportHandler.endFrame();
636
 
 
637
 
                //if (COLLTYPE_CONTACT == collType)
638
 
                //{
639
 
                //      // get closest contact only
640
 
                //      return checkReportHandler.getClosestCollision(ball.p, cpPtr);
641
 
                //}
642
 
                //else
643
 
                //{
644
 
                        // get all contacts (unsorted)
645
 
                        return checkReportHandler.getAllCollisions(cpPtr);
646
 
                //}
647
 
        }
648
 
 
649
 
 
650
 
        void CollisionContext::update(Real dt)
651
 
        {
652
 
                for (attached_list_iterator co = attached_list.begin(); co != attached_list.end(); ++co)
653
 
                {
654
 
                        (*co)->update(dt);
655
 
                }
656
 
        }
657
 
 
658
 
        /// reset position and timestamp of all attached collide objects to 0.0.
659
 
        /// This is useful at the beginning of a level to prevent phantom collisions
660
 
        /// (when objects are repositioned to their starting point in the level).
661
 
        void CollisionContext::reset()
662
 
        {
663
 
                Matrix4 identity = Matrix4::IDENTITY;
664
 
                for (attached_list_iterator co = attached_list.begin(); co != attached_list.end(); ++co)
665
 
                {
666
 
                        // This must be done twice, so that old timestamp also becomes 0
667
 
                        (*co)->update(-1.0, identity);
668
 
                        (*co)->update(-1.0, identity);
669
 
                }
670
 
 
671
 
        }
672
 
}