rigid_body_bullet.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**************************************************************************/
  2. /* rigid_body_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 RIGID_BODY_BULLET_H
  31. #define RIGID_BODY_BULLET_H
  32. #include "collision_object_bullet.h"
  33. #include "space_bullet.h"
  34. #include <BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h>
  35. #include <LinearMath/btTransform.h>
  36. /**
  37. @author AndreaCatania
  38. */
  39. class AreaBullet;
  40. class SpaceBullet;
  41. class btRigidBody;
  42. class GodotMotionState;
  43. class BulletPhysicsDirectBodyState : public PhysicsDirectBodyState {
  44. GDCLASS(BulletPhysicsDirectBodyState, PhysicsDirectBodyState);
  45. public:
  46. RigidBodyBullet *body = nullptr;
  47. BulletPhysicsDirectBodyState() {}
  48. virtual Vector3 get_total_gravity() const;
  49. virtual float get_total_angular_damp() const;
  50. virtual float get_total_linear_damp() const;
  51. virtual Vector3 get_center_of_mass() const;
  52. virtual Basis get_principal_inertia_axes() const;
  53. // get the mass
  54. virtual float get_inverse_mass() const;
  55. // get density of this body space
  56. virtual Vector3 get_inverse_inertia() const;
  57. // get density of this body space
  58. virtual Basis get_inverse_inertia_tensor() const;
  59. virtual void set_linear_velocity(const Vector3 &p_velocity);
  60. virtual Vector3 get_linear_velocity() const;
  61. virtual void set_angular_velocity(const Vector3 &p_velocity);
  62. virtual Vector3 get_angular_velocity() const;
  63. virtual void set_transform(const Transform &p_transform);
  64. virtual Transform get_transform() const;
  65. virtual Vector3 get_velocity_at_local_position(const Vector3 &p_position) const;
  66. virtual void add_central_force(const Vector3 &p_force);
  67. virtual void add_force(const Vector3 &p_force, const Vector3 &p_pos);
  68. virtual void add_torque(const Vector3 &p_torque);
  69. virtual void apply_central_impulse(const Vector3 &p_impulse);
  70. virtual void apply_impulse(const Vector3 &p_pos, const Vector3 &p_impulse);
  71. virtual void apply_torque_impulse(const Vector3 &p_impulse);
  72. virtual void set_sleep_state(bool p_enable);
  73. virtual bool is_sleeping() const;
  74. virtual int get_contact_count() const;
  75. virtual Vector3 get_contact_local_position(int p_contact_idx) const;
  76. virtual Vector3 get_contact_local_normal(int p_contact_idx) const;
  77. virtual float get_contact_impulse(int p_contact_idx) const;
  78. virtual int get_contact_local_shape(int p_contact_idx) const;
  79. virtual RID get_contact_collider(int p_contact_idx) const;
  80. virtual Vector3 get_contact_collider_position(int p_contact_idx) const;
  81. virtual ObjectID get_contact_collider_id(int p_contact_idx) const;
  82. virtual int get_contact_collider_shape(int p_contact_idx) const;
  83. virtual Vector3 get_contact_collider_velocity_at_position(int p_contact_idx) const;
  84. virtual real_t get_step() const;
  85. virtual void integrate_forces() {
  86. // Skip the execution of this function
  87. }
  88. virtual PhysicsDirectSpaceState *get_space_state();
  89. };
  90. class RigidBodyBullet : public RigidCollisionObjectBullet {
  91. public:
  92. struct CollisionData {
  93. RigidBodyBullet *otherObject;
  94. int other_object_shape;
  95. int local_shape;
  96. Vector3 hitLocalLocation;
  97. Vector3 hitWorldLocation;
  98. Vector3 hitNormal;
  99. float appliedImpulse;
  100. };
  101. struct ForceIntegrationCallback {
  102. ObjectID id;
  103. StringName method;
  104. Variant udata;
  105. };
  106. /// Used to hold shapes
  107. struct KinematicShape {
  108. class btConvexShape *shape;
  109. btTransform transform;
  110. KinematicShape() :
  111. shape(nullptr) {}
  112. bool is_active() const { return shape; }
  113. };
  114. struct KinematicUtilities {
  115. RigidBodyBullet *owner;
  116. btScalar safe_margin;
  117. Vector<KinematicShape> shapes;
  118. KinematicUtilities(RigidBodyBullet *p_owner);
  119. ~KinematicUtilities();
  120. void setSafeMargin(btScalar p_margin);
  121. /// Used to set the default shape to ghost
  122. void copyAllOwnerShapes();
  123. private:
  124. void just_delete_shapes(int new_size);
  125. };
  126. private:
  127. BulletPhysicsDirectBodyState *direct_access = nullptr;
  128. friend class BulletPhysicsDirectBodyState;
  129. // This is required only for Kinematic movement
  130. KinematicUtilities *kinematic_utilities;
  131. PhysicsServer::BodyMode mode;
  132. GodotMotionState *godotMotionState;
  133. btRigidBody *btBody;
  134. uint16_t locked_axis;
  135. real_t mass;
  136. real_t gravity_scale;
  137. real_t linearDamp;
  138. real_t angularDamp;
  139. Vector3 total_gravity;
  140. real_t total_linear_damp;
  141. real_t total_angular_damp;
  142. bool can_sleep;
  143. bool omit_forces_integration;
  144. bool can_integrate_forces;
  145. Vector<CollisionData> collisions;
  146. Vector<RigidBodyBullet *> collision_traces_1;
  147. Vector<RigidBodyBullet *> collision_traces_2;
  148. Vector<RigidBodyBullet *> *prev_collision_traces;
  149. Vector<RigidBodyBullet *> *curr_collision_traces;
  150. // these parameters are used to avoid vector resize
  151. int maxCollisionsDetection;
  152. int collisionsCount;
  153. int prev_collision_count;
  154. Vector<AreaBullet *> areasWhereIam;
  155. // these parameters are used to avoid vector resize
  156. int maxAreasWhereIam;
  157. int areaWhereIamCount;
  158. // Used to know if the area is used as gravity point
  159. int countGravityPointSpaces;
  160. bool isScratchedSpaceOverrideModificator;
  161. bool previousActiveState; // Last check state
  162. ForceIntegrationCallback *force_integration_callback;
  163. public:
  164. RigidBodyBullet();
  165. ~RigidBodyBullet();
  166. BulletPhysicsDirectBodyState *get_direct_state() const { return direct_access; }
  167. void init_kinematic_utilities();
  168. void destroy_kinematic_utilities();
  169. _FORCE_INLINE_ KinematicUtilities *get_kinematic_utilities() const { return kinematic_utilities; }
  170. _FORCE_INLINE_ btRigidBody *get_bt_rigid_body() { return btBody; }
  171. virtual void main_shape_changed();
  172. virtual void reload_body();
  173. virtual void set_space(SpaceBullet *p_space);
  174. virtual void dispatch_callbacks();
  175. void set_force_integration_callback(ObjectID p_id, const StringName &p_method, const Variant &p_udata = Variant());
  176. void scratch_space_override_modificator();
  177. virtual void on_collision_filters_change();
  178. virtual void on_collision_checker_start();
  179. virtual void on_collision_checker_end();
  180. void set_max_collisions_detection(int p_maxCollisionsDetection) {
  181. ERR_FAIL_COND(0 > p_maxCollisionsDetection);
  182. maxCollisionsDetection = p_maxCollisionsDetection;
  183. collisions.resize(p_maxCollisionsDetection);
  184. collision_traces_1.resize(p_maxCollisionsDetection);
  185. collision_traces_2.resize(p_maxCollisionsDetection);
  186. collisionsCount = 0;
  187. prev_collision_count = MIN(prev_collision_count, p_maxCollisionsDetection);
  188. }
  189. int get_max_collisions_detection() {
  190. return maxCollisionsDetection;
  191. }
  192. bool can_add_collision() { return collisionsCount < maxCollisionsDetection; }
  193. bool add_collision_object(RigidBodyBullet *p_otherObject, const Vector3 &p_hitWorldLocation, const Vector3 &p_hitLocalLocation, const Vector3 &p_hitNormal, const float &p_appliedImpulse, int p_other_shape_index, int p_local_shape_index);
  194. bool was_colliding(RigidBodyBullet *p_other_object);
  195. void set_activation_state(bool p_active);
  196. bool is_active() const;
  197. void set_omit_forces_integration(bool p_omit);
  198. _FORCE_INLINE_ bool get_omit_forces_integration() const { return omit_forces_integration; }
  199. void set_param(PhysicsServer::BodyParameter p_param, real_t);
  200. real_t get_param(PhysicsServer::BodyParameter p_param) const;
  201. void set_mode(PhysicsServer::BodyMode p_mode);
  202. PhysicsServer::BodyMode get_mode() const;
  203. void set_state(PhysicsServer::BodyState p_state, const Variant &p_variant);
  204. Variant get_state(PhysicsServer::BodyState p_state) const;
  205. void apply_impulse(const Vector3 &p_pos, const Vector3 &p_impulse);
  206. void apply_central_impulse(const Vector3 &p_impulse);
  207. void apply_torque_impulse(const Vector3 &p_impulse);
  208. void apply_force(const Vector3 &p_force, const Vector3 &p_pos);
  209. void apply_central_force(const Vector3 &p_force);
  210. void apply_torque(const Vector3 &p_torque);
  211. void set_applied_force(const Vector3 &p_force);
  212. Vector3 get_applied_force() const;
  213. void set_applied_torque(const Vector3 &p_torque);
  214. Vector3 get_applied_torque() const;
  215. void set_axis_lock(PhysicsServer::BodyAxis p_axis, bool lock);
  216. bool is_axis_locked(PhysicsServer::BodyAxis p_axis) const;
  217. void reload_axis_lock();
  218. /// Doc:
  219. /// https://web.archive.org/web/20180404091446/http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Anti_tunneling_by_Motion_Clamping
  220. void set_continuous_collision_detection(bool p_enable);
  221. bool is_continuous_collision_detection_enabled() const;
  222. void set_linear_velocity(const Vector3 &p_velocity);
  223. Vector3 get_linear_velocity() const;
  224. void set_angular_velocity(const Vector3 &p_velocity);
  225. Vector3 get_angular_velocity() const;
  226. virtual void set_transform__bullet(const btTransform &p_global_transform);
  227. virtual const btTransform &get_transform__bullet() const;
  228. virtual void reload_shapes();
  229. virtual void on_enter_area(AreaBullet *p_area);
  230. virtual void on_exit_area(AreaBullet *p_area);
  231. void reload_space_override_modificator();
  232. /// Kinematic
  233. void reload_kinematic_shapes();
  234. virtual void notify_transform_changed();
  235. private:
  236. void _internal_set_mass(real_t p_mass);
  237. };
  238. #endif // RIGID_BODY_BULLET_H