rigid_body_bullet.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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-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 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 = NULL;
  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 void add_force(const Vector3 &p_force, const Vector3 &p_pos);
  95. virtual void apply_impulse(const Vector3 &p_pos, const Vector3 &p_j);
  96. virtual void apply_torque_impulse(const Vector3 &p_j);
  97. virtual void set_sleep_state(bool p_enable);
  98. virtual bool is_sleeping() const;
  99. virtual int get_contact_count() const;
  100. virtual Vector3 get_contact_local_position(int p_contact_idx) const;
  101. virtual Vector3 get_contact_local_normal(int p_contact_idx) const;
  102. virtual int get_contact_local_shape(int p_contact_idx) const;
  103. virtual RID get_contact_collider(int p_contact_idx) const;
  104. virtual Vector3 get_contact_collider_position(int p_contact_idx) const;
  105. virtual ObjectID get_contact_collider_id(int p_contact_idx) const;
  106. virtual int get_contact_collider_shape(int p_contact_idx) const;
  107. virtual Vector3 get_contact_collider_velocity_at_position(int p_contact_idx) const;
  108. virtual real_t get_step() const { return deltaTime; }
  109. virtual void integrate_forces() {
  110. // Skip the execution of this function
  111. }
  112. virtual PhysicsDirectSpaceState *get_space_state();
  113. };
  114. class RigidBodyBullet : public RigidCollisionObjectBullet {
  115. public:
  116. struct CollisionData {
  117. RigidBodyBullet *otherObject;
  118. int other_object_shape;
  119. int local_shape;
  120. Vector3 hitLocalLocation;
  121. Vector3 hitWorldLocation;
  122. Vector3 hitNormal;
  123. };
  124. struct ForceIntegrationCallback {
  125. ObjectID id;
  126. StringName method;
  127. Variant udata;
  128. };
  129. /// Used to hold shapes
  130. struct KinematicShape {
  131. class btConvexShape *shape;
  132. btTransform transform;
  133. KinematicShape() :
  134. shape(NULL) {}
  135. const bool is_active() const { return shape; }
  136. };
  137. struct KinematicUtilities {
  138. RigidBodyBullet *owner;
  139. btScalar safe_margin;
  140. Vector<KinematicShape> shapes;
  141. KinematicUtilities(RigidBodyBullet *p_owner);
  142. ~KinematicUtilities();
  143. void setSafeMargin(btScalar p_margin);
  144. /// Used to set the default shape to ghost
  145. void copyAllOwnerShapes();
  146. private:
  147. void just_delete_shapes(int new_size);
  148. };
  149. private:
  150. friend class BulletPhysicsDirectBodyState;
  151. // This is required only for Kinematic movement
  152. KinematicUtilities *kinematic_utilities;
  153. PhysicsServer::BodyMode mode;
  154. GodotMotionState *godotMotionState;
  155. btRigidBody *btBody;
  156. uint16_t locked_axis;
  157. real_t mass;
  158. real_t gravity_scale;
  159. real_t linearDamp;
  160. real_t angularDamp;
  161. bool can_sleep;
  162. Vector<CollisionData> collisions;
  163. // these parameters are used to avoid vector resize
  164. int maxCollisionsDetection;
  165. int collisionsCount;
  166. Vector<AreaBullet *> areasWhereIam;
  167. // these parameters are used to avoid vector resize
  168. int maxAreasWhereIam;
  169. int areaWhereIamCount;
  170. // Used to know if the area is used as gravity point
  171. int countGravityPointSpaces;
  172. bool isScratchedSpaceOverrideModificator;
  173. bool isTransformChanged;
  174. bool previousActiveState; // Last check state
  175. ForceIntegrationCallback *force_integration_callback;
  176. public:
  177. RigidBodyBullet();
  178. ~RigidBodyBullet();
  179. void init_kinematic_utilities();
  180. void destroy_kinematic_utilities();
  181. _FORCE_INLINE_ class KinematicUtilities *get_kinematic_utilities() const { return kinematic_utilities; }
  182. _FORCE_INLINE_ btRigidBody *get_bt_rigid_body() { return btBody; }
  183. virtual void reload_body();
  184. virtual void set_space(SpaceBullet *p_space);
  185. virtual void dispatch_callbacks();
  186. void set_force_integration_callback(ObjectID p_id, const StringName &p_method, const Variant &p_udata = Variant());
  187. void scratch();
  188. void scratch_space_override_modificator();
  189. virtual void on_collision_filters_change();
  190. virtual void on_collision_checker_start();
  191. void set_max_collisions_detection(int p_maxCollisionsDetection) {
  192. maxCollisionsDetection = p_maxCollisionsDetection;
  193. collisions.resize(p_maxCollisionsDetection);
  194. collisionsCount = 0;
  195. }
  196. int get_max_collisions_detection() {
  197. return maxCollisionsDetection;
  198. }
  199. bool can_add_collision() { return collisionsCount < maxCollisionsDetection; }
  200. bool add_collision_object(RigidBodyBullet *p_otherObject, const Vector3 &p_hitWorldLocation, const Vector3 &p_hitLocalLocation, const Vector3 &p_hitNormal, int p_other_shape_index, int p_local_shape_index);
  201. void assert_no_constraints();
  202. void set_activation_state(bool p_active);
  203. bool is_active() const;
  204. void set_param(PhysicsServer::BodyParameter p_param, real_t);
  205. real_t get_param(PhysicsServer::BodyParameter p_param) const;
  206. void set_mode(PhysicsServer::BodyMode p_mode);
  207. PhysicsServer::BodyMode get_mode() const;
  208. void set_state(PhysicsServer::BodyState p_state, const Variant &p_variant);
  209. Variant get_state(PhysicsServer::BodyState p_state) const;
  210. void apply_impulse(const Vector3 &p_pos, const Vector3 &p_impulse);
  211. void apply_central_impulse(const Vector3 &p_impulse);
  212. void apply_torque_impulse(const Vector3 &p_impulse);
  213. void apply_force(const Vector3 &p_force, const Vector3 &p_pos);
  214. void apply_central_force(const Vector3 &p_force);
  215. void apply_torque(const Vector3 &p_torque);
  216. void set_applied_force(const Vector3 &p_force);
  217. Vector3 get_applied_force() const;
  218. void set_applied_torque(const Vector3 &p_torque);
  219. Vector3 get_applied_torque() const;
  220. void set_axis_lock(PhysicsServer::BodyAxis p_axis, bool lock);
  221. bool is_axis_locked(PhysicsServer::BodyAxis p_axis) const;
  222. void reload_axis_lock();
  223. /// Doc:
  224. /// http://www.bulletphysics.org/mediawiki-1.5.8/index.php?title=Anti_tunneling_by_Motion_Clamping
  225. void set_continuous_collision_detection(bool p_enable);
  226. bool is_continuous_collision_detection_enabled() const;
  227. void set_linear_velocity(const Vector3 &p_velocity);
  228. Vector3 get_linear_velocity() const;
  229. void set_angular_velocity(const Vector3 &p_velocity);
  230. Vector3 get_angular_velocity() const;
  231. virtual void set_transform__bullet(const btTransform &p_global_transform);
  232. virtual const btTransform &get_transform__bullet() const;
  233. virtual void on_shapes_changed();
  234. virtual void on_enter_area(AreaBullet *p_area);
  235. virtual void on_exit_area(AreaBullet *p_area);
  236. void reload_space_override_modificator();
  237. /// Kinematic
  238. void reload_kinematic_shapes();
  239. private:
  240. void _internal_set_mass(real_t p_mass);
  241. };
  242. #endif