1
0

CCOBB.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /****************************************************************************
  2. Copyright (c) 2014-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #ifndef __CC_OBB_H__
  21. #define __CC_OBB_H__
  22. #include "3d/CCAABB.h"
  23. NS_CC_BEGIN
  24. /**
  25. * @addtogroup _3d
  26. * @{
  27. */
  28. /**
  29. * Oriented Bounding Box(OBB)
  30. * @brief the OBB is similar to the AABB but the bounding box has the same direction as Sprite3D. so it's collision detection more precise than AABB
  31. * @js NA
  32. */
  33. class CC_DLL OBB
  34. {
  35. public:
  36. OBB();
  37. /*
  38. * Construct obb from oriented bounding box
  39. *
  40. * @lua NA
  41. */
  42. OBB(const AABB& aabb);
  43. /*
  44. * Construct obb from points
  45. *
  46. * @lua NA
  47. */
  48. OBB(const Vec3* verts, int num);
  49. /*
  50. * Check point in
  51. */
  52. bool containPoint(const Vec3& point) const;
  53. /*
  54. * Specify obb values
  55. */
  56. void set(const Vec3& center, const Vec3& _xAxis, const Vec3& _yAxis, const Vec3& _zAxis, const Vec3& _extents);
  57. /*
  58. * Clear obb
  59. */
  60. void reset();
  61. /* face to the obb's -z direction
  62. * verts[0] : left top front
  63. * verts[1] : left bottom front
  64. * verts[2] : right bottom front
  65. * verts[3] : right top front
  66. *
  67. * face to the obb's z direction
  68. * verts[4] : right top back
  69. * verts[5] : right bottom back
  70. * verts[6] : left bottom back
  71. * verts[7] : left top back
  72. */
  73. void getCorners(Vec3* verts) const;
  74. /*
  75. * Check intersect with other
  76. */
  77. bool intersects(const OBB& box) const;
  78. /**
  79. * Transforms the obb by the given transformation matrix.
  80. */
  81. void transform(const Mat4& mat);
  82. protected:
  83. /*
  84. * compute extX, extY, extZ
  85. */
  86. void computeExtAxis()
  87. {
  88. _extentX = _xAxis * _extents.x;
  89. _extentY = _yAxis * _extents.y;
  90. _extentZ = _zAxis * _extents.z;
  91. }
  92. /*
  93. * Project point to the target axis
  94. */
  95. float projectPoint(const Vec3& point, const Vec3& axis) const;
  96. /*
  97. * Calculate the min and max project value of through the box's corners
  98. */
  99. void getInterval(const OBB& box, const Vec3& axis, float &min, float &max) const;
  100. /*
  101. * Get the edge of x y z axis direction
  102. */
  103. Vec3 getEdgeDirection(int index) const;
  104. /*
  105. * Get the face of x y z axis direction
  106. */
  107. Vec3 getFaceDirection(int index) const;
  108. public:
  109. Vec3 _center; // obb center
  110. Vec3 _xAxis; // x axis of obb, unit vector
  111. Vec3 _yAxis; // y axis of obb, unit vector
  112. Vec3 _zAxis; // z axis of obb, unit vector
  113. Vec3 _extentX; // _xAxis * _extents.x
  114. Vec3 _extentY; // _yAxis * _extents.y
  115. Vec3 _extentZ; // _zAxis * _extents.z
  116. Vec3 _extents; // obb length along each axis
  117. };
  118. // end of 3d group
  119. /// @}
  120. NS_CC_END
  121. #endif