collision_object_bullet.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**************************************************************************/
  2. /* collision_object_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 COLLISION_OBJECT_BULLET_H
  31. #define COLLISION_OBJECT_BULLET_H
  32. #include "core/math/transform.h"
  33. #include "core/math/vector3.h"
  34. #include "core/object.h"
  35. #include "core/vset.h"
  36. #include "shape_owner_bullet.h"
  37. #if defined(__clang__) && (__clang_major__ >= 13)
  38. #pragma clang diagnostic push
  39. #pragma clang diagnostic ignored "-Wdeprecated-copy-with-user-provided-copy"
  40. #endif
  41. #include <LinearMath/btTransform.h>
  42. #if defined(__clang__) && (__clang_major__ >= 13)
  43. #pragma clang diagnostic pop
  44. #endif
  45. /**
  46. @author AndreaCatania
  47. */
  48. class AreaBullet;
  49. class ShapeBullet;
  50. class btCollisionObject;
  51. class btCompoundShape;
  52. class btCollisionShape;
  53. class SpaceBullet;
  54. class CollisionObjectBullet : public RIDBullet {
  55. public:
  56. enum GodotObjectFlags {
  57. GOF_IS_MONITORING_AREA = 1 << 0
  58. // FLAG2 = 1 << 1,
  59. // FLAG3 = 1 << 2,
  60. // FLAG4 = 1 << 3,
  61. // FLAG5 = 1 << 4,
  62. // FLAG6 = 1 << 5
  63. // etc..
  64. };
  65. enum Type {
  66. TYPE_AREA = 0,
  67. TYPE_RIGID_BODY,
  68. TYPE_SOFT_BODY,
  69. TYPE_KINEMATIC_GHOST_BODY
  70. };
  71. struct ShapeWrapper {
  72. ShapeBullet *shape;
  73. btCollisionShape *bt_shape;
  74. btTransform transform;
  75. btVector3 scale;
  76. bool active;
  77. ShapeWrapper() :
  78. shape(nullptr),
  79. bt_shape(nullptr),
  80. active(true) {}
  81. ShapeWrapper(ShapeBullet *p_shape, const btTransform &p_transform, bool p_active) :
  82. shape(p_shape),
  83. bt_shape(nullptr),
  84. active(p_active) {
  85. set_transform(p_transform);
  86. }
  87. ShapeWrapper(ShapeBullet *p_shape, const Transform &p_transform, bool p_active) :
  88. shape(p_shape),
  89. bt_shape(nullptr),
  90. active(p_active) {
  91. set_transform(p_transform);
  92. }
  93. ~ShapeWrapper();
  94. ShapeWrapper(const ShapeWrapper &otherShape) {
  95. operator=(otherShape);
  96. }
  97. void operator=(const ShapeWrapper &otherShape) {
  98. shape = otherShape.shape;
  99. bt_shape = otherShape.bt_shape;
  100. transform = otherShape.transform;
  101. scale = otherShape.scale;
  102. active = otherShape.active;
  103. }
  104. void set_transform(const Transform &p_transform);
  105. void set_transform(const btTransform &p_transform);
  106. btTransform get_adjusted_transform() const;
  107. void claim_bt_shape(const btVector3 &body_scale);
  108. };
  109. protected:
  110. Type type;
  111. ObjectID instance_id;
  112. uint32_t collisionLayer;
  113. uint32_t collisionMask;
  114. bool collisionsEnabled;
  115. bool m_isStatic;
  116. bool ray_pickable;
  117. btCollisionObject *bt_collision_object;
  118. Vector3 body_scale;
  119. bool force_shape_reset;
  120. SpaceBullet *space;
  121. VSet<RID> exceptions;
  122. /// This array is used to know all areas where this Object is overlapped in
  123. /// New area is added when overlap with new area (AreaBullet::addOverlap), then is removed when it exit (CollisionObjectBullet::onExitArea)
  124. /// This array is used mainly to know which area hold the pointer of this object
  125. Vector<AreaBullet *> areasOverlapped;
  126. bool updated = false;
  127. public:
  128. CollisionObjectBullet(Type p_type);
  129. virtual ~CollisionObjectBullet();
  130. Type getType() { return type; }
  131. protected:
  132. void destroyBulletCollisionObject();
  133. void setupBulletCollisionObject(btCollisionObject *p_collisionObject);
  134. public:
  135. _FORCE_INLINE_ btCollisionObject *get_bt_collision_object() { return bt_collision_object; }
  136. _FORCE_INLINE_ void set_instance_id(const ObjectID &p_instance_id) { instance_id = p_instance_id; }
  137. _FORCE_INLINE_ ObjectID get_instance_id() const { return instance_id; }
  138. _FORCE_INLINE_ bool is_static() const { return m_isStatic; }
  139. _FORCE_INLINE_ void set_ray_pickable(bool p_enable) { ray_pickable = p_enable; }
  140. _FORCE_INLINE_ bool is_ray_pickable() const { return ray_pickable; }
  141. void set_body_scale(const Vector3 &p_new_scale);
  142. const Vector3 &get_body_scale() const { return body_scale; }
  143. btVector3 get_bt_body_scale() const;
  144. virtual void body_scale_changed();
  145. void add_collision_exception(const CollisionObjectBullet *p_ignoreCollisionObject);
  146. void remove_collision_exception(const CollisionObjectBullet *p_ignoreCollisionObject);
  147. bool has_collision_exception(const CollisionObjectBullet *p_otherCollisionObject) const;
  148. _FORCE_INLINE_ const VSet<RID> &get_exceptions() const { return exceptions; }
  149. _FORCE_INLINE_ void set_collision_layer(uint32_t p_layer) {
  150. if (collisionLayer != p_layer) {
  151. collisionLayer = p_layer;
  152. on_collision_filters_change();
  153. }
  154. }
  155. _FORCE_INLINE_ uint32_t get_collision_layer() const { return collisionLayer; }
  156. _FORCE_INLINE_ void set_collision_mask(uint32_t p_mask) {
  157. if (collisionMask != p_mask) {
  158. collisionMask = p_mask;
  159. on_collision_filters_change();
  160. }
  161. }
  162. _FORCE_INLINE_ uint32_t get_collision_mask() const { return collisionMask; }
  163. virtual void on_collision_filters_change() = 0;
  164. _FORCE_INLINE_ bool test_collision_mask(CollisionObjectBullet *p_other) const {
  165. return collisionLayer & p_other->collisionMask || p_other->collisionLayer & collisionMask;
  166. }
  167. virtual void reload_body() = 0;
  168. virtual void set_space(SpaceBullet *p_space) = 0;
  169. _FORCE_INLINE_ SpaceBullet *get_space() const { return space; }
  170. virtual void on_collision_checker_start() = 0;
  171. virtual void on_collision_checker_end() = 0;
  172. virtual void dispatch_callbacks() = 0;
  173. void set_collision_enabled(bool p_enabled);
  174. bool is_collisions_response_enabled();
  175. void notify_new_overlap(AreaBullet *p_area);
  176. virtual void on_enter_area(AreaBullet *p_area) = 0;
  177. virtual void on_exit_area(AreaBullet *p_area);
  178. void set_godot_object_flags(int flags);
  179. int get_godot_object_flags() const;
  180. void set_transform(const Transform &p_global_transform);
  181. Transform get_transform() const;
  182. virtual void set_transform__bullet(const btTransform &p_global_transform);
  183. virtual const btTransform &get_transform__bullet() const;
  184. virtual void notify_transform_changed();
  185. bool is_updated() const { return updated; }
  186. };
  187. class RigidCollisionObjectBullet : public CollisionObjectBullet, public ShapeOwnerBullet {
  188. protected:
  189. btCollisionShape *mainShape;
  190. Vector<ShapeWrapper> shapes;
  191. public:
  192. RigidCollisionObjectBullet(Type p_type);
  193. ~RigidCollisionObjectBullet();
  194. _FORCE_INLINE_ const Vector<ShapeWrapper> &get_shapes_wrappers() const { return shapes; }
  195. _FORCE_INLINE_ btCollisionShape *get_main_shape() const { return mainShape; }
  196. void add_shape(ShapeBullet *p_shape, const Transform &p_transform = Transform(), bool p_disabled = false);
  197. void set_shape(int p_index, ShapeBullet *p_shape);
  198. int get_shape_count() const;
  199. ShapeBullet *get_shape(int p_index) const;
  200. btCollisionShape *get_bt_shape(int p_index) const;
  201. int find_shape(ShapeBullet *p_shape) const;
  202. virtual void remove_shape_full(ShapeBullet *p_shape);
  203. void remove_shape_full(int p_index);
  204. void remove_all_shapes(bool p_permanentlyFromThisBody = false, bool p_force_not_reload = false);
  205. void set_shape_transform(int p_index, const Transform &p_transform);
  206. const btTransform &get_bt_shape_transform(int p_index) const;
  207. Transform get_shape_transform(int p_index) const;
  208. void set_shape_disabled(int p_index, bool p_disabled);
  209. bool is_shape_disabled(int p_index);
  210. virtual void shape_changed(int p_shape_index);
  211. virtual void reload_shapes();
  212. virtual void main_shape_changed() = 0;
  213. virtual void body_scale_changed();
  214. private:
  215. void internal_shape_destroy(int p_index, bool p_permanentlyFromThisBody = false);
  216. };
  217. #endif // COLLISION_OBJECT_BULLET_H