2 * OPCODE - Optimized Collision Detection
3 * http://www.codercorner.com/Opcode.htm
5 * Copyright (c) 2001-2008 Pierre Terdiman, pierre@codercorner.com
7 This software is provided 'as-is', without any express or implied warranty.
8 In no event will the authors be held liable for any damages arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it freely,
11 subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
15 3. This notice may not be removed or altered from any source distribution.
18 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
20 * Contains code for an OBB collider.
21 * \file OPC_OBBCollider.h
22 * \author Pierre Terdiman
23 * \date January, 1st, 2002
25 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
27 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
29 #ifndef __OPC_OBBCOLLIDER_H__
30 #define __OPC_OBBCOLLIDER_H__
32 struct OPCODE_API OBBCache : VolumeCache
34 OBBCache() : FatCoeff(1.1f)
36 FatBox.mCenter.Zero();
37 FatBox.mExtents.Zero();
38 FatBox.mRot.Identity();
41 // Cached faces signature
42 OBB FatBox; //!< Box used when performing the query resulting in cached faces
44 float FatCoeff; //!< extents multiplier used to create a fat box
47 class OPCODE_API OBBCollider : public VolumeCollider
50 // Constructor / Destructor
52 virtual ~OBBCollider();
54 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
56 * Generic collision query for generic OPCODE models. After the call, access the results:
57 * - with GetContactStatus()
58 * - with GetNbTouchedPrimitives()
59 * - with GetTouchedPrimitives()
61 * \param cache [in/out] a box cache
62 * \param box [in] collision OBB in local space
63 * \param model [in] Opcode model to collide with
64 * \param worldb [in] OBB's world matrix, or null
65 * \param worldm [in] model's world matrix, or null
66 * \return true if success
67 * \warning SCALE NOT SUPPORTED. The matrices must contain rotation & translation parts only.
69 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
70 bool Collide(OBBCache& cache, const OBB& box, const Model& model, const Matrix4x4* worldb=null, const Matrix4x4* worldm=null);
74 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
76 * Settings: select between full box-box tests or "SAT-lite" tests (where Class III axes are discarded)
77 * \param flag [in] true for full tests, false for coarse tests
79 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
80 inline_ void SetFullBoxBoxTest(bool flag) { mFullBoxBoxTest = flag; }
84 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
86 * Validates current settings. You should call this method after all the settings and callbacks have been defined for a collider.
87 * \return null if everything is ok, else a string describing the problem
89 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
90 override(Collider) const char* ValidateSettings();
94 Matrix3x3 mAR; //!< Absolute rotation matrix
95 Matrix3x3 mRModelToBox; //!< Rotation from model space to obb space
96 Matrix3x3 mRBoxToModel; //!< Rotation from obb space to model space
97 Point mTModelToBox; //!< Translation from model space to obb space
98 Point mTBoxToModel; //!< Translation from obb space to model space
101 Point mB0; //!< - mTModelToBox + mBoxExtents
102 Point mB1; //!< - mTModelToBox - mBoxExtents
119 Point mLeafVerts[3]; //!< Triangle vertices
121 bool mFullBoxBoxTest; //!< Perform full BV-BV tests (true) or SAT-lite tests (false)
123 void _Collide(const AABBCollisionNode* node);
124 void _Collide(const AABBNoLeafNode* node);
125 void _Collide(const AABBQuantizedNode* node);
126 void _Collide(const AABBQuantizedNoLeafNode* node);
127 void _CollideNoPrimitiveTest(const AABBCollisionNode* node);
128 void _CollideNoPrimitiveTest(const AABBNoLeafNode* node);
129 void _CollideNoPrimitiveTest(const AABBQuantizedNode* node);
130 void _CollideNoPrimitiveTest(const AABBQuantizedNoLeafNode* node);
132 inline_ BOOL OBBContainsBox(const Point& bc, const Point& be);
133 inline_ BOOL BoxBoxOverlap(const Point& extents, const Point& center);
134 inline_ BOOL TriBoxOverlap();
136 BOOL InitQuery(OBBCache& cache, const OBB& box, const Matrix4x4* worldb=null, const Matrix4x4* worldm=null);
139 class OPCODE_API HybridOBBCollider : public OBBCollider
142 // Constructor / Destructor
144 virtual ~HybridOBBCollider();
146 bool Collide(OBBCache& cache, const OBB& box, const HybridModel& model, const Matrix4x4* worldb=null, const Matrix4x4* worldm=null);
148 Container mTouchedBoxes;
151 #endif // __OPC_OBBCOLLIDER_H__