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 base collider class.
21 * \file OPC_Collider.h
22 * \author Pierre Terdiman
25 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
27 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
29 #ifndef __OPC_COLLIDER_H__
30 #define __OPC_COLLIDER_H__
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)
40 OPC_CONTACT_FOUND = OPC_FIRST_CONTACT | OPC_CONTACT,
41 OPC_TEMPORAL_CONTACT = OPC_TEMPORAL_HIT | OPC_CONTACT,
43 OPC_FORCE_DWORD = 0x7fffffff
46 class OPCODE_API Collider
49 // Constructor / Destructor
55 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
57 * Gets the last collision status after a collision query.
58 * \return true if a collision occured
60 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
61 inline_ BOOL GetContactStatus() const { return mFlags & OPC_CONTACT; }
63 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
65 * Gets the "first contact" mode.
66 * \return true if "first contact" mode is on
68 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
69 inline_ BOOL FirstContactEnabled() const { return mFlags & OPC_FIRST_CONTACT; }
71 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
73 * Gets the temporal coherence mode.
74 * \return true if temporal coherence is on
76 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
77 inline_ BOOL TemporalCoherenceEnabled() const { return mFlags & OPC_TEMPORAL_COHERENCE; }
79 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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
84 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
85 inline_ BOOL ContactFound() const { return (mFlags&OPC_CONTACT_FOUND)==OPC_CONTACT_FOUND; }
87 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
89 * Checks there's been an early exit due to temporal coherence;
90 * \return true if a temporal hit has occured
92 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
93 inline_ BOOL TemporalHit() const { return mFlags & OPC_TEMPORAL_HIT; }
95 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
97 * Checks primitive tests are enabled;
98 * \return true if primitive tests must be skipped
100 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
101 inline_ BOOL SkipPrimitiveTests() const { return mFlags & OPC_NO_PRIMITIVE_TESTS; }
105 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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()
112 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
113 inline_ void SetFirstContact(bool flag)
115 if(flag) mFlags |= OPC_FIRST_CONTACT;
116 else mFlags &= ~OPC_FIRST_CONTACT;
119 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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()
126 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
127 inline_ void SetTemporalCoherence(bool flag)
129 if(flag) mFlags |= OPC_TEMPORAL_COHERENCE;
130 else mFlags &= ~OPC_TEMPORAL_COHERENCE;
133 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
135 * Enable/disable primitive tests.
136 * \param flag [in] true to enable primitive tests, false to discard them
138 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
139 inline_ void SetPrimitiveTests(bool flag)
141 if(!flag) mFlags |= OPC_NO_PRIMITIVE_TESTS;
142 else mFlags &= ~OPC_NO_PRIMITIVE_TESTS;
145 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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
150 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
151 virtual const char* ValidateSettings() = 0;
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
160 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
162 * Setups current collision model
163 * \param model [in] current collision model
164 * \return TRUE if success
166 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
167 inline_ BOOL Setup(const BaseModel* model)
169 // Keep track of current model
170 mCurrentModel = model;
171 if(!mCurrentModel) return FALSE;
173 mIMesh = model->GetMeshInterface();
177 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
179 * Initializes a query
181 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
182 virtual inline_ void InitQuery() { mFlags &= ~OPC_TEMPORAL_CONTACT; }
185 #endif // __OPC_COLLIDER_H__