OPC_PlanesCollider.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 a planes collider.
  11. * \file OPC_PlanesCollider.h
  12. * \author Pierre Terdiman
  13. * \date January, 1st, 2002
  14. */
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. // Include Guard
  18. #ifndef __OPC_PLANESCOLLIDER_H__
  19. #define __OPC_PLANESCOLLIDER_H__
  20. struct OPCODE_API PlanesCache : VolumeCache
  21. {
  22. PlanesCache()
  23. {
  24. }
  25. };
  26. class OPCODE_API PlanesCollider : public VolumeCollider
  27. {
  28. public:
  29. // Constructor / Destructor
  30. PlanesCollider();
  31. virtual ~PlanesCollider();
  32. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  33. /**
  34. * Generic collision query for generic OPCODE models. After the call, access the results:
  35. * - with GetContactStatus()
  36. * - with GetNbTouchedPrimitives()
  37. * - with GetTouchedPrimitives()
  38. *
  39. * \param cache [in/out] a planes cache
  40. * \param planes [in] list of planes in world space
  41. * \param nb_planes [in] number of planes
  42. * \param model [in] Opcode model to collide with
  43. * \param worldm [in] model's world matrix, or null
  44. * \return true if success
  45. * \warning SCALE NOT SUPPORTED. The matrices must contain rotation & translation parts only.
  46. */
  47. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  48. bool Collide(PlanesCache& cache, const Plane* planes, udword nb_planes, const Model& model, const Matrix4x4* worldm=null);
  49. // Mutant box-with-planes collision queries
  50. inline_ bool Collide(PlanesCache& cache, const OBB& box, const Model& model, const Matrix4x4* worldb=null, const Matrix4x4* worldm=null)
  51. {
  52. Plane PL[6];
  53. if(worldb)
  54. {
  55. // Create a new OBB in world space
  56. OBB WorldBox;
  57. box.Rotate(*worldb, WorldBox);
  58. // Compute planes from the sides of the box
  59. WorldBox.ComputePlanes(PL);
  60. }
  61. else
  62. {
  63. // Compute planes from the sides of the box
  64. box.ComputePlanes(PL);
  65. }
  66. // Collide with box planes
  67. return Collide(cache, PL, 6, model, worldm);
  68. }
  69. // Settings
  70. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  71. /**
  72. * Validates current settings. You should call this method after all the settings and callbacks have been defined for a collider.
  73. * \return null if everything is ok, else a string describing the problem
  74. */
  75. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  76. override(Collider) const char* ValidateSettings();
  77. protected:
  78. // Planes in model space
  79. udword mNbPlanes;
  80. Plane* mPlanes;
  81. // Leaf description
  82. VertexPointers mVP;
  83. // Internal methods
  84. void _Collide(const AABBCollisionNode* node, udword clip_mask);
  85. void _Collide(const AABBNoLeafNode* node, udword clip_mask);
  86. void _Collide(const AABBQuantizedNode* node, udword clip_mask);
  87. void _Collide(const AABBQuantizedNoLeafNode* node, udword clip_mask);
  88. void _CollideNoPrimitiveTest(const AABBCollisionNode* node, udword clip_mask);
  89. void _CollideNoPrimitiveTest(const AABBNoLeafNode* node, udword clip_mask);
  90. void _CollideNoPrimitiveTest(const AABBQuantizedNode* node, udword clip_mask);
  91. void _CollideNoPrimitiveTest(const AABBQuantizedNoLeafNode* node, udword clip_mask);
  92. // Overlap tests
  93. inline_ BOOL PlanesAABBOverlap(const Point& center, const Point& extents, udword& out_clip_mask, udword in_clip_mask);
  94. inline_ BOOL PlanesTriOverlap(udword in_clip_mask);
  95. // Init methods
  96. BOOL InitQuery(PlanesCache& cache, const Plane* planes, udword nb_planes, const Matrix4x4* worldm=null);
  97. };
  98. class OPCODE_API HybridPlanesCollider : public PlanesCollider
  99. {
  100. public:
  101. // Constructor / Destructor
  102. HybridPlanesCollider();
  103. virtual ~HybridPlanesCollider();
  104. bool Collide(PlanesCache& cache, const Plane* planes, udword nb_planes, const HybridModel& model, const Matrix4x4* worldm=null);
  105. protected:
  106. OPC_Container mTouchedBoxes;
  107. };
  108. #endif // __OPC_PLANESCOLLIDER_H__