Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / CDTestFramework / Opcode / OPC_Collider.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 base collider class.
21  *      \file           OPC_Collider.h
22  *      \author         Pierre Terdiman
23  *      \date           June, 2, 2001
24  */
25 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26
27 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
28 // Include Guard
29 #ifndef __OPC_COLLIDER_H__
30 #define __OPC_COLLIDER_H__
31
32         enum CollisionFlag
33         {
34                 OPC_FIRST_CONTACT               = (1<<0),               //!< Report all contacts (false) or only first one (true)
35                 OPC_TEMPORAL_COHERENCE  = (1<<1),               //!< Use temporal coherence or not
36                 OPC_CONTACT                             = (1<<2),               //!< Final contact status after a collision query
37                 OPC_TEMPORAL_HIT                = (1<<3),               //!< There has been an early exit due to temporal coherence
38                 OPC_NO_PRIMITIVE_TESTS  = (1<<4),               //!< Keep or discard primitive-bv tests in leaf nodes (volume-mesh queries)
39
40                 OPC_CONTACT_FOUND               = OPC_FIRST_CONTACT | OPC_CONTACT,
41                 OPC_TEMPORAL_CONTACT    = OPC_TEMPORAL_HIT | OPC_CONTACT,
42
43                 OPC_FORCE_DWORD                 = 0x7fffffff
44         };
45
46         class OPCODE_API Collider
47         {
48                 public:
49                 // Constructor / Destructor
50                                                                                         Collider();
51                 virtual                                                         ~Collider();
52
53                 // Collision report
54
55                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
56                 /**
57                  *      Gets the last collision status after a collision query.
58                  *      \return         true if a collision occured
59                  */
60                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
61                 inline_                         BOOL                    GetContactStatus()                      const   { return mFlags & OPC_CONTACT;                                                  }
62
63                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
64                 /**
65                  *      Gets the "first contact" mode.
66                  *      \return         true if "first contact" mode is on
67                  */
68                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
69                 inline_                         BOOL                    FirstContactEnabled()           const   { return mFlags & OPC_FIRST_CONTACT;                                    }
70
71                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
72                 /**
73                  *      Gets the temporal coherence mode.
74                  *      \return         true if temporal coherence is on
75                  */
76                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
77                 inline_                         BOOL                    TemporalCoherenceEnabled()      const   { return mFlags & OPC_TEMPORAL_COHERENCE;                               }
78
79                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
80                 /**
81                  *      Checks a first contact has already been found.
82                  *      \return         true if a first contact has been found and we can stop a query
83                  */
84                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
85                 inline_                         BOOL                    ContactFound()                          const   { return (mFlags&OPC_CONTACT_FOUND)==OPC_CONTACT_FOUND; }
86
87                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
88                 /**
89                  *      Checks there's been an early exit due to temporal coherence;
90                  *      \return         true if a temporal hit has occured
91                  */
92                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
93                 inline_                         BOOL                    TemporalHit()                           const   { return mFlags & OPC_TEMPORAL_HIT;                                             }
94
95                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
96                 /**
97                  *      Checks primitive tests are enabled;
98                  *      \return         true if primitive tests must be skipped
99                  */
100                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
101                 inline_                         BOOL                    SkipPrimitiveTests()            const   { return mFlags & OPC_NO_PRIMITIVE_TESTS;                               }
102
103                 // Settings
104
105                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
106                 /**
107                  *      Reports all contacts (false) or first contact only (true)
108                  *      \param          flag            [in] true for first contact, false for all contacts
109                  *      \see            SetTemporalCoherence(bool flag)
110                  *      \see            ValidateSettings()
111                  */
112                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
113                 inline_                         void                    SetFirstContact(bool flag)
114                                                                                         {
115                                                                                                 if(flag)        mFlags |= OPC_FIRST_CONTACT;
116                                                                                                 else            mFlags &= ~OPC_FIRST_CONTACT;
117                                                                                         }
118
119                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
120                 /**
121                  *      Enable/disable temporal coherence.
122                  *      \param          flag            [in] true to enable temporal coherence, false to discard it
123                  *      \see            SetFirstContact(bool flag)
124                  *      \see            ValidateSettings()
125                  */
126                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
127                 inline_                         void                    SetTemporalCoherence(bool flag)
128                                                                                         {
129                                                                                                 if(flag)        mFlags |= OPC_TEMPORAL_COHERENCE;
130                                                                                                 else            mFlags &= ~OPC_TEMPORAL_COHERENCE;
131                                                                                         }
132
133                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
134                 /**
135                  *      Enable/disable primitive tests.
136                  *      \param          flag            [in] true to enable primitive tests, false to discard them
137                  */
138                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
139                 inline_                         void                    SetPrimitiveTests(bool flag)
140                                                                                         {
141                                                                                                 if(!flag)       mFlags |= OPC_NO_PRIMITIVE_TESTS;
142                                                                                                 else            mFlags &= ~OPC_NO_PRIMITIVE_TESTS;
143                                                                                         }
144
145                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
146                 /**
147                  *      Validates current settings. You should call this method after all the settings / callbacks have been defined for a collider.
148                  *      \return         null if everything is ok, else a string describing the problem
149                  */
150                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
151                 virtual                         const char*             ValidateSettings()      = 0;
152
153                 protected:
154                                                         udword                  mFlags;                 //!< Bit flags
155                                         const   BaseModel*              mCurrentModel;  //!< Current model for collision query (owner of touched faces)
156                 // User mesh interface
157                                         const   MeshInterface*  mIMesh;                 //!< User-defined mesh interface
158
159                 // Internal methods
160                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
161                 /**
162                  *      Setups current collision model
163                  *      \param          model   [in] current collision model
164                  *      \return         TRUE if success
165                  */
166                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
167                 inline_                         BOOL                    Setup(const BaseModel* model)
168                                                                                         {
169                                                                                                 // Keep track of current model
170                                                                                                 mCurrentModel = model;
171                                                                                                 if(!mCurrentModel)      return FALSE;
172
173                                                                                                 mIMesh = model->GetMeshInterface();
174                                                                                                 return mIMesh!=null;
175                                                                                         }
176
177                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
178                 /**
179                  *      Initializes a query
180                  */
181                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
182                 virtual inline_         void                    InitQuery()             { mFlags &= ~OPC_TEMPORAL_CONTACT;      }
183         };
184
185 #endif // __OPC_COLLIDER_H__