collision_object_bullet.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. btTransform get_adjusted_transform() const;
  100. void claim_bt_shape(const btVector3 &body_scale);
  101. };
  102. protected:
  103. Type type;
  104. ObjectID instance_id;
  105. uint32_t collisionLayer;
  106. uint32_t collisionMask;
  107. bool collisionsEnabled;
  108. bool m_isStatic;
  109. bool ray_pickable;
  110. btCollisionObject *bt_collision_object;
  111. Vector3 body_scale;
  112. bool force_shape_reset;
  113. SpaceBullet *space;
  114. VSet<RID> exceptions;
  115. /// This array is used to know all areas where this Object is overlapped in
  116. /// New area is added when overlap with new area (AreaBullet::addOverlap), then is removed when it exit (CollisionObjectBullet::onExitArea)
  117. /// This array is used mainly to know which area hold the pointer of this object
  118. Vector<AreaBullet *> areasOverlapped;
  119. bool isTransformChanged;
  120. public:
  121. CollisionObjectBullet(Type p_type);
  122. virtual ~CollisionObjectBullet();
  123. Type getType() { return type; }
  124. protected:
  125. void destroyBulletCollisionObject();
  126. void setupBulletCollisionObject(btCollisionObject *p_collisionObject);
  127. public:
  128. _FORCE_INLINE_ btCollisionObject *get_bt_collision_object() { return bt_collision_object; }
  129. _FORCE_INLINE_ void set_instance_id(const ObjectID &p_instance_id) { instance_id = p_instance_id; }
  130. _FORCE_INLINE_ ObjectID get_instance_id() const { return instance_id; }
  131. _FORCE_INLINE_ bool is_static() const { return m_isStatic; }
  132. _FORCE_INLINE_ void set_ray_pickable(bool p_enable) { ray_pickable = p_enable; }
  133. _FORCE_INLINE_ bool is_ray_pickable() const { return ray_pickable; }
  134. void set_body_scale(const Vector3 &p_new_scale);
  135. const Vector3 &get_body_scale() const { return body_scale; }
  136. btVector3 get_bt_body_scale() const;
  137. virtual void body_scale_changed();
  138. void add_collision_exception(const CollisionObjectBullet *p_ignoreCollisionObject);
  139. void remove_collision_exception(const CollisionObjectBullet *p_ignoreCollisionObject);
  140. bool has_collision_exception(const CollisionObjectBullet *p_otherCollisionObject) const;
  141. _FORCE_INLINE_ const VSet<RID> &get_exceptions() const { return exceptions; }
  142. _FORCE_INLINE_ void set_collision_layer(uint32_t p_layer) {
  143. if (collisionLayer != p_layer) {
  144. collisionLayer = p_layer;
  145. on_collision_filters_change();
  146. }
  147. }
  148. _FORCE_INLINE_ uint32_t get_collision_layer() const { return collisionLayer; }
  149. _FORCE_INLINE_ void set_collision_mask(uint32_t p_mask) {
  150. if (collisionMask != p_mask) {
  151. collisionMask = p_mask;
  152. on_collision_filters_change();
  153. }
  154. }
  155. _FORCE_INLINE_ uint32_t get_collision_mask() const { return collisionMask; }
  156. virtual void on_collision_filters_change() = 0;
  157. _FORCE_INLINE_ bool test_collision_mask(CollisionObjectBullet *p_other) const {
  158. return collisionLayer & p_other->collisionMask || p_other->collisionLayer & collisionMask;
  159. }
  160. virtual void reload_body() = 0;
  161. virtual void set_space(SpaceBullet *p_space) = 0;
  162. _FORCE_INLINE_ SpaceBullet *get_space() const { return space; }
  163. virtual void on_collision_checker_start() = 0;
  164. virtual void on_collision_checker_end() = 0;
  165. virtual void dispatch_callbacks() = 0;
  166. void set_collision_enabled(bool p_enabled);
  167. bool is_collisions_response_enabled();
  168. void notify_new_overlap(AreaBullet *p_area);
  169. virtual void on_enter_area(AreaBullet *p_area) = 0;
  170. virtual void on_exit_area(AreaBullet *p_area);
  171. void set_godot_object_flags(int flags);
  172. int get_godot_object_flags() const;
  173. void set_transform(const Transform &p_global_transform);
  174. Transform get_transform() const;
  175. virtual void set_transform__bullet(const btTransform &p_global_transform);
  176. virtual const btTransform &get_transform__bullet() const;
  177. bool is_transform_changed() const { return isTransformChanged; }
  178. virtual void notify_transform_changed();
  179. };
  180. class RigidCollisionObjectBullet : public CollisionObjectBullet, public ShapeOwnerBullet {
  181. protected:
  182. btCollisionShape *mainShape;
  183. Vector<ShapeWrapper> shapes;
  184. public:
  185. RigidCollisionObjectBullet(Type p_type);
  186. ~RigidCollisionObjectBullet();
  187. _FORCE_INLINE_ const Vector<ShapeWrapper> &get_shapes_wrappers() const { return shapes; }
  188. _FORCE_INLINE_ btCollisionShape *get_main_shape() const { return mainShape; }
  189. void add_shape(ShapeBullet *p_shape, const Transform &p_transform = Transform(), bool p_disabled = false);
  190. void set_shape(int p_index, ShapeBullet *p_shape);
  191. int get_shape_count() const;
  192. ShapeBullet *get_shape(int p_index) const;
  193. btCollisionShape *get_bt_shape(int p_index) const;
  194. int find_shape(ShapeBullet *p_shape) const;
  195. virtual void remove_shape_full(ShapeBullet *p_shape);
  196. void remove_shape_full(int p_index);
  197. void remove_all_shapes(bool p_permanentlyFromThisBody = false, bool p_force_not_reload = false);
  198. void set_shape_transform(int p_index, const Transform &p_transform);
  199. const btTransform &get_bt_shape_transform(int p_index) const;
  200. Transform get_shape_transform(int p_index) const;
  201. void set_shape_disabled(int p_index, bool p_disabled);
  202. bool is_shape_disabled(int p_index);
  203. virtual void shape_changed(int p_shape_index);
  204. virtual void reload_shapes();
  205. virtual void main_shape_changed() = 0;
  206. virtual void body_scale_changed();
  207. private:
  208. void internal_shape_destroy(int p_index, bool p_permanentlyFromThisBody = false);
  209. };
  210. #endif