collision_object_bullet.h 9.0 KB

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