space_bullet.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**************************************************************************/
  2. /* space_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 SPACE_BULLET_H
  31. #define SPACE_BULLET_H
  32. #include "core/variant.h"
  33. #include "core/vector.h"
  34. #include "godot_result_callbacks.h"
  35. #include "rid_bullet.h"
  36. #include "servers/physics_server.h"
  37. #include <BulletCollision/BroadphaseCollision/btBroadphaseProxy.h>
  38. #include <BulletCollision/BroadphaseCollision/btOverlappingPairCache.h>
  39. #include <LinearMath/btScalar.h>
  40. #include <LinearMath/btTransform.h>
  41. #include <LinearMath/btVector3.h>
  42. /**
  43. @author AndreaCatania
  44. */
  45. class AreaBullet;
  46. class btBroadphaseInterface;
  47. class btCollisionDispatcher;
  48. class btConstraintSolver;
  49. class btDefaultCollisionConfiguration;
  50. class btDynamicsWorld;
  51. class btDiscreteDynamicsWorld;
  52. class btEmptyShape;
  53. class btGhostPairCallback;
  54. class btSoftRigidDynamicsWorld;
  55. struct btSoftBodyWorldInfo;
  56. class ConstraintBullet;
  57. class CollisionObjectBullet;
  58. class RigidBodyBullet;
  59. class SpaceBullet;
  60. class SoftBodyBullet;
  61. class btGjkEpaPenetrationDepthSolver;
  62. extern ContactAddedCallback gContactAddedCallback;
  63. class BulletPhysicsDirectSpaceState : public PhysicsDirectSpaceState {
  64. GDCLASS(BulletPhysicsDirectSpaceState, PhysicsDirectSpaceState);
  65. private:
  66. SpaceBullet *space;
  67. public:
  68. BulletPhysicsDirectSpaceState(SpaceBullet *p_space);
  69. virtual int intersect_point(const Vector3 &p_point, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
  70. virtual bool intersect_ray(const Vector3 &p_from, const Vector3 &p_to, RayResult &r_result, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false, bool p_pick_ray = false);
  71. virtual int intersect_shape(const RID &p_shape, const Transform &p_xform, float p_margin, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
  72. virtual bool cast_motion(const RID &p_shape, const Transform &p_xform, const Vector3 &p_motion, float p_margin, float &r_closest_safe, float &r_closest_unsafe, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false, ShapeRestInfo *r_info = nullptr);
  73. /// Returns the list of contacts pairs in this order: Local contact, other body contact
  74. virtual bool collide_shape(RID p_shape, const Transform &p_shape_xform, float p_margin, Vector3 *r_results, int p_result_max, int &r_result_count, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
  75. virtual bool rest_info(RID p_shape, const Transform &p_shape_xform, float p_margin, ShapeRestInfo *r_info, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
  76. virtual Vector3 get_closest_point_to_object_volume(RID p_object, const Vector3 p_point) const;
  77. };
  78. class SpaceBullet : public RIDBullet {
  79. friend class AreaBullet;
  80. friend void onBulletTickCallback(btDynamicsWorld *world, btScalar timeStep);
  81. friend class BulletPhysicsDirectSpaceState;
  82. public:
  83. /// IMPORTANT NOTE: These members are public to expose some bullet
  84. /// physics internal functionalities that are not supported or exposed
  85. /// through the `PhysicsServer`.
  86. ///
  87. /// One example is the unofficial module `godot-kinematic-body`:
  88. /// https://github.com/AndreaCatania/godot-kinematic-body/blob/3.x/kinematic_object_3d.cpp
  89. ///
  90. /// # How it works
  91. /// To access these members from a module, the first step is to cast the
  92. /// `PhysicsServer` to `BulletPhysicsServer`. This step is necessary
  93. /// to have access to the `space_owner` (which is a storage that holds
  94. /// the `SpaceBullet` objects).
  95. /// ```c++
  96. /// BulletPhysicsServer* bullet_physics_singleton = static_cast<BulletPhysicsServer *>(PhysicsServer::get_singleton());
  97. /// SpaceBullet* space = bullet_physics_singleton->get_space_owner()->getornull(get_world()->get_space());
  98. /// ```
  99. //
  100. /// Once you have access to the space, you can access the members and
  101. /// execute bullet physics commands:
  102. /// ```c++
  103. /// space->dynamicsWorld->convexSweepTest(
  104. /// p_shape,
  105. /// btTransform(btMatrix3x3::getIdentity(), p_position),
  106. /// btTransform(btMatrix3x3::getIdentity(), p_position + p_motion),
  107. /// result,
  108. /// p_margin);
  109. /// ```
  110. btBroadphaseInterface *broadphase;
  111. btDefaultCollisionConfiguration *collisionConfiguration;
  112. btCollisionDispatcher *dispatcher;
  113. btConstraintSolver *solver;
  114. btDiscreteDynamicsWorld *dynamicsWorld;
  115. btSoftBodyWorldInfo *soft_body_world_info;
  116. btGhostPairCallback *ghostPairCallback;
  117. GodotFilterCallback *godotFilterCallback;
  118. btGjkEpaPenetrationDepthSolver *gjk_epa_pen_solver;
  119. btVoronoiSimplexSolver *gjk_simplex_solver;
  120. BulletPhysicsDirectSpaceState *direct_access;
  121. Vector3 gravityDirection;
  122. real_t gravityMagnitude;
  123. real_t linear_damp;
  124. real_t angular_damp;
  125. Vector<AreaBullet *> areas;
  126. Vector<Vector3> contactDebug;
  127. int contactDebugCount;
  128. real_t delta_time;
  129. public:
  130. SpaceBullet();
  131. virtual ~SpaceBullet();
  132. void flush_queries();
  133. real_t get_delta_time() { return delta_time; }
  134. void step(real_t p_delta_time);
  135. _FORCE_INLINE_ btBroadphaseInterface *get_broadphase() { return broadphase; }
  136. _FORCE_INLINE_ btCollisionDispatcher *get_dispatcher() { return dispatcher; }
  137. _FORCE_INLINE_ btSoftBodyWorldInfo *get_soft_body_world_info() { return soft_body_world_info; }
  138. _FORCE_INLINE_ bool is_using_soft_world() { return soft_body_world_info; }
  139. /// Used to set some parameters to Bullet world
  140. /// @param p_param:
  141. /// AREA_PARAM_GRAVITY to set the gravity magnitude of entire world
  142. /// AREA_PARAM_GRAVITY_VECTOR to set the gravity direction of entire world
  143. void set_param(PhysicsServer::AreaParameter p_param, const Variant &p_value);
  144. /// Used to get some parameters to Bullet world
  145. /// @param p_param:
  146. /// AREA_PARAM_GRAVITY to get the gravity magnitude of entire world
  147. /// AREA_PARAM_GRAVITY_VECTOR to get the gravity direction of entire world
  148. Variant get_param(PhysicsServer::AreaParameter p_param);
  149. void set_param(PhysicsServer::SpaceParameter p_param, real_t p_value);
  150. real_t get_param(PhysicsServer::SpaceParameter p_param);
  151. void add_area(AreaBullet *p_area);
  152. void remove_area(AreaBullet *p_area);
  153. void reload_collision_filters(AreaBullet *p_area);
  154. void add_rigid_body(RigidBodyBullet *p_body);
  155. void remove_rigid_body_constraints(RigidBodyBullet *p_body);
  156. void remove_rigid_body(RigidBodyBullet *p_body);
  157. void reload_collision_filters(RigidBodyBullet *p_body);
  158. void add_soft_body(SoftBodyBullet *p_body);
  159. void remove_soft_body(SoftBodyBullet *p_body);
  160. void reload_collision_filters(SoftBodyBullet *p_body);
  161. void add_constraint(ConstraintBullet *p_constraint, bool disableCollisionsBetweenLinkedBodies = false);
  162. void remove_constraint(ConstraintBullet *p_constraint);
  163. int get_num_collision_objects() const;
  164. void remove_all_collision_objects();
  165. BulletPhysicsDirectSpaceState *get_direct_state();
  166. void set_debug_contacts(int p_amount) { contactDebug.resize(p_amount); }
  167. _FORCE_INLINE_ bool is_debugging_contacts() const { return !contactDebug.empty(); }
  168. _FORCE_INLINE_ void reset_debug_contact_count() {
  169. contactDebugCount = 0;
  170. }
  171. _FORCE_INLINE_ void add_debug_contact(const Vector3 &p_contact) {
  172. if (contactDebugCount < contactDebug.size()) {
  173. contactDebug.write[contactDebugCount++] = p_contact;
  174. }
  175. }
  176. _FORCE_INLINE_ Vector<Vector3> get_debug_contacts() { return contactDebug; }
  177. _FORCE_INLINE_ int get_debug_contact_count() { return contactDebugCount; }
  178. const Vector3 &get_gravity_direction() const { return gravityDirection; }
  179. real_t get_gravity_magnitude() const { return gravityMagnitude; }
  180. void update_gravity();
  181. real_t get_linear_damp() const { return linear_damp; }
  182. real_t get_angular_damp() const { return angular_damp; }
  183. bool test_body_motion(RigidBodyBullet *p_body, const Transform &p_from, const Vector3 &p_motion, bool p_infinite_inertia, PhysicsServer::MotionResult *r_result, bool p_exclude_raycast_shapes, const Set<RID> &p_exclude = Set<RID>());
  184. int test_ray_separation(RigidBodyBullet *p_body, const Transform &p_transform, bool p_infinite_inertia, Vector3 &r_recover_motion, PhysicsServer::SeparationResult *r_results, int p_result_max, float p_margin);
  185. private:
  186. void create_empty_world(bool p_create_soft_world);
  187. void destroy_world();
  188. void check_ghost_overlaps();
  189. void check_body_collision();
  190. public:
  191. struct RecoverResult {
  192. bool hasPenetration;
  193. btVector3 normal;
  194. btVector3 pointWorld;
  195. btScalar penetration_distance; // Negative mean penetration
  196. int other_compound_shape_index;
  197. const btCollisionObject *other_collision_object;
  198. int local_shape_most_recovered;
  199. RecoverResult() :
  200. hasPenetration(false),
  201. normal(0, 0, 0),
  202. pointWorld(0, 0, 0),
  203. penetration_distance(1e20),
  204. other_compound_shape_index(0),
  205. other_collision_object(nullptr),
  206. local_shape_most_recovered(0) {}
  207. };
  208. private:
  209. bool recover_from_penetration(RigidBodyBullet *p_body, const btTransform &p_body_position, btScalar p_recover_movement_scale, bool p_infinite_inertia, btVector3 &r_delta_recover_movement, RecoverResult *r_recover_result = nullptr, const Set<RID> &p_exclude = Set<RID>());
  210. /// This is an API that recover a kinematic object from penetration
  211. /// This allow only Convex Convex test and it always use GJK algorithm, With this API we don't benefit of Bullet special accelerated functions
  212. bool RFP_convex_convex_test(const btConvexShape *p_shapeA, const btConvexShape *p_shapeB, btCollisionObject *p_objectB, int p_shapeId_A, int p_shapeId_B, const btTransform &p_transformA, const btTransform &p_transformB, btScalar p_recover_movement_scale, btVector3 &r_delta_recover_movement, RecoverResult *r_recover_result = nullptr);
  213. /// This is an API that recover a kinematic object from penetration
  214. /// Using this we leave Bullet to select the best algorithm, For example GJK in case we have Convex Convex, or a Bullet accelerated algorithm
  215. bool RFP_convex_world_test(const btConvexShape *p_shapeA, const btCollisionShape *p_shapeB, btCollisionObject *p_objectA, btCollisionObject *p_objectB, int p_shapeId_A, int p_shapeId_B, const btTransform &p_transformA, const btTransform &p_transformB, btScalar p_recover_movement_scale, btVector3 &r_delta_recover_movement, RecoverResult *r_recover_result = nullptr);
  216. int add_separation_result(PhysicsServer::SeparationResult *r_results, const SpaceBullet::RecoverResult &p_recover_result, int p_shape_id, const btCollisionObject *p_other_object) const;
  217. int recover_from_penetration_ray(RigidBodyBullet *p_body, const btTransform &p_body_position, btScalar p_recover_movement_scale, bool p_infinite_inertia, int p_result_max, btVector3 &r_delta_recover_movement, PhysicsServer::SeparationResult *r_results);
  218. };
  219. #endif // SPACE_BULLET_H