OPC_OBBCollider.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 code for an OBB collider.
  11. * \file OPC_OBBCollider.h
  12. * \author Pierre Terdiman
  13. * \date January, 1st, 2002
  14. */
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. // Include Guard
  18. #ifndef __OPC_OBBCOLLIDER_H__
  19. #define __OPC_OBBCOLLIDER_H__
  20. struct OPCODE_API OBBCache : VolumeCache
  21. {
  22. OBBCache() : FatCoeff(1.1f)
  23. {
  24. FatBox.mCenter.Zero();
  25. FatBox.mExtents.Zero();
  26. FatBox.mRot.Identity();
  27. }
  28. // Cached faces signature
  29. OBB FatBox; //!< Box used when performing the query resulting in cached faces
  30. // User settings
  31. float FatCoeff; //!< extents multiplier used to create a fat box
  32. };
  33. class OPCODE_API OBBCollider : public VolumeCollider
  34. {
  35. public:
  36. // Constructor / Destructor
  37. OBBCollider();
  38. virtual ~OBBCollider();
  39. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  40. /**
  41. * Generic collision query for generic OPCODE models. After the call, access the results:
  42. * - with GetContactStatus()
  43. * - with GetNbTouchedPrimitives()
  44. * - with GetTouchedPrimitives()
  45. *
  46. * \param cache [in/out] a box cache
  47. * \param box [in] collision OBB in local space
  48. * \param model [in] Opcode model to collide with
  49. * \param worldb [in] OBB's world matrix, or null
  50. * \param worldm [in] model's world matrix, or null
  51. * \return true if success
  52. * \warning SCALE NOT SUPPORTED. The matrices must contain rotation & translation parts only.
  53. */
  54. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  55. bool Collide(OBBCache& cache, const OBB& box, const Model& model, const Matrix4x4* worldb=null, const Matrix4x4* worldm=null);
  56. // Settings
  57. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  58. /**
  59. * Settings: select between full box-box tests or "SAT-lite" tests (where Class III axes are discarded)
  60. * \param flag [in] true for full tests, false for coarse tests
  61. */
  62. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  63. inline_ void SetFullBoxBoxTest(bool flag) { mFullBoxBoxTest = flag; }
  64. // Settings
  65. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66. /**
  67. * Validates current settings. You should call this method after all the settings and callbacks have been defined for a collider.
  68. * \return null if everything is ok, else a string describing the problem
  69. */
  70. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  71. override(Collider) const char* ValidateSettings();
  72. protected:
  73. // Precomputed data
  74. Matrix3x3 mAR; //!< Absolute rotation matrix
  75. Matrix3x3 mRModelToBox; //!< Rotation from model space to obb space
  76. Matrix3x3 mRBoxToModel; //!< Rotation from obb space to model space
  77. Point mTModelToBox; //!< Translation from model space to obb space
  78. Point mTBoxToModel; //!< Translation from obb space to model space
  79. Point mBoxExtents;
  80. Point mB0; //!< - mTModelToBox + mBoxExtents
  81. Point mB1; //!< - mTModelToBox - mBoxExtents
  82. float mBBx1;
  83. float mBBy1;
  84. float mBBz1;
  85. float mBB_1;
  86. float mBB_2;
  87. float mBB_3;
  88. float mBB_4;
  89. float mBB_5;
  90. float mBB_6;
  91. float mBB_7;
  92. float mBB_8;
  93. float mBB_9;
  94. // Leaf description
  95. Point mLeafVerts[3]; //!< Triangle vertices
  96. // Settings
  97. bool mFullBoxBoxTest; //!< Perform full BV-BV tests (true) or SAT-lite tests (false)
  98. // Internal methods
  99. void _Collide(const AABBCollisionNode* node);
  100. void _Collide(const AABBNoLeafNode* node);
  101. void _Collide(const AABBQuantizedNode* node);
  102. void _Collide(const AABBQuantizedNoLeafNode* node);
  103. void _CollideNoPrimitiveTest(const AABBCollisionNode* node);
  104. void _CollideNoPrimitiveTest(const AABBNoLeafNode* node);
  105. void _CollideNoPrimitiveTest(const AABBQuantizedNode* node);
  106. void _CollideNoPrimitiveTest(const AABBQuantizedNoLeafNode* node);
  107. // Overlap tests
  108. inline_ BOOL OBBContainsBox(const Point& bc, const Point& be);
  109. inline_ BOOL BoxBoxOverlap(const Point& extents, const Point& center);
  110. inline_ BOOL TriBoxOverlap();
  111. // Init methods
  112. BOOL InitQuery(OBBCache& cache, const OBB& box, const Matrix4x4* worldb=null, const Matrix4x4* worldm=null);
  113. };
  114. class OPCODE_API HybridOBBCollider : public OBBCollider
  115. {
  116. public:
  117. // Constructor / Destructor
  118. HybridOBBCollider();
  119. virtual ~HybridOBBCollider();
  120. bool Collide(OBBCache& cache, const OBB& box, const HybridModel& model, const Matrix4x4* worldb=null, const Matrix4x4* worldm=null);
  121. protected:
  122. OPC_Container mTouchedBoxes;
  123. };
  124. #endif // __OPC_OBBCOLLIDER_H__