Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / CDTestFramework / Opcode / OPC_SphereCollider.h
1 /*
2  *      OPCODE - Optimized Collision Detection
3  * http://www.codercorner.com/Opcode.htm
4  * 
5  * Copyright (c) 2001-2008 Pierre Terdiman,  pierre@codercorner.com
6
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:
12
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.
16 */
17
18 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
19 /**
20  *      Contains code for a sphere collider.
21  *      \file           OPC_SphereCollider.h
22  *      \author         Pierre Terdiman
23  *      \date           June, 2, 2001
24  */
25 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26
27 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
28 // Include Guard
29 #ifndef __OPC_SPHERECOLLIDER_H__
30 #define __OPC_SPHERECOLLIDER_H__
31
32         struct OPCODE_API SphereCache : VolumeCache
33         {
34                                         SphereCache() : Center(0.0f,0.0f,0.0f), FatRadius2(0.0f), FatCoeff(1.1f)        {}
35                                         ~SphereCache()                                                                                                                          {}
36
37                 // Cached faces signature
38                 Point           Center;         //!< Sphere used when performing the query resulting in cached faces
39                 float           FatRadius2;     //!< Sphere used when performing the query resulting in cached faces
40                 // User settings
41                 float           FatCoeff;       //!< mRadius2 multiplier used to create a fat sphere
42         };
43
44         class OPCODE_API SphereCollider : public VolumeCollider
45         {
46                 public:
47                 // Constructor / Destructor
48                                                                                         SphereCollider();
49                 virtual                                                         ~SphereCollider();
50
51                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
52                 /**
53                  *      Generic collision query for generic OPCODE models. After the call, access the results:
54                  *      - with GetContactStatus()
55                  *      - with GetNbTouchedPrimitives()
56                  *      - with GetTouchedPrimitives()
57                  *
58                  *      \param          cache                   [in/out] a sphere cache
59                  *      \param          sphere                  [in] collision sphere in local space
60                  *      \param          model                   [in] Opcode model to collide with
61                  *      \param          worlds                  [in] sphere's world matrix, or null
62                  *      \param          worldm                  [in] model's world matrix, or null
63                  *      \return         true if success
64                  *      \warning        SCALE NOT SUPPORTED. The matrices must contain rotation & translation parts only.
65                  */
66                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
67                                                         bool                    Collide(SphereCache& cache, const Sphere& sphere, const Model& model, const Matrix4x4* worlds=null, const Matrix4x4* worldm=null);
68
69                 // 
70                                                         bool                    Collide(SphereCache& cache, const Sphere& sphere, const AABBTree* tree);
71                 protected:
72                 // Sphere in model space
73                                                         Point                   mCenter;                        //!< Sphere center
74                                                         float                   mRadius2;                       //!< Sphere radius squared
75                 // Internal methods
76                                                         void                    _Collide(const AABBCollisionNode* node);
77                                                         void                    _Collide(const AABBNoLeafNode* node);
78                                                         void                    _Collide(const AABBQuantizedNode* node);
79                                                         void                    _Collide(const AABBQuantizedNoLeafNode* node);
80                                                         void                    _Collide(const AABBTreeNode* node);
81                                                         void                    _CollideNoPrimitiveTest(const AABBCollisionNode* node);
82                                                         void                    _CollideNoPrimitiveTest(const AABBNoLeafNode* node);
83                                                         void                    _CollideNoPrimitiveTest(const AABBQuantizedNode* node);
84                                                         void                    _CollideNoPrimitiveTest(const AABBQuantizedNoLeafNode* node);
85                         // Overlap tests
86                 inline_                         BOOL                    SphereContainsBox(const Point& bc, const Point& be);
87                 inline_                         BOOL                    SphereAABBOverlap(const Point& center, const Point& extents);
88                                                         BOOL                    SphereTriOverlap(const Point& vert0, const Point& vert1, const Point& vert2);
89                         // Init methods
90                                                         BOOL                    InitQuery(SphereCache& cache, const Sphere& sphere, const Matrix4x4* worlds=null, const Matrix4x4* worldm=null);
91         };
92
93         class OPCODE_API HybridSphereCollider : public SphereCollider
94         {
95                 public:
96                 // Constructor / Destructor
97                                                                                         HybridSphereCollider();
98                 virtual                                                         ~HybridSphereCollider();
99
100                                                         bool                    Collide(SphereCache& cache, const Sphere& sphere, const HybridModel& model, const Matrix4x4* worlds=null, const Matrix4x4* worldm=null);
101                 protected:
102                                                         Container               mTouchedBoxes;
103         };
104
105 #endif // __OPC_SPHERECOLLIDER_H__