physics_server_sw.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /**************************************************************************/
  2. /* physics_server_sw.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 PHYSICS_SERVER_SW_H
  31. #define PHYSICS_SERVER_SW_H
  32. #include "joints_sw.h"
  33. #include "servers/physics_server.h"
  34. #include "shape_sw.h"
  35. #include "space_sw.h"
  36. #include "step_sw.h"
  37. class PhysicsServerSW : public PhysicsServer {
  38. GDCLASS(PhysicsServerSW, PhysicsServer);
  39. friend class PhysicsDirectSpaceStateSW;
  40. bool active;
  41. int iterations;
  42. int island_count;
  43. int active_objects;
  44. int collision_pairs;
  45. bool flushing_queries;
  46. StepSW *stepper;
  47. Set<const SpaceSW *> active_spaces;
  48. mutable RID_Owner<ShapeSW> shape_owner;
  49. mutable RID_Owner<SpaceSW> space_owner;
  50. mutable RID_Owner<AreaSW> area_owner;
  51. mutable RID_Owner<BodySW> body_owner;
  52. mutable RID_Owner<JointSW> joint_owner;
  53. //void _clear_query(QuerySW *p_query);
  54. friend class CollisionObjectSW;
  55. SelfList<CollisionObjectSW>::List pending_shape_update_list;
  56. void _update_shapes();
  57. public:
  58. static PhysicsServerSW *singleton;
  59. struct CollCbkData {
  60. int max;
  61. int amount;
  62. Vector3 *ptr;
  63. };
  64. static void _shape_col_cbk(const Vector3 &p_point_A, const Vector3 &p_point_B, void *p_userdata);
  65. virtual RID shape_create(ShapeType p_shape);
  66. virtual void shape_set_data(RID p_shape, const Variant &p_data);
  67. virtual void shape_set_custom_solver_bias(RID p_shape, real_t p_bias);
  68. virtual ShapeType shape_get_type(RID p_shape) const;
  69. virtual Variant shape_get_data(RID p_shape) const;
  70. virtual void shape_set_margin(RID p_shape, real_t p_margin);
  71. virtual real_t shape_get_margin(RID p_shape) const;
  72. virtual real_t shape_get_custom_solver_bias(RID p_shape) const;
  73. /* SPACE API */
  74. virtual RID space_create();
  75. virtual void space_set_active(RID p_space, bool p_active);
  76. virtual bool space_is_active(RID p_space) const;
  77. virtual void space_set_param(RID p_space, SpaceParameter p_param, real_t p_value);
  78. virtual real_t space_get_param(RID p_space, SpaceParameter p_param) const;
  79. // this function only works on physics process, errors and returns null otherwise
  80. virtual PhysicsDirectSpaceState *space_get_direct_state(RID p_space);
  81. virtual void space_set_debug_contacts(RID p_space, int p_max_contacts);
  82. virtual Vector<Vector3> space_get_contacts(RID p_space) const;
  83. virtual int space_get_contact_count(RID p_space) const;
  84. /* AREA API */
  85. virtual RID area_create();
  86. virtual void area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode);
  87. virtual AreaSpaceOverrideMode area_get_space_override_mode(RID p_area) const;
  88. virtual void area_set_space(RID p_area, RID p_space);
  89. virtual RID area_get_space(RID p_area) const;
  90. virtual void area_add_shape(RID p_area, RID p_shape, const Transform &p_transform = Transform(), bool p_disabled = false);
  91. virtual void area_set_shape(RID p_area, int p_shape_idx, RID p_shape);
  92. virtual void area_set_shape_transform(RID p_area, int p_shape_idx, const Transform &p_transform);
  93. virtual int area_get_shape_count(RID p_area) const;
  94. virtual RID area_get_shape(RID p_area, int p_shape_idx) const;
  95. virtual Transform area_get_shape_transform(RID p_area, int p_shape_idx) const;
  96. virtual void area_remove_shape(RID p_area, int p_shape_idx);
  97. virtual void area_clear_shapes(RID p_area);
  98. virtual void area_set_shape_disabled(RID p_area, int p_shape_idx, bool p_disabled);
  99. virtual void area_attach_object_instance_id(RID p_area, ObjectID p_id);
  100. virtual ObjectID area_get_object_instance_id(RID p_area) const;
  101. virtual void area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value);
  102. virtual void area_set_transform(RID p_area, const Transform &p_transform);
  103. virtual Variant area_get_param(RID p_area, AreaParameter p_param) const;
  104. virtual Transform area_get_transform(RID p_area) const;
  105. virtual void area_set_ray_pickable(RID p_area, bool p_enable);
  106. virtual bool area_is_ray_pickable(RID p_area) const;
  107. virtual void area_set_collision_mask(RID p_area, uint32_t p_mask);
  108. virtual void area_set_collision_layer(RID p_area, uint32_t p_layer);
  109. virtual void area_set_monitorable(RID p_area, bool p_monitorable);
  110. virtual void area_set_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method);
  111. virtual void area_set_area_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method);
  112. /* BODY API */
  113. // create a body of a given type
  114. virtual RID body_create(BodyMode p_mode = BODY_MODE_RIGID, bool p_init_sleeping = false);
  115. virtual void body_set_space(RID p_body, RID p_space);
  116. virtual RID body_get_space(RID p_body) const;
  117. virtual void body_set_mode(RID p_body, BodyMode p_mode);
  118. virtual BodyMode body_get_mode(RID p_body) const;
  119. virtual void body_add_shape(RID p_body, RID p_shape, const Transform &p_transform = Transform(), bool p_disabled = false);
  120. virtual void body_set_shape(RID p_body, int p_shape_idx, RID p_shape);
  121. virtual void body_set_shape_transform(RID p_body, int p_shape_idx, const Transform &p_transform);
  122. virtual int body_get_shape_count(RID p_body) const;
  123. virtual RID body_get_shape(RID p_body, int p_shape_idx) const;
  124. virtual Transform body_get_shape_transform(RID p_body, int p_shape_idx) const;
  125. virtual void body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled);
  126. virtual void body_remove_shape(RID p_body, int p_shape_idx);
  127. virtual void body_clear_shapes(RID p_body);
  128. virtual void body_attach_object_instance_id(RID p_body, uint32_t p_id);
  129. virtual uint32_t body_get_object_instance_id(RID p_body) const;
  130. virtual void body_set_enable_continuous_collision_detection(RID p_body, bool p_enable);
  131. virtual bool body_is_continuous_collision_detection_enabled(RID p_body) const;
  132. virtual void body_set_collision_layer(RID p_body, uint32_t p_layer);
  133. virtual uint32_t body_get_collision_layer(RID p_body) const;
  134. virtual void body_set_collision_mask(RID p_body, uint32_t p_mask);
  135. virtual uint32_t body_get_collision_mask(RID p_body) const;
  136. virtual void body_set_user_flags(RID p_body, uint32_t p_flags);
  137. virtual uint32_t body_get_user_flags(RID p_body) const;
  138. virtual void body_set_param(RID p_body, BodyParameter p_param, real_t p_value);
  139. virtual real_t body_get_param(RID p_body, BodyParameter p_param) const;
  140. virtual void body_set_kinematic_safe_margin(RID p_body, real_t p_margin);
  141. virtual real_t body_get_kinematic_safe_margin(RID p_body) const;
  142. virtual void body_set_state(RID p_body, BodyState p_state, const Variant &p_variant);
  143. virtual Variant body_get_state(RID p_body, BodyState p_state) const;
  144. virtual void body_set_applied_force(RID p_body, const Vector3 &p_force);
  145. virtual Vector3 body_get_applied_force(RID p_body) const;
  146. virtual void body_set_applied_torque(RID p_body, const Vector3 &p_torque);
  147. virtual Vector3 body_get_applied_torque(RID p_body) const;
  148. virtual void body_add_central_force(RID p_body, const Vector3 &p_force);
  149. virtual void body_add_force(RID p_body, const Vector3 &p_force, const Vector3 &p_pos);
  150. virtual void body_add_torque(RID p_body, const Vector3 &p_torque);
  151. virtual void body_apply_central_impulse(RID p_body, const Vector3 &p_impulse);
  152. virtual void body_apply_impulse(RID p_body, const Vector3 &p_pos, const Vector3 &p_impulse);
  153. virtual void body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse);
  154. virtual void body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity);
  155. virtual void body_set_axis_lock(RID p_body, BodyAxis p_axis, bool p_lock);
  156. virtual bool body_is_axis_locked(RID p_body, BodyAxis p_axis) const;
  157. virtual void body_add_collision_exception(RID p_body, RID p_body_b);
  158. virtual void body_remove_collision_exception(RID p_body, RID p_body_b);
  159. virtual void body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions);
  160. virtual void body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold);
  161. virtual real_t body_get_contacts_reported_depth_threshold(RID p_body) const;
  162. virtual void body_set_omit_force_integration(RID p_body, bool p_omit);
  163. virtual bool body_is_omitting_force_integration(RID p_body) const;
  164. virtual void body_set_max_contacts_reported(RID p_body, int p_contacts);
  165. virtual int body_get_max_contacts_reported(RID p_body) const;
  166. virtual void body_set_force_integration_callback(RID p_body, Object *p_receiver, const StringName &p_method, const Variant &p_udata = Variant());
  167. virtual void body_set_ray_pickable(RID p_body, bool p_enable);
  168. virtual bool body_is_ray_pickable(RID p_body) const;
  169. 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>());
  170. 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);
  171. // this function only works on physics process, errors and returns null otherwise
  172. virtual PhysicsDirectBodyState *body_get_direct_state(RID p_body);
  173. /* SOFT BODY */
  174. virtual RID soft_body_create(bool p_init_sleeping = false) { return RID(); }
  175. virtual void soft_body_update_visual_server(RID p_body, class SoftBodyVisualServerHandler *p_visual_server_handler) {}
  176. virtual void soft_body_set_space(RID p_body, RID p_space) {}
  177. virtual RID soft_body_get_space(RID p_body) const { return RID(); }
  178. virtual void soft_body_set_collision_layer(RID p_body, uint32_t p_layer) {}
  179. virtual uint32_t soft_body_get_collision_layer(RID p_body) const { return 0; }
  180. virtual void soft_body_set_collision_mask(RID p_body, uint32_t p_mask) {}
  181. virtual uint32_t soft_body_get_collision_mask(RID p_body) const { return 0; }
  182. virtual void soft_body_add_collision_exception(RID p_body, RID p_body_b) {}
  183. virtual void soft_body_remove_collision_exception(RID p_body, RID p_body_b) {}
  184. virtual void soft_body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {}
  185. virtual void soft_body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {}
  186. virtual Variant soft_body_get_state(RID p_body, BodyState p_state) const { return Variant(); }
  187. virtual void soft_body_set_transform(RID p_body, const Transform &p_transform) {}
  188. virtual Vector3 soft_body_get_vertex_position(RID p_body, int vertex_index) const { return Vector3(); }
  189. virtual void soft_body_set_ray_pickable(RID p_body, bool p_enable) {}
  190. virtual bool soft_body_is_ray_pickable(RID p_body) const { return false; }
  191. virtual void soft_body_set_simulation_precision(RID p_body, int p_simulation_precision) {}
  192. virtual int soft_body_get_simulation_precision(RID p_body) { return 0; }
  193. virtual void soft_body_set_total_mass(RID p_body, real_t p_total_mass) {}
  194. virtual real_t soft_body_get_total_mass(RID p_body) { return 0.; }
  195. virtual void soft_body_set_linear_stiffness(RID p_body, real_t p_stiffness) {}
  196. virtual real_t soft_body_get_linear_stiffness(RID p_body) { return 0.; }
  197. virtual void soft_body_set_areaAngular_stiffness(RID p_body, real_t p_stiffness) {}
  198. virtual real_t soft_body_get_areaAngular_stiffness(RID p_body) { return 0.; }
  199. virtual void soft_body_set_volume_stiffness(RID p_body, real_t p_stiffness) {}
  200. virtual real_t soft_body_get_volume_stiffness(RID p_body) { return 0.; }
  201. virtual void soft_body_set_pressure_coefficient(RID p_body, real_t p_pressure_coefficient) {}
  202. virtual real_t soft_body_get_pressure_coefficient(RID p_body) { return 0.; }
  203. virtual void soft_body_set_pose_matching_coefficient(RID p_body, real_t p_pose_matching_coefficient) {}
  204. virtual real_t soft_body_get_pose_matching_coefficient(RID p_body) { return 0.; }
  205. virtual void soft_body_set_damping_coefficient(RID p_body, real_t p_damping_coefficient) {}
  206. virtual real_t soft_body_get_damping_coefficient(RID p_body) { return 0.; }
  207. virtual void soft_body_set_drag_coefficient(RID p_body, real_t p_drag_coefficient) {}
  208. virtual real_t soft_body_get_drag_coefficient(RID p_body) { return 0.; }
  209. virtual void soft_body_set_mesh(RID p_body, const REF &p_mesh) {}
  210. virtual void soft_body_move_point(RID p_body, int p_point_index, const Vector3 &p_global_position) {}
  211. virtual Vector3 soft_body_get_point_global_position(RID p_body, int p_point_index) { return Vector3(); }
  212. virtual Vector3 soft_body_get_point_offset(RID p_body, int p_point_index) const { return Vector3(); }
  213. virtual void soft_body_remove_all_pinned_points(RID p_body) {}
  214. virtual void soft_body_pin_point(RID p_body, int p_point_index, bool p_pin) {}
  215. virtual bool soft_body_is_point_pinned(RID p_body, int p_point_index) { return false; }
  216. /* JOINT API */
  217. virtual RID joint_create_pin(RID p_body_A, const Vector3 &p_local_A, RID p_body_B, const Vector3 &p_local_B);
  218. virtual void pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value);
  219. virtual real_t pin_joint_get_param(RID p_joint, PinJointParam p_param) const;
  220. virtual void pin_joint_set_local_a(RID p_joint, const Vector3 &p_A);
  221. virtual Vector3 pin_joint_get_local_a(RID p_joint) const;
  222. virtual void pin_joint_set_local_b(RID p_joint, const Vector3 &p_B);
  223. virtual Vector3 pin_joint_get_local_b(RID p_joint) const;
  224. virtual RID joint_create_hinge(RID p_body_A, const Transform &p_frame_A, RID p_body_B, const Transform &p_frame_B);
  225. 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);
  226. virtual void hinge_joint_set_param(RID p_joint, HingeJointParam p_param, real_t p_value);
  227. virtual real_t hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const;
  228. virtual void hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_value);
  229. virtual bool hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const;
  230. 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); //reference frame is A
  231. virtual void slider_joint_set_param(RID p_joint, SliderJointParam p_param, real_t p_value);
  232. virtual real_t slider_joint_get_param(RID p_joint, SliderJointParam p_param) const;
  233. 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); //reference frame is A
  234. virtual void cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, real_t p_value);
  235. virtual real_t cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const;
  236. 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); //reference frame is A
  237. virtual void generic_6dof_joint_set_param(RID p_joint, Vector3::Axis, G6DOFJointAxisParam p_param, real_t p_value);
  238. virtual real_t generic_6dof_joint_get_param(RID p_joint, Vector3::Axis, G6DOFJointAxisParam p_param);
  239. virtual void generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis, G6DOFJointAxisFlag p_flag, bool p_enable);
  240. virtual bool generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis, G6DOFJointAxisFlag p_flag);
  241. virtual JointType joint_get_type(RID p_joint) const;
  242. virtual void joint_set_solver_priority(RID p_joint, int p_priority);
  243. virtual int joint_get_solver_priority(RID p_joint) const;
  244. virtual void joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable);
  245. virtual bool joint_is_disabled_collisions_between_bodies(RID p_joint) const;
  246. /* MISC */
  247. virtual void free(RID p_rid);
  248. virtual void set_active(bool p_active);
  249. virtual void init();
  250. virtual void step(real_t p_step);
  251. virtual void flush_queries();
  252. virtual void finish();
  253. virtual void set_collision_iterations(int p_iterations);
  254. virtual bool is_flushing_queries() const { return flushing_queries; }
  255. int get_process_info(ProcessInfo p_info);
  256. PhysicsServerSW();
  257. ~PhysicsServerSW();
  258. };
  259. #endif // PHYSICS_SERVER_SW_H