OPC_SphereCollider.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 sphere collider.
  11. * \file OPC_SphereCollider.h
  12. * \author Pierre Terdiman
  13. * \date June, 2, 2001
  14. */
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. // Include Guard
  18. #ifndef __OPC_SPHERECOLLIDER_H__
  19. #define __OPC_SPHERECOLLIDER_H__
  20. struct OPCODE_API SphereCache : VolumeCache
  21. {
  22. SphereCache() : Center(0.0f,0.0f,0.0f), FatRadius2(0.0f), FatCoeff(1.1f) {}
  23. ~SphereCache() {}
  24. // Cached faces signature
  25. Point Center; //!< Sphere used when performing the query resulting in cached faces
  26. float FatRadius2; //!< Sphere used when performing the query resulting in cached faces
  27. // User settings
  28. float FatCoeff; //!< mRadius2 multiplier used to create a fat sphere
  29. };
  30. class OPCODE_API SphereCollider : public VolumeCollider
  31. {
  32. public:
  33. // Constructor / Destructor
  34. SphereCollider();
  35. virtual ~SphereCollider();
  36. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  37. /**
  38. * Generic collision query for generic OPCODE models. After the call, access the results:
  39. * - with GetContactStatus()
  40. * - with GetNbTouchedPrimitives()
  41. * - with GetTouchedPrimitives()
  42. *
  43. * \param cache [in/out] a sphere cache
  44. * \param sphere [in] collision sphere in local space
  45. * \param model [in] Opcode model to collide with
  46. * \param worlds [in] sphere's world matrix, or null
  47. * \param worldm [in] model's world matrix, or null
  48. * \return true if success
  49. * \warning SCALE NOT SUPPORTED. The matrices must contain rotation & translation parts only.
  50. */
  51. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  52. bool Collide(SphereCache& cache, const Sphere& sphere, const Model& model, const Matrix4x4* worlds=null, const Matrix4x4* worldm=null);
  53. //
  54. bool Collide(SphereCache& cache, const Sphere& sphere, const AABBTree* tree);
  55. protected:
  56. // Sphere in model space
  57. Point mCenter; //!< Sphere center
  58. float mRadius2; //!< Sphere radius squared
  59. // Internal methods
  60. void _Collide(const AABBCollisionNode* node);
  61. void _Collide(const AABBNoLeafNode* node);
  62. void _Collide(const AABBQuantizedNode* node);
  63. void _Collide(const AABBQuantizedNoLeafNode* node);
  64. void _Collide(const AABBTreeNode* node);
  65. void _CollideNoPrimitiveTest(const AABBCollisionNode* node);
  66. void _CollideNoPrimitiveTest(const AABBNoLeafNode* node);
  67. void _CollideNoPrimitiveTest(const AABBQuantizedNode* node);
  68. void _CollideNoPrimitiveTest(const AABBQuantizedNoLeafNode* node);
  69. // Overlap tests
  70. inline_ BOOL SphereContainsBox(const Point& bc, const Point& be);
  71. inline_ BOOL SphereAABBOverlap(const Point& center, const Point& extents);
  72. BOOL SphereTriOverlap(const Point& vert0, const Point& vert1, const Point& vert2);
  73. // Init methods
  74. BOOL InitQuery(SphereCache& cache, const Sphere& sphere, const Matrix4x4* worlds=null, const Matrix4x4* worldm=null);
  75. };
  76. class OPCODE_API HybridSphereCollider : public SphereCollider
  77. {
  78. public:
  79. // Constructor / Destructor
  80. HybridSphereCollider();
  81. virtual ~HybridSphereCollider();
  82. bool Collide(SphereCache& cache, const Sphere& sphere, const HybridModel& model, const Matrix4x4* worlds=null, const Matrix4x4* worldm=null);
  83. protected:
  84. OPC_Container mTouchedBoxes;
  85. };
  86. #endif // __OPC_SPHERECOLLIDER_H__