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