physics_server_sw.h 18 KB

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