rigid_body_bullet.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*************************************************************************/
  2. /* rigid_body_bullet.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 BODYBULLET_H
  31. #define BODYBULLET_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;
  44. /// This class could be used in multi thread with few changes but currently
  45. /// is set to be only in one single thread.
  46. ///
  47. /// In the system there is only one object at a time that manage all bodies and is
  48. /// created by BulletPhysicsServer and is held by the "singleton" variable of this class
  49. /// Each time something require it, the body must be set again.
  50. class BulletPhysicsDirectBodyState : public PhysicsDirectBodyState {
  51. GDCLASS(BulletPhysicsDirectBodyState, PhysicsDirectBodyState);
  52. static BulletPhysicsDirectBodyState *singleton;
  53. public:
  54. /// This class avoid the creation of more object of this class
  55. static void initSingleton() {
  56. if (!singleton) {
  57. singleton = memnew(BulletPhysicsDirectBodyState);
  58. }
  59. }
  60. static void destroySingleton() {
  61. memdelete(singleton);
  62. singleton = nullptr;
  63. }
  64. static void singleton_setDeltaTime(real_t p_deltaTime) {
  65. singleton->deltaTime = p_deltaTime;
  66. }
  67. static BulletPhysicsDirectBodyState *get_singleton(RigidBodyBullet *p_body) {
  68. singleton->body = p_body;
  69. return singleton;
  70. }
  71. public:
  72. RigidBodyBullet *body;
  73. real_t deltaTime;
  74. private:
  75. BulletPhysicsDirectBodyState() {}
  76. public:
  77. virtual Vector3 get_total_gravity() const;
  78. virtual float get_total_angular_damp() const;
  79. virtual float get_total_linear_damp() const;
  80. virtual Vector3 get_center_of_mass() const;
  81. virtual Basis get_principal_inertia_axes() const;
  82. // get the mass
  83. virtual float get_inverse_mass() const;
  84. // get density of this body space
  85. virtual Vector3 get_inverse_inertia() const;
  86. // get density of this body space
  87. virtual Basis get_inverse_inertia_tensor() const;
  88. virtual void set_linear_velocity(const Vector3 &p_velocity);
  89. virtual Vector3 get_linear_velocity() const;
  90. virtual void set_angular_velocity(const Vector3 &p_velocity);
  91. virtual Vector3 get_angular_velocity() const;
  92. virtual void set_transform(const Transform &p_transform);
  93. virtual Transform get_transform() const;
  94. virtual Vector3 get_velocity_at_local_position(const Vector3 &p_position) const;
  95. virtual void add_central_force(const Vector3 &p_force);
  96. virtual void add_force(const Vector3 &p_force, const Vector3 &p_pos);
  97. virtual void add_torque(const Vector3 &p_torque);
  98. virtual void apply_central_impulse(const Vector3 &p_impulse);
  99. virtual void apply_impulse(const Vector3 &p_pos, const Vector3 &p_impulse);
  100. virtual void apply_torque_impulse(const Vector3 &p_impulse);
  101. virtual void set_sleep_state(bool p_enable);
  102. virtual bool is_sleeping() const;
  103. virtual int get_contact_count() const;
  104. virtual Vector3 get_contact_local_position(int p_contact_idx) const;
  105. virtual Vector3 get_contact_local_normal(int p_contact_idx) const;
  106. virtual float get_contact_impulse(int p_contact_idx) const;
  107. virtual int get_contact_local_shape(int p_contact_idx) const;
  108. virtual RID get_contact_collider(int p_contact_idx) const;
  109. virtual Vector3 get_contact_collider_position(int p_contact_idx) const;
  110. virtual ObjectID get_contact_collider_id(int p_contact_idx) const;
  111. virtual int get_contact_collider_shape(int p_contact_idx) const;
  112. virtual Vector3 get_contact_collider_velocity_at_position(int p_contact_idx) const;
  113. virtual real_t get_step() const { return deltaTime; }
  114. virtual void integrate_forces() {
  115. // Skip the execution of this function
  116. }
  117. virtual PhysicsDirectSpaceState *get_space_state();
  118. };
  119. class RigidBodyBullet : public RigidCollisionObjectBullet {
  120. public:
  121. struct CollisionData {
  122. RigidBodyBullet *otherObject;
  123. int other_object_shape;
  124. int local_shape;
  125. Vector3 hitLocalLocation;
  126. Vector3 hitWorldLocation;
  127. Vector3 hitNormal;
  128. float appliedImpulse;
  129. };
  130. struct ForceIntegrationCallback {
  131. ObjectID id;
  132. StringName method;
  133. Variant udata;
  134. };
  135. /// Used to hold shapes
  136. struct KinematicShape {
  137. class btConvexShape *shape;
  138. btTransform transform;
  139. KinematicShape() :
  140. shape(nullptr) {}
  141. bool is_active() const { return shape; }
  142. };
  143. struct KinematicUtilities {
  144. RigidBodyBullet *owner;
  145. btScalar safe_margin;
  146. Vector<KinematicShape> shapes;
  147. KinematicUtilities(RigidBodyBullet *p_owner);
  148. ~KinematicUtilities();
  149. void setSafeMargin(btScalar p_margin);
  150. /// Used to set the default shape to ghost
  151. void copyAllOwnerShapes();
  152. private:
  153. void just_delete_shapes(int new_size);
  154. };
  155. private:
  156. friend class BulletPhysicsDirectBodyState;
  157. // This is required only for Kinematic movement
  158. KinematicUtilities *kinematic_utilities;
  159. PhysicsServer::BodyMode mode;
  160. GodotMotionState *godotMotionState;
  161. btRigidBody *btBody;
  162. uint16_t locked_axis;
  163. real_t mass;
  164. real_t gravity_scale;
  165. real_t linearDamp;
  166. real_t angularDamp;
  167. bool can_sleep;
  168. bool omit_forces_integration;
  169. bool can_integrate_forces;
  170. Vector<CollisionData> collisions;
  171. Vector<RigidBodyBullet *> collision_traces_1;
  172. Vector<RigidBodyBullet *> collision_traces_2;
  173. Vector<RigidBodyBullet *> *prev_collision_traces;
  174. Vector<RigidBodyBullet *> *curr_collision_traces;
  175. // these parameters are used to avoid vector resize
  176. int maxCollisionsDetection;
  177. int collisionsCount;
  178. int prev_collision_count;
  179. Vector<AreaBullet *> areasWhereIam;
  180. // these parameters are used to avoid vector resize
  181. int maxAreasWhereIam;
  182. int areaWhereIamCount;
  183. // Used to know if the area is used as gravity point
  184. int countGravityPointSpaces;
  185. bool isScratchedSpaceOverrideModificator;
  186. bool previousActiveState; // Last check state
  187. ForceIntegrationCallback *force_integration_callback;
  188. public:
  189. RigidBodyBullet();
  190. ~RigidBodyBullet();
  191. void init_kinematic_utilities();
  192. void destroy_kinematic_utilities();
  193. _FORCE_INLINE_ KinematicUtilities *get_kinematic_utilities() const { return kinematic_utilities; }
  194. _FORCE_INLINE_ btRigidBody *get_bt_rigid_body() { return btBody; }
  195. virtual void main_shape_changed();
  196. virtual void reload_body();
  197. virtual void set_space(SpaceBullet *p_space);
  198. virtual void dispatch_callbacks();
  199. void set_force_integration_callback(ObjectID p_id, const StringName &p_method, const Variant &p_udata = Variant());
  200. void scratch_space_override_modificator();
  201. virtual void on_collision_filters_change();
  202. virtual void on_collision_checker_start();
  203. virtual void on_collision_checker_end();
  204. void set_max_collisions_detection(int p_maxCollisionsDetection) {
  205. ERR_FAIL_COND(0 > p_maxCollisionsDetection);
  206. maxCollisionsDetection = p_maxCollisionsDetection;
  207. collisions.resize(p_maxCollisionsDetection);
  208. collision_traces_1.resize(p_maxCollisionsDetection);
  209. collision_traces_2.resize(p_maxCollisionsDetection);
  210. collisionsCount = 0;
  211. prev_collision_count = MIN(prev_collision_count, p_maxCollisionsDetection);
  212. }
  213. int get_max_collisions_detection() {
  214. return maxCollisionsDetection;
  215. }
  216. bool can_add_collision() { return collisionsCount < maxCollisionsDetection; }
  217. 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);
  218. bool was_colliding(RigidBodyBullet *p_other_object);
  219. void set_activation_state(bool p_active);
  220. bool is_active() const;
  221. void set_omit_forces_integration(bool p_omit);
  222. _FORCE_INLINE_ bool get_omit_forces_integration() const { return omit_forces_integration; }
  223. void set_param(PhysicsServer::BodyParameter p_param, real_t);
  224. real_t get_param(PhysicsServer::BodyParameter p_param) const;
  225. void set_mode(PhysicsServer::BodyMode p_mode);
  226. PhysicsServer::BodyMode get_mode() const;
  227. void set_state(PhysicsServer::BodyState p_state, const Variant &p_variant);
  228. Variant get_state(PhysicsServer::BodyState p_state) const;
  229. void apply_impulse(const Vector3 &p_pos, const Vector3 &p_impulse);
  230. void apply_central_impulse(const Vector3 &p_impulse);
  231. void apply_torque_impulse(const Vector3 &p_impulse);
  232. void apply_force(const Vector3 &p_force, const Vector3 &p_pos);
  233. void apply_central_force(const Vector3 &p_force);
  234. void apply_torque(const Vector3 &p_torque);
  235. void set_applied_force(const Vector3 &p_force);
  236. Vector3 get_applied_force() const;
  237. void set_applied_torque(const Vector3 &p_torque);
  238. Vector3 get_applied_torque() const;
  239. void set_axis_lock(PhysicsServer::BodyAxis p_axis, bool lock);
  240. bool is_axis_locked(PhysicsServer::BodyAxis p_axis) const;
  241. void reload_axis_lock();
  242. /// Doc:
  243. /// https://web.archive.org/web/20180404091446/http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Anti_tunneling_by_Motion_Clamping
  244. void set_continuous_collision_detection(bool p_enable);
  245. bool is_continuous_collision_detection_enabled() const;
  246. void set_linear_velocity(const Vector3 &p_velocity);
  247. Vector3 get_linear_velocity() const;
  248. void set_angular_velocity(const Vector3 &p_velocity);
  249. Vector3 get_angular_velocity() const;
  250. virtual void set_transform__bullet(const btTransform &p_global_transform);
  251. virtual const btTransform &get_transform__bullet() const;
  252. virtual void reload_shapes();
  253. virtual void on_enter_area(AreaBullet *p_area);
  254. virtual void on_exit_area(AreaBullet *p_area);
  255. void reload_space_override_modificator();
  256. /// Kinematic
  257. void reload_kinematic_shapes();
  258. virtual void notify_transform_changed();
  259. private:
  260. void _internal_set_mass(real_t p_mass);
  261. };
  262. #endif