shape_bullet.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*************************************************************************/
  2. /* shape_bullet.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. #include <LinearMath/btAlignedObjectArray.h>
  37. #include <LinearMath/btScalar.h>
  38. #include <LinearMath/btVector3.h>
  39. /**
  40. @author AndreaCatania
  41. */
  42. class ShapeBullet;
  43. class btCollisionShape;
  44. class ShapeOwnerBullet;
  45. class btBvhTriangleMeshShape;
  46. class ShapeBullet : public RIDBullet {
  47. Map<ShapeOwnerBullet *, int> owners;
  48. real_t margin;
  49. protected:
  50. /// return self
  51. btCollisionShape *prepare(btCollisionShape *p_btShape) const;
  52. void notifyShapeChanged();
  53. public:
  54. ShapeBullet();
  55. virtual ~ShapeBullet();
  56. btCollisionShape *create_bt_shape(const Vector3 &p_implicit_scale, real_t p_extra_edge = 0);
  57. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0) = 0;
  58. void add_owner(ShapeOwnerBullet *p_owner);
  59. void remove_owner(ShapeOwnerBullet *p_owner, bool p_permanentlyFromThisBody = false);
  60. bool is_owner(ShapeOwnerBullet *p_owner) const;
  61. const Map<ShapeOwnerBullet *, int> &get_owners() const;
  62. void set_margin(real_t p_margin);
  63. real_t get_margin() const;
  64. /// Setup the shape
  65. virtual void set_data(const Variant &p_data) = 0;
  66. virtual Variant get_data() const = 0;
  67. virtual PhysicsServer::ShapeType get_type() const = 0;
  68. public:
  69. static class btEmptyShape *create_shape_empty();
  70. static class btStaticPlaneShape *create_shape_plane(const btVector3 &planeNormal, btScalar planeConstant);
  71. static class btSphereShape *create_shape_sphere(btScalar radius);
  72. static class btBoxShape *create_shape_box(const btVector3 &boxHalfExtents);
  73. static class btCapsuleShapeZ *create_shape_capsule(btScalar radius, btScalar height);
  74. static class btCylinderShape *create_shape_cylinder(btScalar radius, btScalar height);
  75. /// IMPORTANT: Remember to delete the shape interface by calling: delete my_shape->getMeshInterface();
  76. static class btConvexPointCloudShape *create_shape_convex(btAlignedObjectArray<btVector3> &p_vertices, const btVector3 &p_local_scaling = btVector3(1, 1, 1));
  77. static class btScaledBvhTriangleMeshShape *create_shape_concave(btBvhTriangleMeshShape *p_mesh_shape, const btVector3 &p_local_scaling = btVector3(1, 1, 1));
  78. 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);
  79. static class btRayShape *create_shape_ray(real_t p_length, bool p_slips_on_slope);
  80. };
  81. class PlaneShapeBullet : public ShapeBullet {
  82. Plane plane;
  83. public:
  84. PlaneShapeBullet();
  85. virtual void set_data(const Variant &p_data);
  86. virtual Variant get_data() const;
  87. virtual PhysicsServer::ShapeType get_type() const;
  88. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);
  89. private:
  90. void setup(const Plane &p_plane);
  91. };
  92. class SphereShapeBullet : public ShapeBullet {
  93. real_t radius;
  94. public:
  95. SphereShapeBullet();
  96. _FORCE_INLINE_ real_t get_radius() { return radius; }
  97. virtual void set_data(const Variant &p_data);
  98. virtual Variant get_data() const;
  99. virtual PhysicsServer::ShapeType get_type() const;
  100. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);
  101. private:
  102. void setup(real_t p_radius);
  103. };
  104. class BoxShapeBullet : public ShapeBullet {
  105. btVector3 half_extents;
  106. public:
  107. BoxShapeBullet();
  108. _FORCE_INLINE_ const btVector3 &get_half_extents() { return half_extents; }
  109. virtual void set_data(const Variant &p_data);
  110. virtual Variant get_data() const;
  111. virtual PhysicsServer::ShapeType get_type() const;
  112. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);
  113. private:
  114. void setup(const Vector3 &p_half_extents);
  115. };
  116. class CapsuleShapeBullet : public ShapeBullet {
  117. real_t height;
  118. real_t radius;
  119. public:
  120. CapsuleShapeBullet();
  121. _FORCE_INLINE_ real_t get_height() { return height; }
  122. _FORCE_INLINE_ real_t get_radius() { return radius; }
  123. virtual void set_data(const Variant &p_data);
  124. virtual Variant get_data() const;
  125. virtual PhysicsServer::ShapeType get_type() const;
  126. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);
  127. private:
  128. void setup(real_t p_height, real_t p_radius);
  129. };
  130. class CylinderShapeBullet : public ShapeBullet {
  131. real_t height;
  132. real_t radius;
  133. public:
  134. CylinderShapeBullet();
  135. _FORCE_INLINE_ real_t get_height() { return height; }
  136. _FORCE_INLINE_ real_t get_radius() { return radius; }
  137. virtual void set_data(const Variant &p_data);
  138. virtual Variant get_data() const;
  139. virtual PhysicsServer::ShapeType get_type() const;
  140. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_margin = 0);
  141. private:
  142. void setup(real_t p_height, real_t p_radius);
  143. };
  144. class ConvexPolygonShapeBullet : public ShapeBullet {
  145. public:
  146. btAlignedObjectArray<btVector3> vertices;
  147. ConvexPolygonShapeBullet();
  148. virtual void set_data(const Variant &p_data);
  149. void get_vertices(Vector<Vector3> &out_vertices);
  150. virtual Variant get_data() const;
  151. virtual PhysicsServer::ShapeType get_type() const;
  152. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);
  153. private:
  154. void setup(const Vector<Vector3> &p_vertices);
  155. };
  156. class ConcavePolygonShapeBullet : public ShapeBullet {
  157. class btBvhTriangleMeshShape *meshShape;
  158. public:
  159. PoolVector<Vector3> faces;
  160. ConcavePolygonShapeBullet();
  161. virtual ~ConcavePolygonShapeBullet();
  162. virtual void set_data(const Variant &p_data);
  163. virtual Variant get_data() const;
  164. virtual PhysicsServer::ShapeType get_type() const;
  165. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);
  166. private:
  167. void setup(PoolVector<Vector3> p_faces);
  168. };
  169. class HeightMapShapeBullet : public ShapeBullet {
  170. public:
  171. PoolVector<real_t> heights;
  172. int width;
  173. int depth;
  174. real_t min_height;
  175. real_t max_height;
  176. HeightMapShapeBullet();
  177. virtual void set_data(const Variant &p_data);
  178. virtual Variant get_data() const;
  179. virtual PhysicsServer::ShapeType get_type() const;
  180. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);
  181. private:
  182. void setup(PoolVector<real_t> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height);
  183. };
  184. class RayShapeBullet : public ShapeBullet {
  185. public:
  186. real_t length;
  187. bool slips_on_slope;
  188. RayShapeBullet();
  189. virtual void set_data(const Variant &p_data);
  190. virtual Variant get_data() const;
  191. virtual PhysicsServer::ShapeType get_type() const;
  192. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);
  193. private:
  194. void setup(real_t p_length, bool p_slips_on_slope);
  195. };
  196. #endif