shape_bullet.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*************************************************************************/
  2. /* shape_bullet.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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/variant.h"
  33. #include "geometry.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. protected:
  49. /// return self
  50. btCollisionShape *prepare(btCollisionShape *p_btShape) const;
  51. void notifyShapeChanged();
  52. public:
  53. ShapeBullet();
  54. virtual ~ShapeBullet();
  55. btCollisionShape *create_bt_shape(const Vector3 &p_implicit_scale, real_t p_margin = 0);
  56. virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_margin = 0) = 0;
  57. void add_owner(ShapeOwnerBullet *p_owner);
  58. void remove_owner(ShapeOwnerBullet *p_owner, bool p_permanentlyFromThisBody = false);
  59. bool is_owner(ShapeOwnerBullet *p_owner) const;
  60. const Map<ShapeOwnerBullet *, int> &get_owners() const;
  61. /// Setup the shape
  62. virtual void set_data(const Variant &p_data) = 0;
  63. virtual Variant get_data() const = 0;
  64. virtual PhysicsServer::ShapeType get_type() const = 0;
  65. public:
  66. static class btEmptyShape *create_shape_empty();
  67. static class btStaticPlaneShape *create_shape_plane(const btVector3 &planeNormal, btScalar planeConstant);
  68. static class btSphereShape *create_shape_sphere(btScalar radius);
  69. static class btBoxShape *create_shape_box(const btVector3 &boxHalfExtents);
  70. static class btCapsuleShapeZ *create_shape_capsule(btScalar radius, btScalar height);
  71. /// IMPORTANT: Remember to delete the shape interface by calling: delete my_shape->getMeshInterface();
  72. static class btConvexPointCloudShape *create_shape_convex(btAlignedObjectArray<btVector3> &p_vertices, const btVector3 &p_local_scaling = btVector3(1, 1, 1));
  73. static class btScaledBvhTriangleMeshShape *create_shape_concave(btBvhTriangleMeshShape *p_mesh_shape, const btVector3 &p_local_scaling = btVector3(1, 1, 1));
  74. static class btHeightfieldTerrainShape *create_shape_height_field(PoolVector<real_t> &p_heights, int p_width, int p_depth, real_t p_cell_size);
  75. static class btRayShape *create_shape_ray(real_t p_length);
  76. };
  77. class PlaneShapeBullet : public ShapeBullet {
  78. Plane plane;
  79. public:
  80. PlaneShapeBullet();
  81. virtual void set_data(const Variant &p_data);
  82. virtual Variant get_data() const;
  83. virtual PhysicsServer::ShapeType get_type() const;
  84. virtual btCollisionShape *create_bt_shape(const btVector3 &p_scale, real_t p_margin = 0);
  85. private:
  86. void setup(const Plane &p_plane);
  87. };
  88. class SphereShapeBullet : public ShapeBullet {
  89. real_t radius;
  90. public:
  91. SphereShapeBullet();
  92. _FORCE_INLINE_ real_t get_radius() { return radius; }
  93. virtual void set_data(const Variant &p_data);
  94. virtual Variant get_data() const;
  95. virtual PhysicsServer::ShapeType get_type() const;
  96. virtual btCollisionShape *create_bt_shape(const btVector3 &p_scale, real_t p_margin = 0);
  97. private:
  98. void setup(real_t p_radius);
  99. };
  100. class BoxShapeBullet : public ShapeBullet {
  101. btVector3 half_extents;
  102. public:
  103. BoxShapeBullet();
  104. _FORCE_INLINE_ const btVector3 &get_half_extents() { return half_extents; }
  105. virtual void set_data(const Variant &p_data);
  106. virtual Variant get_data() const;
  107. virtual PhysicsServer::ShapeType get_type() const;
  108. virtual btCollisionShape *create_bt_shape(const btVector3 &p_scale, real_t p_margin = 0);
  109. private:
  110. void setup(const Vector3 &p_half_extents);
  111. };
  112. class CapsuleShapeBullet : public ShapeBullet {
  113. real_t height;
  114. real_t radius;
  115. public:
  116. CapsuleShapeBullet();
  117. _FORCE_INLINE_ real_t get_height() { return height; }
  118. _FORCE_INLINE_ real_t get_radius() { return radius; }
  119. virtual void set_data(const Variant &p_data);
  120. virtual Variant get_data() const;
  121. virtual PhysicsServer::ShapeType get_type() const;
  122. virtual btCollisionShape *create_bt_shape(const btVector3 &p_scale, real_t p_margin = 0);
  123. private:
  124. void setup(real_t p_height, real_t p_radius);
  125. };
  126. class ConvexPolygonShapeBullet : public ShapeBullet {
  127. public:
  128. btAlignedObjectArray<btVector3> vertices;
  129. ConvexPolygonShapeBullet();
  130. virtual void set_data(const Variant &p_data);
  131. void get_vertices(Vector<Vector3> &out_vertices);
  132. virtual Variant get_data() const;
  133. virtual PhysicsServer::ShapeType get_type() const;
  134. virtual btCollisionShape *create_bt_shape(const btVector3 &p_scale, real_t p_margin = 0);
  135. private:
  136. void setup(const Vector<Vector3> &p_vertices);
  137. };
  138. class ConcavePolygonShapeBullet : public ShapeBullet {
  139. class btBvhTriangleMeshShape *meshShape;
  140. public:
  141. PoolVector<Vector3> faces;
  142. ConcavePolygonShapeBullet();
  143. virtual ~ConcavePolygonShapeBullet();
  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_scale, real_t p_margin = 0);
  148. private:
  149. void setup(PoolVector<Vector3> p_faces);
  150. };
  151. class HeightMapShapeBullet : public ShapeBullet {
  152. public:
  153. PoolVector<real_t> heights;
  154. int width;
  155. int depth;
  156. real_t cell_size;
  157. HeightMapShapeBullet();
  158. virtual void set_data(const Variant &p_data);
  159. virtual Variant get_data() const;
  160. virtual PhysicsServer::ShapeType get_type() const;
  161. virtual btCollisionShape *create_bt_shape(const btVector3 &p_scale, real_t p_margin = 0);
  162. private:
  163. void setup(PoolVector<real_t> &p_heights, int p_width, int p_depth, real_t p_cell_size);
  164. };
  165. class RayShapeBullet : public ShapeBullet {
  166. public:
  167. real_t length;
  168. RayShapeBullet();
  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_scale, real_t p_margin = 0);
  173. private:
  174. void setup(real_t p_length);
  175. };
  176. #endif