1
0

CCPhysics3DShape.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /****************************************************************************
  2. Copyright (c) 2015-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 __PHYSICS_3D_SHAPE_H__
  21. #define __PHYSICS_3D_SHAPE_H__
  22. #include "base/CCRef.h"
  23. #include "base/ccConfig.h"
  24. #include "math/CCMath.h"
  25. #if CC_USE_3D_PHYSICS
  26. #if (CC_ENABLE_BULLET_INTEGRATION)
  27. class btCollisionShape;
  28. NS_CC_BEGIN
  29. /**
  30. * @addtogroup _3d
  31. * @{
  32. */
  33. /**
  34. * @brief Create a physical shape(box, sphere, cylinder, capsule, convexhull, mesh and heightfield)
  35. */
  36. class CC_DLL Physics3DShape : public Ref
  37. {
  38. public:
  39. enum class ShapeType
  40. {
  41. UNKNOWN = 0,
  42. BOX,
  43. SPHERE,
  44. CYLINDER,
  45. CAPSULE,
  46. CONVEX,
  47. MESH,
  48. HEIGHT_FIELD,
  49. COMPOUND
  50. };
  51. /**
  52. * get shape type
  53. */
  54. virtual ShapeType getShapeType() const;
  55. /**
  56. * create box shape
  57. * @param extent The extent of sphere.
  58. */
  59. static Physics3DShape* createBox(const cocos2d::Vec3& extent);
  60. /**
  61. * create sphere shape
  62. * @param radius The radius of sphere.
  63. */
  64. static Physics3DShape* createSphere(float radius);
  65. /**
  66. * create cylinder shape
  67. * @param radius The radius of cylinder.
  68. * @param height The height.
  69. */
  70. static Physics3DShape* createCylinder(float radius, float height);
  71. /**
  72. * create capsule shape
  73. * @param radius The radius of capsule.
  74. * @param height The height (cylinder part).
  75. */
  76. static Physics3DShape* createCapsule(float radius, float height);
  77. /**
  78. * create convex hull
  79. * @param points The vertices of convex hull
  80. * @param numPoints The number of vertices.
  81. */
  82. static Physics3DShape* createConvexHull(const cocos2d::Vec3 *points, int numPoints);
  83. /**
  84. * create mesh
  85. * @param triangles The pointer of triangle list
  86. * @param numTriangles The number of triangles.
  87. */
  88. static Physics3DShape* createMesh(const cocos2d::Vec3 *triangles, int numTriangles);
  89. /**
  90. * create heightfield
  91. * @param heightStickWidth The Width of heightfield
  92. * @param heightStickLength The Length of heightfield.
  93. * @param heightfieldData The Data of heightfield.
  94. * @param minHeight The minHeight of heightfield.
  95. * @param maxHeight The maxHeight of heightfield.
  96. * @param flipQuadEdges if flip QuadEdges
  97. */
  98. static Physics3DShape* createHeightfield(int heightStickWidth,int heightStickLength
  99. , const void* heightfieldData, float heightScale
  100. , float minHeight, float maxHeight
  101. , bool useFloatDatam, bool flipQuadEdges, bool useDiamondSubdivision = false);
  102. /**
  103. * create Compound Shape
  104. * @param shapes The list of child shape
  105. */
  106. static Physics3DShape* createCompoundShape(const std::vector<std::pair<Physics3DShape *, Mat4>> &shapes);
  107. #if CC_ENABLE_BULLET_INTEGRATION
  108. btCollisionShape* getbtShape() const { return _btShape; }
  109. #endif
  110. CC_CONSTRUCTOR_ACCESS:
  111. Physics3DShape();
  112. ~Physics3DShape();
  113. bool initBox(const cocos2d::Vec3& ext);
  114. bool initSphere(float radius);
  115. bool initCylinder(float radius, float height);
  116. bool initCapsule(float radius, float height);
  117. bool initConvexHull(const cocos2d::Vec3 *points, int numPoints);
  118. bool initMesh(const cocos2d::Vec3 *triangles, int numTriangles);
  119. bool initHeightfield(int heightStickWidth,int heightStickLength
  120. , const void* heightfieldData, float heightScale
  121. , float minHeight, float maxHeight
  122. , bool useFloatDatam, bool flipQuadEdges
  123. , bool useDiamondSubdivision);
  124. bool initCompoundShape(const std::vector<std::pair<Physics3DShape *, Mat4>> &shapes);
  125. protected:
  126. ShapeType _shapeType; //shape type
  127. #if (CC_ENABLE_BULLET_INTEGRATION)
  128. btCollisionShape* _btShape;
  129. unsigned char *_heightfieldData;
  130. std::vector<Physics3DShape *> _compoundChildShapes;
  131. #endif
  132. };
  133. // end of 3d group
  134. /// @}
  135. NS_CC_END
  136. #endif // CC_ENABLE_BULLET_INTEGRATION
  137. #endif //CC_USE_3D_PHYSICS
  138. #endif // __PHYSICS_3D_SHAPE_H__