physics_2d_server_sw.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*************************************************************************/
  2. /* physics_2d_server_sw.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_2D_SERVER_SW
  31. #define PHYSICS_2D_SERVER_SW
  32. #include "joints_2d_sw.h"
  33. #include "servers/physics_2d_server.h"
  34. #include "shape_2d_sw.h"
  35. #include "space_2d_sw.h"
  36. #include "step_2d_sw.h"
  37. class Physics2DServerSW : public Physics2DServer {
  38. GDCLASS(Physics2DServerSW, Physics2DServer);
  39. friend class Physics2DDirectSpaceStateSW;
  40. friend class Physics2DDirectBodyStateSW;
  41. bool active;
  42. int iterations;
  43. bool doing_sync;
  44. real_t last_step;
  45. int island_count;
  46. int active_objects;
  47. int collision_pairs;
  48. bool using_threads;
  49. bool flushing_queries;
  50. Step2DSW *stepper;
  51. Set<const Space2DSW *> active_spaces;
  52. Physics2DDirectBodyStateSW *direct_state;
  53. mutable RID_Owner<Shape2DSW> shape_owner;
  54. mutable RID_Owner<Space2DSW> space_owner;
  55. mutable RID_Owner<Area2DSW> area_owner;
  56. mutable RID_Owner<Body2DSW> body_owner;
  57. mutable RID_Owner<Joint2DSW> joint_owner;
  58. static Physics2DServerSW *singletonsw;
  59. //void _clear_query(Query2DSW *p_query);
  60. friend class CollisionObject2DSW;
  61. SelfList<CollisionObject2DSW>::List pending_shape_update_list;
  62. void _update_shapes();
  63. RID _shape_create(ShapeType p_shape);
  64. public:
  65. struct CollCbkData {
  66. Vector2 valid_dir;
  67. real_t valid_depth;
  68. int max;
  69. int amount;
  70. int passed;
  71. int invalid_by_dir;
  72. Vector2 *ptr;
  73. };
  74. virtual RID line_shape_create();
  75. virtual RID ray_shape_create();
  76. virtual RID segment_shape_create();
  77. virtual RID circle_shape_create();
  78. virtual RID rectangle_shape_create();
  79. virtual RID capsule_shape_create();
  80. virtual RID convex_polygon_shape_create();
  81. virtual RID concave_polygon_shape_create();
  82. static void _shape_col_cbk(const Vector2 &p_point_A, const Vector2 &p_point_B, void *p_userdata);
  83. virtual void shape_set_data(RID p_shape, const Variant &p_data);
  84. virtual void shape_set_custom_solver_bias(RID p_shape, real_t p_bias);
  85. virtual ShapeType shape_get_type(RID p_shape) const;
  86. virtual Variant shape_get_data(RID p_shape) const;
  87. virtual real_t shape_get_custom_solver_bias(RID p_shape) const;
  88. virtual bool shape_collide(RID p_shape_A, const Transform2D &p_xform_A, const Vector2 &p_motion_A, RID p_shape_B, const Transform2D &p_xform_B, const Vector2 &p_motion_B, Vector2 *r_results, int p_result_max, int &r_result_count);
  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. virtual void space_set_param(RID p_space, SpaceParameter p_param, real_t p_value);
  94. virtual real_t space_get_param(RID p_space, SpaceParameter p_param) const;
  95. virtual void space_set_debug_contacts(RID p_space, int p_max_contacts);
  96. virtual Vector<Vector2> space_get_contacts(RID p_space) const;
  97. virtual int space_get_contact_count(RID p_space) const;
  98. // this function only works on physics process, errors and returns null otherwise
  99. virtual Physics2DDirectSpaceState *space_get_direct_state(RID p_space);
  100. /* AREA API */
  101. virtual RID area_create();
  102. virtual void area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode);
  103. virtual AreaSpaceOverrideMode area_get_space_override_mode(RID p_area) const;
  104. virtual void area_set_space(RID p_area, RID p_space);
  105. virtual RID area_get_space(RID p_area) const;
  106. virtual void area_add_shape(RID p_area, RID p_shape, const Transform2D &p_transform = Transform2D(), bool p_disabled = false);
  107. virtual void area_set_shape(RID p_area, int p_shape_idx, RID p_shape);
  108. virtual void area_set_shape_transform(RID p_area, int p_shape_idx, const Transform2D &p_transform);
  109. virtual int area_get_shape_count(RID p_area) const;
  110. virtual RID area_get_shape(RID p_area, int p_shape_idx) const;
  111. virtual Transform2D area_get_shape_transform(RID p_area, int p_shape_idx) const;
  112. virtual void area_set_shape_disabled(RID p_area, int p_shape, bool p_disabled);
  113. virtual void area_remove_shape(RID p_area, int p_shape_idx);
  114. virtual void area_clear_shapes(RID p_area);
  115. virtual void area_attach_object_instance_id(RID p_area, ObjectID p_id);
  116. virtual ObjectID area_get_object_instance_id(RID p_area) const;
  117. virtual void area_attach_canvas_instance_id(RID p_area, ObjectID p_id);
  118. virtual ObjectID area_get_canvas_instance_id(RID p_area) const;
  119. virtual void area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value);
  120. virtual void area_set_transform(RID p_area, const Transform2D &p_transform);
  121. virtual Variant area_get_param(RID p_area, AreaParameter p_param) const;
  122. virtual Transform2D area_get_transform(RID p_area) const;
  123. virtual void area_set_monitorable(RID p_area, bool p_monitorable);
  124. virtual void area_set_collision_mask(RID p_area, uint32_t p_mask);
  125. virtual void area_set_collision_layer(RID p_area, uint32_t p_layer);
  126. virtual void area_set_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method);
  127. virtual void area_set_area_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method);
  128. virtual void area_set_pickable(RID p_area, bool p_pickable);
  129. /* BODY API */
  130. // create a body of a given type
  131. virtual RID body_create();
  132. virtual void body_set_space(RID p_body, RID p_space);
  133. virtual RID body_get_space(RID p_body) const;
  134. virtual void body_set_mode(RID p_body, BodyMode p_mode);
  135. virtual BodyMode body_get_mode(RID p_body) const;
  136. virtual void body_add_shape(RID p_body, RID p_shape, const Transform2D &p_transform = Transform2D(), bool p_disabled = false);
  137. virtual void body_set_shape(RID p_body, int p_shape_idx, RID p_shape);
  138. virtual void body_set_shape_transform(RID p_body, int p_shape_idx, const Transform2D &p_transform);
  139. virtual void body_set_shape_metadata(RID p_body, int p_shape_idx, const Variant &p_metadata);
  140. virtual int body_get_shape_count(RID p_body) const;
  141. virtual RID body_get_shape(RID p_body, int p_shape_idx) const;
  142. virtual Transform2D body_get_shape_transform(RID p_body, int p_shape_idx) const;
  143. virtual Variant body_get_shape_metadata(RID p_body, int p_shape_idx) const;
  144. virtual void body_remove_shape(RID p_body, int p_shape_idx);
  145. virtual void body_clear_shapes(RID p_body);
  146. virtual void body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled);
  147. virtual void body_set_shape_as_one_way_collision(RID p_body, int p_shape_idx, bool p_enable, float p_margin);
  148. virtual void body_attach_object_instance_id(RID p_body, uint32_t p_id);
  149. virtual uint32_t body_get_object_instance_id(RID p_body) const;
  150. virtual void body_attach_canvas_instance_id(RID p_body, uint32_t p_id);
  151. virtual uint32_t body_get_canvas_instance_id(RID p_body) const;
  152. virtual void body_set_continuous_collision_detection_mode(RID p_body, CCDMode p_mode);
  153. virtual CCDMode body_get_continuous_collision_detection_mode(RID p_body) const;
  154. virtual void body_set_collision_layer(RID p_body, uint32_t p_layer);
  155. virtual uint32_t body_get_collision_layer(RID p_body) const;
  156. virtual void body_set_collision_mask(RID p_body, uint32_t p_mask);
  157. virtual uint32_t body_get_collision_mask(RID p_body) const;
  158. virtual void body_set_param(RID p_body, BodyParameter p_param, real_t p_value);
  159. virtual real_t body_get_param(RID p_body, BodyParameter p_param) const;
  160. virtual void body_set_state(RID p_body, BodyState p_state, const Variant &p_variant);
  161. virtual Variant body_get_state(RID p_body, BodyState p_state) const;
  162. virtual void body_set_applied_force(RID p_body, const Vector2 &p_force);
  163. virtual Vector2 body_get_applied_force(RID p_body) const;
  164. virtual void body_set_applied_torque(RID p_body, real_t p_torque);
  165. virtual real_t body_get_applied_torque(RID p_body) const;
  166. virtual void body_add_central_force(RID p_body, const Vector2 &p_force);
  167. virtual void body_add_force(RID p_body, const Vector2 &p_offset, const Vector2 &p_force);
  168. virtual void body_add_torque(RID p_body, real_t p_torque);
  169. virtual void body_apply_central_impulse(RID p_body, const Vector2 &p_impulse);
  170. virtual void body_apply_torque_impulse(RID p_body, real_t p_torque);
  171. virtual void body_apply_impulse(RID p_body, const Vector2 &p_pos, const Vector2 &p_impulse);
  172. virtual void body_set_axis_velocity(RID p_body, const Vector2 &p_axis_velocity);
  173. virtual void body_add_collision_exception(RID p_body, RID p_body_b);
  174. virtual void body_remove_collision_exception(RID p_body, RID p_body_b);
  175. virtual void body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions);
  176. virtual void body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold);
  177. virtual real_t body_get_contacts_reported_depth_threshold(RID p_body) const;
  178. virtual void body_set_omit_force_integration(RID p_body, bool p_omit);
  179. virtual bool body_is_omitting_force_integration(RID p_body) const;
  180. virtual void body_set_max_contacts_reported(RID p_body, int p_contacts);
  181. virtual int body_get_max_contacts_reported(RID p_body) const;
  182. virtual void body_set_force_integration_callback(RID p_body, Object *p_receiver, const StringName &p_method, const Variant &p_udata = Variant());
  183. virtual bool body_collide_shape(RID p_body, int p_body_shape, RID p_shape, const Transform2D &p_shape_xform, const Vector2 &p_motion, Vector2 *r_results, int p_result_max, int &r_result_count);
  184. virtual void body_set_pickable(RID p_body, bool p_pickable);
  185. virtual bool body_test_motion(RID p_body, const Transform2D &p_from, const Vector2 &p_motion, bool p_infinite_inertia, real_t p_margin = 0.001, MotionResult *r_result = NULL, bool p_exclude_raycast_shapes = true);
  186. virtual int body_test_ray_separation(RID p_body, const Transform2D &p_transform, bool p_infinite_inertia, Vector2 &r_recover_motion, SeparationResult *r_results, int p_result_max, float p_margin = 0.001);
  187. // this function only works on physics process, errors and returns null otherwise
  188. virtual Physics2DDirectBodyState *body_get_direct_state(RID p_body);
  189. /* JOINT API */
  190. virtual void joint_set_param(RID p_joint, JointParam p_param, real_t p_value);
  191. virtual real_t joint_get_param(RID p_joint, JointParam p_param) const;
  192. virtual void joint_disable_collisions_between_bodies(RID p_joint, const bool p_disabled);
  193. virtual bool joint_is_disabled_collisions_between_bodies(RID p_joint) const;
  194. virtual RID pin_joint_create(const Vector2 &p_pos, RID p_body_a, RID p_body_b = RID());
  195. virtual RID groove_joint_create(const Vector2 &p_a_groove1, const Vector2 &p_a_groove2, const Vector2 &p_b_anchor, RID p_body_a, RID p_body_b);
  196. virtual RID damped_spring_joint_create(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, RID p_body_a, RID p_body_b = RID());
  197. virtual void pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value);
  198. virtual real_t pin_joint_get_param(RID p_joint, PinJointParam p_param) const;
  199. virtual void damped_string_joint_set_param(RID p_joint, DampedStringParam p_param, real_t p_value);
  200. virtual real_t damped_string_joint_get_param(RID p_joint, DampedStringParam p_param) const;
  201. virtual JointType joint_get_type(RID p_joint) const;
  202. /* MISC */
  203. virtual void free(RID p_rid);
  204. virtual void set_active(bool p_active);
  205. virtual void init();
  206. virtual void step(real_t p_step);
  207. virtual void sync();
  208. virtual void flush_queries();
  209. virtual void end_sync();
  210. virtual void finish();
  211. virtual bool is_flushing_queries() const { return flushing_queries; }
  212. int get_process_info(ProcessInfo p_info);
  213. Physics2DServerSW();
  214. ~Physics2DServerSW();
  215. };
  216. #endif