OPC_Collider.h 10 KB

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