bullet_physics_server.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /**************************************************************************/
  2. /* bullet_physics_server.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 BULLET_PHYSICS_SERVER_H
  31. #define BULLET_PHYSICS_SERVER_H
  32. #include "area_bullet.h"
  33. #include "core/rid.h"
  34. #include "joint_bullet.h"
  35. #include "rigid_body_bullet.h"
  36. #include "servers/physics_server.h"
  37. #include "shape_bullet.h"
  38. #include "soft_body_bullet.h"
  39. #include "space_bullet.h"
  40. /**
  41. @author AndreaCatania
  42. */
  43. class BulletPhysicsServer : public PhysicsServer {
  44. GDCLASS(BulletPhysicsServer, PhysicsServer);
  45. friend class BulletPhysicsDirectSpaceState;
  46. bool active;
  47. char active_spaces_count;
  48. Vector<SpaceBullet *> active_spaces;
  49. mutable RID_Owner<SpaceBullet> space_owner;
  50. mutable RID_Owner<ShapeBullet> shape_owner;
  51. mutable RID_Owner<AreaBullet> area_owner;
  52. mutable RID_Owner<RigidBodyBullet> rigid_body_owner;
  53. mutable RID_Owner<SoftBodyBullet> soft_body_owner;
  54. mutable RID_Owner<JointBullet> joint_owner;
  55. protected:
  56. static void _bind_methods();
  57. public:
  58. BulletPhysicsServer();
  59. ~BulletPhysicsServer();
  60. _FORCE_INLINE_ RID_Owner<SpaceBullet> *get_space_owner() {
  61. return &space_owner;
  62. }
  63. _FORCE_INLINE_ RID_Owner<ShapeBullet> *get_shape_owner() {
  64. return &shape_owner;
  65. }
  66. _FORCE_INLINE_ RID_Owner<AreaBullet> *get_area_owner() {
  67. return &area_owner;
  68. }
  69. _FORCE_INLINE_ RID_Owner<RigidBodyBullet> *get_rigid_body_owner() {
  70. return &rigid_body_owner;
  71. }
  72. _FORCE_INLINE_ RID_Owner<SoftBodyBullet> *get_soft_body_owner() {
  73. return &soft_body_owner;
  74. }
  75. _FORCE_INLINE_ RID_Owner<JointBullet> *get_joint_owner() {
  76. return &joint_owner;
  77. }
  78. /* SHAPE API */
  79. virtual RID shape_create(ShapeType p_shape);
  80. virtual void shape_set_data(RID p_shape, const Variant &p_data);
  81. virtual ShapeType shape_get_type(RID p_shape) const;
  82. virtual Variant shape_get_data(RID p_shape) const;
  83. virtual void shape_set_margin(RID p_shape, real_t p_margin);
  84. virtual real_t shape_get_margin(RID p_shape) const;
  85. /// Not supported
  86. virtual void shape_set_custom_solver_bias(RID p_shape, real_t p_bias);
  87. /// Not supported
  88. virtual real_t shape_get_custom_solver_bias(RID p_shape) const;
  89. /* SPACE API */
  90. virtual RID space_create();
  91. virtual void space_set_active(RID p_space, bool p_active);
  92. virtual bool space_is_active(RID p_space) const;
  93. /// Not supported
  94. virtual void space_set_param(RID p_space, SpaceParameter p_param, real_t p_value);
  95. /// Not supported
  96. virtual real_t space_get_param(RID p_space, SpaceParameter p_param) const;
  97. virtual PhysicsDirectSpaceState *space_get_direct_state(RID p_space);
  98. virtual void space_set_debug_contacts(RID p_space, int p_max_contacts);
  99. virtual Vector<Vector3> space_get_contacts(RID p_space) const;
  100. virtual int space_get_contact_count(RID p_space) const;
  101. /* AREA API */
  102. /// Bullet Physics Engine not support "Area", this must be handled by the game developer in another way.
  103. /// Since godot Physics use the concept of area even to define the main world, the API area_set_param is used to set initial physics world information.
  104. /// The API area_set_param is a bit hacky, and allow Godot to set some parameters on Bullet's world, a different use print a warning to console.
  105. /// All other APIs returns a warning message if used
  106. virtual RID area_create();
  107. virtual void area_set_space(RID p_area, RID p_space);
  108. virtual RID area_get_space(RID p_area) const;
  109. virtual void area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode);
  110. virtual AreaSpaceOverrideMode area_get_space_override_mode(RID p_area) const;
  111. virtual void area_add_shape(RID p_area, RID p_shape, const Transform &p_transform = Transform(), bool p_disabled = false);
  112. virtual void area_set_shape(RID p_area, int p_shape_idx, RID p_shape);
  113. virtual void area_set_shape_transform(RID p_area, int p_shape_idx, const Transform &p_transform);
  114. virtual int area_get_shape_count(RID p_area) const;
  115. virtual RID area_get_shape(RID p_area, int p_shape_idx) const;
  116. virtual Transform area_get_shape_transform(RID p_area, int p_shape_idx) const;
  117. virtual void area_remove_shape(RID p_area, int p_shape_idx);
  118. virtual void area_clear_shapes(RID p_area);
  119. virtual void area_set_shape_disabled(RID p_area, int p_shape_idx, bool p_disabled);
  120. virtual void area_attach_object_instance_id(RID p_area, ObjectID p_id);
  121. virtual ObjectID area_get_object_instance_id(RID p_area) const;
  122. /// If you pass as p_area the SpaceBullet you can set some parameters as specified below
  123. /// AREA_PARAM_GRAVITY
  124. /// AREA_PARAM_GRAVITY_VECTOR
  125. /// Otherwise you can set area parameters
  126. virtual void area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value);
  127. virtual Variant area_get_param(RID p_area, AreaParameter p_param) const;
  128. virtual void area_set_transform(RID p_area, const Transform &p_transform);
  129. virtual Transform area_get_transform(RID p_area) const;
  130. virtual void area_set_collision_mask(RID p_area, uint32_t p_mask);
  131. virtual void area_set_collision_layer(RID p_area, uint32_t p_layer);
  132. virtual void area_set_monitorable(RID p_area, bool p_monitorable);
  133. virtual void area_set_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method);
  134. virtual void area_set_area_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method);
  135. virtual void area_set_ray_pickable(RID p_area, bool p_enable);
  136. virtual bool area_is_ray_pickable(RID p_area) const;
  137. /* RIGID BODY API */
  138. virtual RID body_create(BodyMode p_mode = BODY_MODE_RIGID, bool p_init_sleeping = false);
  139. virtual void body_set_space(RID p_body, RID p_space);
  140. virtual RID body_get_space(RID p_body) const;
  141. virtual void body_set_mode(RID p_body, BodyMode p_mode);
  142. virtual BodyMode body_get_mode(RID p_body) const;
  143. virtual void body_add_shape(RID p_body, RID p_shape, const Transform &p_transform = Transform(), bool p_disabled = false);
  144. // Not supported, Please remove and add new shape
  145. virtual void body_set_shape(RID p_body, int p_shape_idx, RID p_shape);
  146. virtual void body_set_shape_transform(RID p_body, int p_shape_idx, const Transform &p_transform);
  147. virtual int body_get_shape_count(RID p_body) const;
  148. virtual RID body_get_shape(RID p_body, int p_shape_idx) const;
  149. virtual Transform body_get_shape_transform(RID p_body, int p_shape_idx) const;
  150. virtual void body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled);
  151. virtual void body_remove_shape(RID p_body, int p_shape_idx);
  152. virtual void body_clear_shapes(RID p_body);
  153. // Used for Rigid and Soft Bodies
  154. virtual void body_attach_object_instance_id(RID p_body, uint32_t p_id);
  155. virtual uint32_t body_get_object_instance_id(RID p_body) const;
  156. virtual void body_set_enable_continuous_collision_detection(RID p_body, bool p_enable);
  157. virtual bool body_is_continuous_collision_detection_enabled(RID p_body) const;
  158. virtual void body_set_collision_layer(RID p_body, uint32_t p_layer);
  159. virtual uint32_t body_get_collision_layer(RID p_body) const;
  160. virtual void body_set_collision_mask(RID p_body, uint32_t p_mask);
  161. virtual uint32_t body_get_collision_mask(RID p_body) const;
  162. /// This is not supported by physics server
  163. virtual void body_set_user_flags(RID p_body, uint32_t p_flags);
  164. /// This is not supported by physics server
  165. virtual uint32_t body_get_user_flags(RID p_body) const;
  166. virtual void body_set_param(RID p_body, BodyParameter p_param, float p_value);
  167. virtual float body_get_param(RID p_body, BodyParameter p_param) const;
  168. virtual void body_set_kinematic_safe_margin(RID p_body, real_t p_margin);
  169. virtual real_t body_get_kinematic_safe_margin(RID p_body) const;
  170. virtual void body_set_state(RID p_body, BodyState p_state, const Variant &p_variant);
  171. virtual Variant body_get_state(RID p_body, BodyState p_state) const;
  172. virtual void body_set_applied_force(RID p_body, const Vector3 &p_force);
  173. virtual Vector3 body_get_applied_force(RID p_body) const;
  174. virtual void body_set_applied_torque(RID p_body, const Vector3 &p_torque);
  175. virtual Vector3 body_get_applied_torque(RID p_body) const;
  176. virtual void body_add_central_force(RID p_body, const Vector3 &p_force);
  177. virtual void body_add_force(RID p_body, const Vector3 &p_force, const Vector3 &p_pos);
  178. virtual void body_add_torque(RID p_body, const Vector3 &p_torque);
  179. virtual void body_apply_central_impulse(RID p_body, const Vector3 &p_impulse);
  180. virtual void body_apply_impulse(RID p_body, const Vector3 &p_pos, const Vector3 &p_impulse);
  181. virtual void body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse);
  182. virtual void body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity);
  183. virtual void body_set_axis_lock(RID p_body, BodyAxis p_axis, bool p_lock);
  184. virtual bool body_is_axis_locked(RID p_body, BodyAxis p_axis) const;
  185. virtual void body_add_collision_exception(RID p_body, RID p_body_b);
  186. virtual void body_remove_collision_exception(RID p_body, RID p_body_b);
  187. virtual void body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions);
  188. virtual void body_set_max_contacts_reported(RID p_body, int p_contacts);
  189. virtual int body_get_max_contacts_reported(RID p_body) const;
  190. virtual void body_set_contacts_reported_depth_threshold(RID p_body, float p_threshold);
  191. virtual float body_get_contacts_reported_depth_threshold(RID p_body) const;
  192. virtual void body_set_omit_force_integration(RID p_body, bool p_omit);
  193. virtual bool body_is_omitting_force_integration(RID p_body) const;
  194. virtual void body_set_force_integration_callback(RID p_body, Object *p_receiver, const StringName &p_method, const Variant &p_udata = Variant());
  195. virtual void body_set_ray_pickable(RID p_body, bool p_enable);
  196. virtual bool body_is_ray_pickable(RID p_body) const;
  197. // this function only works on physics process, errors and returns null otherwise
  198. virtual PhysicsDirectBodyState *body_get_direct_state(RID p_body);
  199. virtual bool body_test_motion(RID p_body, const Transform &p_from, const Vector3 &p_motion, bool p_infinite_inertia, MotionResult *r_result = nullptr, bool p_exclude_raycast_shapes = true, const Set<RID> &p_exclude = Set<RID>());
  200. virtual int body_test_ray_separation(RID p_body, const Transform &p_transform, bool p_infinite_inertia, Vector3 &r_recover_motion, SeparationResult *r_results, int p_result_max, float p_margin = 0.001);
  201. /* SOFT BODY API */
  202. virtual RID soft_body_create(bool p_init_sleeping = false);
  203. virtual void soft_body_update_visual_server(RID p_body, class SoftBodyVisualServerHandler *p_visual_server_handler);
  204. virtual void soft_body_set_space(RID p_body, RID p_space);
  205. virtual RID soft_body_get_space(RID p_body) const;
  206. virtual void soft_body_set_mesh(RID p_body, const REF &p_mesh);
  207. virtual void soft_body_set_collision_layer(RID p_body, uint32_t p_layer);
  208. virtual uint32_t soft_body_get_collision_layer(RID p_body) const;
  209. virtual void soft_body_set_collision_mask(RID p_body, uint32_t p_mask);
  210. virtual uint32_t soft_body_get_collision_mask(RID p_body) const;
  211. virtual void soft_body_add_collision_exception(RID p_body, RID p_body_b);
  212. virtual void soft_body_remove_collision_exception(RID p_body, RID p_body_b);
  213. virtual void soft_body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions);
  214. virtual void soft_body_set_state(RID p_body, BodyState p_state, const Variant &p_variant);
  215. virtual Variant soft_body_get_state(RID p_body, BodyState p_state) const;
  216. /// Special function. This function has bad performance
  217. virtual void soft_body_set_transform(RID p_body, const Transform &p_transform);
  218. virtual Vector3 soft_body_get_vertex_position(RID p_body, int vertex_index) const;
  219. virtual void soft_body_set_ray_pickable(RID p_body, bool p_enable);
  220. virtual bool soft_body_is_ray_pickable(RID p_body) const;
  221. virtual void soft_body_set_simulation_precision(RID p_body, int p_simulation_precision);
  222. virtual int soft_body_get_simulation_precision(RID p_body);
  223. virtual void soft_body_set_total_mass(RID p_body, real_t p_total_mass);
  224. virtual real_t soft_body_get_total_mass(RID p_body);
  225. virtual void soft_body_set_linear_stiffness(RID p_body, real_t p_stiffness);
  226. virtual real_t soft_body_get_linear_stiffness(RID p_body);
  227. virtual void soft_body_set_areaAngular_stiffness(RID p_body, real_t p_stiffness);
  228. virtual real_t soft_body_get_areaAngular_stiffness(RID p_body);
  229. virtual void soft_body_set_volume_stiffness(RID p_body, real_t p_stiffness);
  230. virtual real_t soft_body_get_volume_stiffness(RID p_body);
  231. virtual void soft_body_set_pressure_coefficient(RID p_body, real_t p_pressure_coefficient);
  232. virtual real_t soft_body_get_pressure_coefficient(RID p_body);
  233. virtual void soft_body_set_pose_matching_coefficient(RID p_body, real_t p_pose_matching_coefficient);
  234. virtual real_t soft_body_get_pose_matching_coefficient(RID p_body);
  235. virtual void soft_body_set_damping_coefficient(RID p_body, real_t p_damping_coefficient);
  236. virtual real_t soft_body_get_damping_coefficient(RID p_body);
  237. virtual void soft_body_set_drag_coefficient(RID p_body, real_t p_drag_coefficient);
  238. virtual real_t soft_body_get_drag_coefficient(RID p_body);
  239. virtual void soft_body_move_point(RID p_body, int p_point_index, const Vector3 &p_global_position);
  240. virtual Vector3 soft_body_get_point_global_position(RID p_body, int p_point_index);
  241. virtual Vector3 soft_body_get_point_offset(RID p_body, int p_point_index) const;
  242. virtual void soft_body_remove_all_pinned_points(RID p_body);
  243. virtual void soft_body_pin_point(RID p_body, int p_point_index, bool p_pin);
  244. virtual bool soft_body_is_point_pinned(RID p_body, int p_point_index);
  245. /* JOINT API */
  246. virtual JointType joint_get_type(RID p_joint) const;
  247. virtual void joint_set_solver_priority(RID p_joint, int p_priority);
  248. virtual int joint_get_solver_priority(RID p_joint) const;
  249. virtual void joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable);
  250. virtual bool joint_is_disabled_collisions_between_bodies(RID p_joint) const;
  251. virtual RID joint_create_pin(RID p_body_A, const Vector3 &p_local_A, RID p_body_B, const Vector3 &p_local_B);
  252. virtual void pin_joint_set_param(RID p_joint, PinJointParam p_param, float p_value);
  253. virtual float pin_joint_get_param(RID p_joint, PinJointParam p_param) const;
  254. virtual void pin_joint_set_local_a(RID p_joint, const Vector3 &p_A);
  255. virtual Vector3 pin_joint_get_local_a(RID p_joint) const;
  256. virtual void pin_joint_set_local_b(RID p_joint, const Vector3 &p_B);
  257. virtual Vector3 pin_joint_get_local_b(RID p_joint) const;
  258. virtual RID joint_create_hinge(RID p_body_A, const Transform &p_hinge_A, RID p_body_B, const Transform &p_hinge_B);
  259. virtual RID joint_create_hinge_simple(RID p_body_A, const Vector3 &p_pivot_A, const Vector3 &p_axis_A, RID p_body_B, const Vector3 &p_pivot_B, const Vector3 &p_axis_B);
  260. virtual void hinge_joint_set_param(RID p_joint, HingeJointParam p_param, float p_value);
  261. virtual float hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const;
  262. virtual void hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_value);
  263. virtual bool hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const;
  264. /// Reference frame is A
  265. virtual RID joint_create_slider(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B);
  266. virtual void slider_joint_set_param(RID p_joint, SliderJointParam p_param, float p_value);
  267. virtual float slider_joint_get_param(RID p_joint, SliderJointParam p_param) const;
  268. /// Reference frame is A
  269. virtual RID joint_create_cone_twist(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B);
  270. virtual void cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, float p_value);
  271. virtual float cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const;
  272. /// Reference frame is A
  273. virtual RID joint_create_generic_6dof(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B);
  274. virtual void generic_6dof_joint_set_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param, float p_value);
  275. virtual float generic_6dof_joint_get_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param);
  276. virtual void generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag, bool p_enable);
  277. virtual bool generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag);
  278. /* MISC */
  279. virtual void free(RID p_rid);
  280. virtual void set_active(bool p_active) {
  281. active = p_active;
  282. }
  283. static bool singleton_isActive() {
  284. return static_cast<BulletPhysicsServer *>(get_singleton())->active;
  285. }
  286. bool isActive() {
  287. return active;
  288. }
  289. virtual void init();
  290. virtual void step(float p_deltaTime);
  291. virtual void flush_queries();
  292. virtual void finish();
  293. virtual bool is_flushing_queries() const { return false; }
  294. virtual void set_collision_iterations(int p_iterations);
  295. virtual int get_process_info(ProcessInfo p_info);
  296. CollisionObjectBullet *get_collision_object(RID p_object) const;
  297. RigidCollisionObjectBullet *get_rigid_collision_object(RID p_object) const;
  298. /// Internal APIs
  299. public:
  300. };
  301. #endif // BULLET_PHYSICS_SERVER_H