shape_bullet.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**************************************************************************/
  2. /* shape_bullet.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef SHAPE_BULLET_H
  31. #define SHAPE_BULLET_H
  32. #include "core/math/geometry.h"
  33. #include "core/variant.h"
  34. #include "rid_bullet.h"
  35. #include "servers/physics_server.h"
  36. #if defined(__clang__) && (__clang_major__ >= 13)
  37. #pragma clang diagnostic push
  38. #pragma clang diagnostic ignored "-Wdeprecated-copy-with-user-provided-copy"
  39. #endif
  40. #include <LinearMath/btAlignedObjectArray.h>
  41. #include <LinearMath/btScalar.h>
  42. #include <LinearMath/btVector3.h>
  43. #if defined(__clang__) && (__clang_major__ >= 13)
  44. #pragma clang diagnostic pop
  45. #endif
  46. /**
  47. @author AndreaCatania
  48. */
  49. class ShapeBullet;
  50. class btCollisionShape;
  51. class ShapeOwnerBullet;
  52. class btBvhTriangleMeshShape;
  53. class ShapeBullet : public RIDBullet {
  54. Map<ShapeOwnerBullet *, int> owners;
  55. real_t margin;
  56. protected:
  57. /// return self
  58. btCollisionShape *prepare(btCollisionShape *p_btShape) const;
  59. void notifyShapeChanged();
  60. public:
  61. ShapeBullet();
  62. virtual ~ShapeBullet();
  63. btCollisionShape *create_bt_shape(const Vector3 &p_implicit_scale, real_t p_extra_edge = 0);
  64. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0) = 0;
  65. void add_owner(ShapeOwnerBullet *p_owner);
  66. void remove_owner(ShapeOwnerBullet *p_owner, bool p_permanentlyFromThisBody = false);
  67. bool is_owner(ShapeOwnerBullet *p_owner) const;
  68. const Map<ShapeOwnerBullet *, int> &get_owners() const;
  69. void set_margin(real_t p_margin);
  70. real_t get_margin() const;
  71. /// Setup the shape
  72. virtual void set_data(const Variant &p_data) = 0;
  73. virtual Variant get_data() const = 0;
  74. virtual PhysicsServer::ShapeType get_type() const = 0;
  75. public:
  76. static class btEmptyShape *create_shape_empty();
  77. static class btStaticPlaneShape *create_shape_plane(const btVector3 &planeNormal, btScalar planeConstant);
  78. static class btSphereShape *create_shape_sphere(btScalar radius);
  79. static class btBoxShape *create_shape_box(const btVector3 &boxHalfExtents);
  80. static class btCapsuleShapeZ *create_shape_capsule(btScalar radius, btScalar height);
  81. static class btCylinderShape *create_shape_cylinder(btScalar radius, btScalar height);
  82. /// IMPORTANT: Remember to delete the shape interface by calling: delete my_shape->getMeshInterface();
  83. static class btConvexPointCloudShape *create_shape_convex(btAlignedObjectArray<btVector3> &p_vertices, const btVector3 &p_local_scaling = btVector3(1, 1, 1));
  84. static class btScaledBvhTriangleMeshShape *create_shape_concave(btBvhTriangleMeshShape *p_mesh_shape, const btVector3 &p_local_scaling = btVector3(1, 1, 1));
  85. static class btHeightfieldTerrainShape *create_shape_height_field(PoolVector<real_t> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height);
  86. static class btRayShape *create_shape_ray(real_t p_length, bool p_slips_on_slope);
  87. };
  88. class PlaneShapeBullet : public ShapeBullet {
  89. Plane plane;
  90. public:
  91. PlaneShapeBullet();
  92. virtual void set_data(const Variant &p_data);
  93. virtual Variant get_data() const;
  94. virtual PhysicsServer::ShapeType get_type() const;
  95. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);
  96. private:
  97. void setup(const Plane &p_plane);
  98. };
  99. class SphereShapeBullet : public ShapeBullet {
  100. real_t radius;
  101. public:
  102. SphereShapeBullet();
  103. _FORCE_INLINE_ real_t get_radius() { return radius; }
  104. virtual void set_data(const Variant &p_data);
  105. virtual Variant get_data() const;
  106. virtual PhysicsServer::ShapeType get_type() const;
  107. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);
  108. private:
  109. void setup(real_t p_radius);
  110. };
  111. class BoxShapeBullet : public ShapeBullet {
  112. btVector3 half_extents;
  113. public:
  114. BoxShapeBullet();
  115. _FORCE_INLINE_ const btVector3 &get_half_extents() { return half_extents; }
  116. virtual void set_data(const Variant &p_data);
  117. virtual Variant get_data() const;
  118. virtual PhysicsServer::ShapeType get_type() const;
  119. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);
  120. private:
  121. void setup(const Vector3 &p_half_extents);
  122. };
  123. class CapsuleShapeBullet : public ShapeBullet {
  124. real_t height;
  125. real_t radius;
  126. public:
  127. CapsuleShapeBullet();
  128. _FORCE_INLINE_ real_t get_height() { return height; }
  129. _FORCE_INLINE_ real_t get_radius() { return radius; }
  130. virtual void set_data(const Variant &p_data);
  131. virtual Variant get_data() const;
  132. virtual PhysicsServer::ShapeType get_type() const;
  133. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);
  134. private:
  135. void setup(real_t p_height, real_t p_radius);
  136. };
  137. class CylinderShapeBullet : public ShapeBullet {
  138. real_t height;
  139. real_t radius;
  140. public:
  141. CylinderShapeBullet();
  142. _FORCE_INLINE_ real_t get_height() { return height; }
  143. _FORCE_INLINE_ real_t get_radius() { return radius; }
  144. virtual void set_data(const Variant &p_data);
  145. virtual Variant get_data() const;
  146. virtual PhysicsServer::ShapeType get_type() const;
  147. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_margin = 0);
  148. private:
  149. void setup(real_t p_height, real_t p_radius);
  150. };
  151. class ConvexPolygonShapeBullet : public ShapeBullet {
  152. public:
  153. btAlignedObjectArray<btVector3> vertices;
  154. ConvexPolygonShapeBullet();
  155. virtual void set_data(const Variant &p_data);
  156. void get_vertices(Vector<Vector3> &out_vertices);
  157. virtual Variant get_data() const;
  158. virtual PhysicsServer::ShapeType get_type() const;
  159. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);
  160. private:
  161. void setup(const Vector<Vector3> &p_vertices);
  162. };
  163. class ConcavePolygonShapeBullet : public ShapeBullet {
  164. class btBvhTriangleMeshShape *meshShape;
  165. public:
  166. PoolVector<Vector3> faces;
  167. ConcavePolygonShapeBullet();
  168. virtual ~ConcavePolygonShapeBullet();
  169. virtual void set_data(const Variant &p_data);
  170. virtual Variant get_data() const;
  171. virtual PhysicsServer::ShapeType get_type() const;
  172. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);
  173. private:
  174. void setup(PoolVector<Vector3> p_faces);
  175. };
  176. class HeightMapShapeBullet : public ShapeBullet {
  177. public:
  178. PoolVector<real_t> heights;
  179. int width;
  180. int depth;
  181. real_t min_height;
  182. real_t max_height;
  183. HeightMapShapeBullet();
  184. virtual void set_data(const Variant &p_data);
  185. virtual Variant get_data() const;
  186. virtual PhysicsServer::ShapeType get_type() const;
  187. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);
  188. private:
  189. void setup(PoolVector<real_t> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height);
  190. };
  191. class RayShapeBullet : public ShapeBullet {
  192. public:
  193. real_t length;
  194. bool slips_on_slope;
  195. RayShapeBullet();
  196. virtual void set_data(const Variant &p_data);
  197. virtual Variant get_data() const;
  198. virtual PhysicsServer::ShapeType get_type() const;
  199. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);
  200. private:
  201. void setup(real_t p_length, bool p_slips_on_slope);
  202. };
  203. #endif // SHAPE_BULLET_H