collision_object_bullet.h 8.8 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-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 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. #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;
  66. btCollisionShape *bt_shape;
  67. btTransform transform;
  68. btVector3 scale;
  69. bool active;
  70. ShapeWrapper() :
  71. shape(NULL),
  72. bt_shape(NULL),
  73. active(true) {}
  74. ShapeWrapper(ShapeBullet *p_shape, const btTransform &p_transform, bool p_active) :
  75. shape(p_shape),
  76. bt_shape(NULL),
  77. active(p_active) {
  78. set_transform(p_transform);
  79. }
  80. ShapeWrapper(ShapeBullet *p_shape, const Transform &p_transform, bool p_active) :
  81. shape(p_shape),
  82. bt_shape(NULL),
  83. active(p_active) {
  84. set_transform(p_transform);
  85. }
  86. ~ShapeWrapper();
  87. ShapeWrapper(const ShapeWrapper &otherShape) {
  88. operator=(otherShape);
  89. }
  90. void operator=(const ShapeWrapper &otherShape) {
  91. shape = otherShape.shape;
  92. bt_shape = otherShape.bt_shape;
  93. transform = otherShape.transform;
  94. scale = otherShape.scale;
  95. active = otherShape.active;
  96. }
  97. void set_transform(const Transform &p_transform);
  98. void set_transform(const btTransform &p_transform);
  99. void claim_bt_shape(const btVector3 &body_scale);
  100. };
  101. protected:
  102. Type type;
  103. ObjectID instance_id;
  104. uint32_t collisionLayer;
  105. uint32_t collisionMask;
  106. bool collisionsEnabled;
  107. bool m_isStatic;
  108. bool ray_pickable;
  109. btCollisionObject *bt_collision_object;
  110. Vector3 body_scale;
  111. bool force_shape_reset;
  112. SpaceBullet *space;
  113. VSet<RID> exceptions;
  114. /// This array is used to know all areas where this Object is overlapped in
  115. /// New area is added when overlap with new area (AreaBullet::addOverlap), then is removed when it exit (CollisionObjectBullet::onExitArea)
  116. /// This array is used mainly to know which area hold the pointer of this object
  117. Vector<AreaBullet *> areasOverlapped;
  118. bool isTransformChanged;
  119. public:
  120. CollisionObjectBullet(Type p_type);
  121. virtual ~CollisionObjectBullet();
  122. Type getType() { return type; }
  123. protected:
  124. void destroyBulletCollisionObject();
  125. void setupBulletCollisionObject(btCollisionObject *p_collisionObject);
  126. public:
  127. _FORCE_INLINE_ btCollisionObject *get_bt_collision_object() { return bt_collision_object; }
  128. _FORCE_INLINE_ void set_instance_id(const ObjectID &p_instance_id) { instance_id = p_instance_id; }
  129. _FORCE_INLINE_ ObjectID get_instance_id() const { return instance_id; }
  130. _FORCE_INLINE_ bool is_static() const { return m_isStatic; }
  131. _FORCE_INLINE_ void set_ray_pickable(bool p_enable) { ray_pickable = p_enable; }
  132. _FORCE_INLINE_ bool is_ray_pickable() const { return ray_pickable; }
  133. void set_body_scale(const Vector3 &p_new_scale);
  134. const Vector3 &get_body_scale() const { return body_scale; }
  135. btVector3 get_bt_body_scale() const;
  136. virtual void body_scale_changed();
  137. void add_collision_exception(const CollisionObjectBullet *p_ignoreCollisionObject);
  138. void remove_collision_exception(const CollisionObjectBullet *p_ignoreCollisionObject);
  139. bool has_collision_exception(const CollisionObjectBullet *p_otherCollisionObject) const;
  140. _FORCE_INLINE_ const VSet<RID> &get_exceptions() const { return exceptions; }
  141. _FORCE_INLINE_ void set_collision_layer(uint32_t p_layer) {
  142. collisionLayer = p_layer;
  143. on_collision_filters_change();
  144. }
  145. _FORCE_INLINE_ uint32_t get_collision_layer() const { return collisionLayer; }
  146. _FORCE_INLINE_ void set_collision_mask(uint32_t p_mask) {
  147. collisionMask = p_mask;
  148. on_collision_filters_change();
  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;
  178. Vector<ShapeWrapper> shapes;
  179. public:
  180. RigidCollisionObjectBullet(Type p_type);
  181. ~RigidCollisionObjectBullet();
  182. _FORCE_INLINE_ const Vector<ShapeWrapper> &get_shapes_wrappers() const { return shapes; }
  183. _FORCE_INLINE_ btCollisionShape *get_main_shape() const { return mainShape; }
  184. void add_shape(ShapeBullet *p_shape, const Transform &p_transform = Transform());
  185. void set_shape(int p_index, ShapeBullet *p_shape);
  186. int get_shape_count() const;
  187. ShapeBullet *get_shape(int p_index) const;
  188. btCollisionShape *get_bt_shape(int p_index) const;
  189. int find_shape(ShapeBullet *p_shape) const;
  190. virtual void remove_shape_full(ShapeBullet *p_shape);
  191. void remove_shape_full(int p_index);
  192. void remove_all_shapes(bool p_permanentlyFromThisBody = false, bool p_force_not_reload = false);
  193. void set_shape_transform(int p_index, const Transform &p_transform);
  194. const btTransform &get_bt_shape_transform(int p_index) const;
  195. Transform get_shape_transform(int p_index) const;
  196. void set_shape_disabled(int p_index, bool p_disabled);
  197. bool is_shape_disabled(int p_index);
  198. virtual void shape_changed(int p_shape_index);
  199. virtual void reload_shapes();
  200. virtual void main_shape_changed() = 0;
  201. virtual void body_scale_changed();
  202. private:
  203. void internal_shape_destroy(int p_index, bool p_permanentlyFromThisBody = false);
  204. };
  205. #endif