physics_2d_server.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /**************************************************************************/
  2. /* physics_2d_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 PHYSICS_2D_SERVER_H
  31. #define PHYSICS_2D_SERVER_H
  32. #include "core/object.h"
  33. #include "core/reference.h"
  34. #include "core/resource.h"
  35. class Physics2DDirectSpaceState;
  36. class Physics2DDirectBodyState : public Object {
  37. GDCLASS(Physics2DDirectBodyState, Object);
  38. protected:
  39. static void _bind_methods();
  40. public:
  41. virtual Vector2 get_total_gravity() const = 0; // get gravity vector working on this body space/area
  42. virtual float get_total_linear_damp() const = 0; // get density of this body space/area
  43. virtual float get_total_angular_damp() const = 0; // get density of this body space/area
  44. virtual float get_inverse_mass() const = 0; // get the mass
  45. virtual real_t get_inverse_inertia() const = 0; // get density of this body space
  46. virtual void set_linear_velocity(const Vector2 &p_velocity) = 0;
  47. virtual Vector2 get_linear_velocity() const = 0;
  48. virtual void set_angular_velocity(real_t p_velocity) = 0;
  49. virtual real_t get_angular_velocity() const = 0;
  50. virtual void set_transform(const Transform2D &p_transform) = 0;
  51. virtual Transform2D get_transform() const = 0;
  52. virtual Vector2 get_velocity_at_local_position(const Vector2 &p_position) const = 0;
  53. virtual void add_central_force(const Vector2 &p_force) = 0;
  54. virtual void add_force(const Vector2 &p_offset, const Vector2 &p_force) = 0;
  55. virtual void add_torque(real_t p_torque) = 0;
  56. virtual void apply_central_impulse(const Vector2 &p_impulse) = 0;
  57. virtual void apply_torque_impulse(real_t p_torque) = 0;
  58. virtual void apply_impulse(const Vector2 &p_offset, const Vector2 &p_impulse) = 0;
  59. virtual void set_sleep_state(bool p_enable) = 0;
  60. virtual bool is_sleeping() const = 0;
  61. virtual int get_contact_count() const = 0;
  62. virtual Vector2 get_contact_local_position(int p_contact_idx) const = 0;
  63. virtual Vector2 get_contact_local_normal(int p_contact_idx) const = 0;
  64. virtual int get_contact_local_shape(int p_contact_idx) const = 0;
  65. virtual RID get_contact_collider(int p_contact_idx) const = 0;
  66. virtual Vector2 get_contact_collider_position(int p_contact_idx) const = 0;
  67. virtual ObjectID get_contact_collider_id(int p_contact_idx) const = 0;
  68. virtual Object *get_contact_collider_object(int p_contact_idx) const;
  69. virtual int get_contact_collider_shape(int p_contact_idx) const = 0;
  70. virtual Variant get_contact_collider_shape_metadata(int p_contact_idx) const = 0;
  71. virtual Vector2 get_contact_collider_velocity_at_position(int p_contact_idx) const = 0;
  72. virtual real_t get_step() const = 0;
  73. virtual void integrate_forces();
  74. virtual Physics2DDirectSpaceState *get_space_state() = 0;
  75. Physics2DDirectBodyState();
  76. };
  77. //used for script
  78. class Physics2DShapeQueryParameters : public Reference {
  79. GDCLASS(Physics2DShapeQueryParameters, Reference);
  80. friend class Physics2DDirectSpaceState;
  81. RID shape;
  82. Transform2D transform;
  83. Vector2 motion;
  84. float margin;
  85. Set<RID> exclude;
  86. uint32_t collision_mask;
  87. bool collide_with_bodies;
  88. bool collide_with_areas;
  89. protected:
  90. static void _bind_methods();
  91. public:
  92. void set_shape(const RES &p_shape);
  93. void set_shape_rid(const RID &p_shape);
  94. RID get_shape_rid() const;
  95. void set_transform(const Transform2D &p_transform);
  96. Transform2D get_transform() const;
  97. void set_motion(const Vector2 &p_motion);
  98. Vector2 get_motion() const;
  99. void set_margin(float p_margin);
  100. float get_margin() const;
  101. void set_collision_mask(uint32_t p_mask);
  102. uint32_t get_collision_mask() const;
  103. void set_collide_with_bodies(bool p_enable);
  104. bool is_collide_with_bodies_enabled() const;
  105. void set_collide_with_areas(bool p_enable);
  106. bool is_collide_with_areas_enabled() const;
  107. void set_exclude(const Vector<RID> &p_exclude);
  108. Vector<RID> get_exclude() const;
  109. Physics2DShapeQueryParameters();
  110. };
  111. class Physics2DDirectSpaceState : public Object {
  112. GDCLASS(Physics2DDirectSpaceState, Object);
  113. Dictionary _intersect_ray(const Vector2 &p_from, const Vector2 &p_to, const Vector<RID> &p_exclude = Vector<RID>(), uint32_t p_layers = 0, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
  114. Array _intersect_point(const Vector2 &p_point, int p_max_results = 32, const Vector<RID> &p_exclude = Vector<RID>(), uint32_t p_layers = 0, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
  115. Array _intersect_point_on_canvas(const Vector2 &p_point, ObjectID p_canvas_intance_id, int p_max_results = 32, const Vector<RID> &p_exclude = Vector<RID>(), uint32_t p_layers = 0, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
  116. Array _intersect_point_impl(const Vector2 &p_point, int p_max_results, const Vector<RID> &p_exclud, uint32_t p_layers, bool p_collide_with_bodies, bool p_collide_with_areas, bool p_filter_by_canvas = false, ObjectID p_canvas_instance_id = 0);
  117. Array _intersect_shape(const Ref<Physics2DShapeQueryParameters> &p_shape_query, int p_max_results = 32);
  118. Array _cast_motion(const Ref<Physics2DShapeQueryParameters> &p_shape_query);
  119. Array _collide_shape(const Ref<Physics2DShapeQueryParameters> &p_shape_query, int p_max_results = 32);
  120. Dictionary _get_rest_info(const Ref<Physics2DShapeQueryParameters> &p_shape_query);
  121. protected:
  122. static void _bind_methods();
  123. public:
  124. struct RayResult {
  125. Vector2 position;
  126. Vector2 normal;
  127. RID rid;
  128. ObjectID collider_id = 0;
  129. Object *collider = nullptr;
  130. int shape = 0;
  131. Variant metadata;
  132. };
  133. virtual bool intersect_ray(const Vector2 &p_from, const Vector2 &p_to, RayResult &r_result, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_layer = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false) = 0;
  134. struct ShapeResult {
  135. RID rid;
  136. ObjectID collider_id = 0;
  137. Object *collider = nullptr;
  138. int shape = 0;
  139. Variant metadata;
  140. };
  141. virtual int intersect_point(const Vector2 &p_point, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_layer = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false, bool p_pick_point = false) = 0;
  142. virtual int intersect_point_on_canvas(const Vector2 &p_point, ObjectID p_canvas_instance_id, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_layer = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false, bool p_pick_point = false) = 0;
  143. virtual int intersect_shape(const RID &p_shape, const Transform2D &p_xform, const Vector2 &p_motion, float p_margin, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_layer = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false) = 0;
  144. virtual bool cast_motion(const RID &p_shape, const Transform2D &p_xform, const Vector2 &p_motion, float p_margin, float &p_closest_safe, float &p_closest_unsafe, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_layer = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false) = 0;
  145. virtual bool collide_shape(RID p_shape, const Transform2D &p_shape_xform, const Vector2 &p_motion, float p_margin, Vector2 *r_results, int p_result_max, int &r_result_count, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_layer = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false) = 0;
  146. struct ShapeRestInfo {
  147. Vector2 point;
  148. Vector2 normal;
  149. RID rid;
  150. ObjectID collider_id = 0;
  151. int shape = 0;
  152. Vector2 linear_velocity; //velocity at contact point
  153. Variant metadata;
  154. };
  155. virtual bool rest_info(RID p_shape, const Transform2D &p_shape_xform, const Vector2 &p_motion, float p_margin, ShapeRestInfo *r_info, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_layer = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false) = 0;
  156. Physics2DDirectSpaceState();
  157. };
  158. class Physics2DTestMotionResult;
  159. class Physics2DServer : public Object {
  160. GDCLASS(Physics2DServer, Object);
  161. static Physics2DServer *singleton;
  162. virtual bool _body_test_motion(RID p_body, const Transform2D &p_from, const Vector2 &p_motion, bool p_infinite_inertia, float p_margin = 0.08, const Ref<Physics2DTestMotionResult> &p_result = Ref<Physics2DTestMotionResult>(), bool p_exclude_raycast_shapes = true, const Vector<RID> &p_exclude = Vector<RID>());
  163. protected:
  164. static void _bind_methods();
  165. public:
  166. static Physics2DServer *get_singleton();
  167. enum ShapeType {
  168. SHAPE_LINE, ///< plane:"plane"
  169. SHAPE_RAY, ///< float:"length"
  170. SHAPE_SEGMENT, ///< float:"length"
  171. SHAPE_CIRCLE, ///< float:"radius"
  172. SHAPE_RECTANGLE, ///< vec3:"extents"
  173. SHAPE_CAPSULE,
  174. SHAPE_CONVEX_POLYGON, ///< array of planes:"planes"
  175. SHAPE_CONCAVE_POLYGON, ///< Vector2 array:"triangles" , or Dictionary with "indices" (int array) and "triangles" (Vector2 array)
  176. SHAPE_CUSTOM, ///< Server-Implementation based custom shape, calling shape_create() with this value will result in an error
  177. };
  178. virtual RID line_shape_create() = 0;
  179. virtual RID ray_shape_create() = 0;
  180. virtual RID segment_shape_create() = 0;
  181. virtual RID circle_shape_create() = 0;
  182. virtual RID rectangle_shape_create() = 0;
  183. virtual RID capsule_shape_create() = 0;
  184. virtual RID convex_polygon_shape_create() = 0;
  185. virtual RID concave_polygon_shape_create() = 0;
  186. virtual void shape_set_data(RID p_shape, const Variant &p_data) = 0;
  187. virtual void shape_set_custom_solver_bias(RID p_shape, real_t p_bias) = 0;
  188. virtual ShapeType shape_get_type(RID p_shape) const = 0;
  189. virtual Variant shape_get_data(RID p_shape) const = 0;
  190. virtual real_t shape_get_custom_solver_bias(RID p_shape) const = 0;
  191. //these work well, but should be used from the main thread only
  192. 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) = 0;
  193. /* SPACE API */
  194. virtual RID space_create() = 0;
  195. virtual void space_set_active(RID p_space, bool p_active) = 0;
  196. virtual bool space_is_active(RID p_space) const = 0;
  197. enum SpaceParameter {
  198. SPACE_PARAM_CONTACT_RECYCLE_RADIUS,
  199. SPACE_PARAM_CONTACT_MAX_SEPARATION,
  200. SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION,
  201. SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD,
  202. SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD,
  203. SPACE_PARAM_BODY_TIME_TO_SLEEP,
  204. SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS,
  205. };
  206. virtual void space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) = 0;
  207. virtual real_t space_get_param(RID p_space, SpaceParameter p_param) const = 0;
  208. // this function only works on physics process, errors and returns null otherwise
  209. virtual Physics2DDirectSpaceState *space_get_direct_state(RID p_space) = 0;
  210. virtual void space_set_debug_contacts(RID p_space, int p_max_contacts) = 0;
  211. virtual Vector<Vector2> space_get_contacts(RID p_space) const = 0;
  212. virtual int space_get_contact_count(RID p_space) const = 0;
  213. //missing space parameters
  214. /* AREA API */
  215. //missing attenuation? missing better override?
  216. enum AreaParameter {
  217. AREA_PARAM_GRAVITY,
  218. AREA_PARAM_GRAVITY_VECTOR,
  219. AREA_PARAM_GRAVITY_IS_POINT,
  220. AREA_PARAM_GRAVITY_DISTANCE_SCALE,
  221. AREA_PARAM_GRAVITY_POINT_ATTENUATION,
  222. AREA_PARAM_LINEAR_DAMP,
  223. AREA_PARAM_ANGULAR_DAMP,
  224. AREA_PARAM_PRIORITY
  225. };
  226. virtual RID area_create() = 0;
  227. virtual void area_set_space(RID p_area, RID p_space) = 0;
  228. virtual RID area_get_space(RID p_area) const = 0;
  229. enum AreaSpaceOverrideMode {
  230. AREA_SPACE_OVERRIDE_DISABLED,
  231. AREA_SPACE_OVERRIDE_COMBINE,
  232. AREA_SPACE_OVERRIDE_COMBINE_REPLACE, // Combines, then discards all subsequent calculations
  233. AREA_SPACE_OVERRIDE_REPLACE,
  234. AREA_SPACE_OVERRIDE_REPLACE_COMBINE // Discards all previous calculations, then keeps combining
  235. };
  236. virtual void area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode) = 0;
  237. virtual AreaSpaceOverrideMode area_get_space_override_mode(RID p_area) const = 0;
  238. virtual void area_add_shape(RID p_area, RID p_shape, const Transform2D &p_transform = Transform2D(), bool p_disabled = false) = 0;
  239. virtual void area_set_shape(RID p_area, int p_shape_idx, RID p_shape) = 0;
  240. virtual void area_set_shape_transform(RID p_area, int p_shape_idx, const Transform2D &p_transform) = 0;
  241. virtual int area_get_shape_count(RID p_area) const = 0;
  242. virtual RID area_get_shape(RID p_area, int p_shape_idx) const = 0;
  243. virtual Transform2D area_get_shape_transform(RID p_area, int p_shape_idx) const = 0;
  244. virtual void area_remove_shape(RID p_area, int p_shape_idx) = 0;
  245. virtual void area_clear_shapes(RID p_area) = 0;
  246. virtual void area_set_shape_disabled(RID p_area, int p_shape, bool p_disabled) = 0;
  247. virtual void area_attach_object_instance_id(RID p_area, ObjectID p_id) = 0;
  248. virtual ObjectID area_get_object_instance_id(RID p_area) const = 0;
  249. virtual void area_attach_canvas_instance_id(RID p_area, ObjectID p_id) = 0;
  250. virtual ObjectID area_get_canvas_instance_id(RID p_area) const = 0;
  251. virtual void area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) = 0;
  252. virtual void area_set_transform(RID p_area, const Transform2D &p_transform) = 0;
  253. virtual Variant area_get_param(RID p_parea, AreaParameter p_param) const = 0;
  254. virtual Transform2D area_get_transform(RID p_area) const = 0;
  255. virtual void area_set_collision_mask(RID p_area, uint32_t p_mask) = 0;
  256. virtual void area_set_collision_layer(RID p_area, uint32_t p_layer) = 0;
  257. virtual void area_set_monitorable(RID p_area, bool p_monitorable) = 0;
  258. virtual void area_set_pickable(RID p_area, bool p_pickable) = 0;
  259. virtual void area_set_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) = 0;
  260. virtual void area_set_area_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) = 0;
  261. /* BODY API */
  262. //missing ccd?
  263. enum BodyMode {
  264. BODY_MODE_STATIC,
  265. BODY_MODE_KINEMATIC,
  266. BODY_MODE_RIGID,
  267. BODY_MODE_CHARACTER
  268. };
  269. virtual RID body_create() = 0;
  270. virtual void body_set_space(RID p_body, RID p_space) = 0;
  271. virtual RID body_get_space(RID p_body) const = 0;
  272. virtual void body_set_mode(RID p_body, BodyMode p_mode) = 0;
  273. virtual BodyMode body_get_mode(RID p_body) const = 0;
  274. virtual void body_add_shape(RID p_body, RID p_shape, const Transform2D &p_transform = Transform2D(), bool p_disabled = false) = 0;
  275. virtual void body_set_shape(RID p_body, int p_shape_idx, RID p_shape) = 0;
  276. virtual void body_set_shape_transform(RID p_body, int p_shape_idx, const Transform2D &p_transform) = 0;
  277. virtual void body_set_shape_metadata(RID p_body, int p_shape_idx, const Variant &p_metadata) = 0;
  278. virtual int body_get_shape_count(RID p_body) const = 0;
  279. virtual RID body_get_shape(RID p_body, int p_shape_idx) const = 0;
  280. virtual Transform2D body_get_shape_transform(RID p_body, int p_shape_idx) const = 0;
  281. virtual Variant body_get_shape_metadata(RID p_body, int p_shape_idx) const = 0;
  282. virtual void body_set_shape_disabled(RID p_body, int p_shape, bool p_disabled) = 0;
  283. virtual void body_set_shape_as_one_way_collision(RID p_body, int p_shape, bool p_enabled, float p_margin = 0) = 0;
  284. virtual void body_remove_shape(RID p_body, int p_shape_idx) = 0;
  285. virtual void body_clear_shapes(RID p_body) = 0;
  286. virtual void body_attach_object_instance_id(RID p_body, uint32_t p_id) = 0;
  287. virtual uint32_t body_get_object_instance_id(RID p_body) const = 0;
  288. virtual void body_attach_canvas_instance_id(RID p_body, uint32_t p_id) = 0;
  289. virtual uint32_t body_get_canvas_instance_id(RID p_body) const = 0;
  290. enum CCDMode {
  291. CCD_MODE_DISABLED,
  292. CCD_MODE_CAST_RAY,
  293. CCD_MODE_CAST_SHAPE,
  294. };
  295. virtual void body_set_continuous_collision_detection_mode(RID p_body, CCDMode p_mode) = 0;
  296. virtual CCDMode body_get_continuous_collision_detection_mode(RID p_body) const = 0;
  297. virtual void body_set_collision_layer(RID p_body, uint32_t p_layer) = 0;
  298. virtual uint32_t body_get_collision_layer(RID p_body) const = 0;
  299. virtual void body_set_collision_mask(RID p_body, uint32_t p_mask) = 0;
  300. virtual uint32_t body_get_collision_mask(RID p_body) const = 0;
  301. // common body variables
  302. enum BodyParameter {
  303. BODY_PARAM_BOUNCE,
  304. BODY_PARAM_FRICTION,
  305. BODY_PARAM_MASS, ///< unused for static, always infinite
  306. BODY_PARAM_INERTIA, // read-only: computed from mass & shapes
  307. BODY_PARAM_GRAVITY_SCALE,
  308. BODY_PARAM_LINEAR_DAMP,
  309. BODY_PARAM_ANGULAR_DAMP,
  310. BODY_PARAM_MAX,
  311. };
  312. virtual void body_set_param(RID p_body, BodyParameter p_param, float p_value) = 0;
  313. virtual float body_get_param(RID p_body, BodyParameter p_param) const = 0;
  314. //state
  315. enum BodyState {
  316. BODY_STATE_TRANSFORM,
  317. BODY_STATE_LINEAR_VELOCITY,
  318. BODY_STATE_ANGULAR_VELOCITY,
  319. BODY_STATE_SLEEPING,
  320. BODY_STATE_CAN_SLEEP,
  321. };
  322. virtual void body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) = 0;
  323. virtual Variant body_get_state(RID p_body, BodyState p_state) const = 0;
  324. //do something about it
  325. virtual void body_set_applied_force(RID p_body, const Vector2 &p_force) = 0;
  326. virtual Vector2 body_get_applied_force(RID p_body) const = 0;
  327. virtual void body_set_applied_torque(RID p_body, float p_torque) = 0;
  328. virtual float body_get_applied_torque(RID p_body) const = 0;
  329. virtual void body_add_central_force(RID p_body, const Vector2 &p_force) = 0;
  330. virtual void body_add_force(RID p_body, const Vector2 &p_offset, const Vector2 &p_force) = 0;
  331. virtual void body_add_torque(RID p_body, float p_torque) = 0;
  332. virtual void body_apply_central_impulse(RID p_body, const Vector2 &p_impulse) = 0;
  333. virtual void body_apply_torque_impulse(RID p_body, float p_torque) = 0;
  334. virtual void body_apply_impulse(RID p_body, const Vector2 &p_offset, const Vector2 &p_impulse) = 0;
  335. virtual void body_set_axis_velocity(RID p_body, const Vector2 &p_axis_velocity) = 0;
  336. //fix
  337. virtual void body_add_collision_exception(RID p_body, RID p_body_b) = 0;
  338. virtual void body_remove_collision_exception(RID p_body, RID p_body_b) = 0;
  339. virtual void body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) = 0;
  340. virtual void body_set_max_contacts_reported(RID p_body, int p_contacts) = 0;
  341. virtual int body_get_max_contacts_reported(RID p_body) const = 0;
  342. //missing remove
  343. virtual void body_set_contacts_reported_depth_threshold(RID p_body, float p_threshold) = 0;
  344. virtual float body_get_contacts_reported_depth_threshold(RID p_body) const = 0;
  345. virtual void body_set_omit_force_integration(RID p_body, bool p_omit) = 0;
  346. virtual bool body_is_omitting_force_integration(RID p_body) const = 0;
  347. virtual void body_set_force_integration_callback(RID p_body, Object *p_receiver, const StringName &p_method, const Variant &p_udata = Variant()) = 0;
  348. 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) = 0;
  349. virtual void body_set_pickable(RID p_body, bool p_pickable) = 0;
  350. // this function only works on physics process, errors and returns null otherwise
  351. virtual Physics2DDirectBodyState *body_get_direct_state(RID p_body) = 0;
  352. struct MotionResult {
  353. Vector2 motion;
  354. Vector2 remainder;
  355. Vector2 collision_point;
  356. Vector2 collision_normal;
  357. Vector2 collider_velocity;
  358. real_t collision_depth = 0.0;
  359. real_t collision_safe_fraction = 0.0;
  360. real_t collision_unsafe_fraction = 0.0;
  361. int collision_local_shape = 0;
  362. ObjectID collider_id = 0;
  363. RID collider;
  364. int collider_shape = 0;
  365. Variant collider_metadata;
  366. };
  367. virtual bool body_test_motion(RID p_body, const Transform2D &p_from, const Vector2 &p_motion, bool p_infinite_inertia, float p_margin = 0.08, MotionResult *r_result = nullptr, bool p_exclude_raycast_shapes = true, const Set<RID> &p_exclude = Set<RID>()) = 0;
  368. struct SeparationResult {
  369. float collision_depth;
  370. Vector2 collision_point;
  371. Vector2 collision_normal;
  372. Vector2 collider_velocity;
  373. int collision_local_shape;
  374. ObjectID collider_id;
  375. RID collider;
  376. int collider_shape;
  377. Variant collider_metadata;
  378. };
  379. 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.08) = 0;
  380. /* JOINT API */
  381. enum JointType {
  382. JOINT_PIN,
  383. JOINT_GROOVE,
  384. JOINT_DAMPED_SPRING
  385. };
  386. enum JointParam {
  387. JOINT_PARAM_BIAS,
  388. JOINT_PARAM_MAX_BIAS,
  389. JOINT_PARAM_MAX_FORCE,
  390. };
  391. virtual void joint_set_param(RID p_joint, JointParam p_param, real_t p_value) = 0;
  392. virtual real_t joint_get_param(RID p_joint, JointParam p_param) const = 0;
  393. virtual void joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable) = 0;
  394. virtual bool joint_is_disabled_collisions_between_bodies(RID p_joint) const = 0;
  395. virtual RID pin_joint_create(const Vector2 &p_anchor, RID p_body_a, RID p_body_b = RID()) = 0;
  396. 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) = 0;
  397. 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()) = 0;
  398. enum PinJointParam {
  399. PIN_JOINT_SOFTNESS
  400. };
  401. virtual void pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) = 0;
  402. virtual real_t pin_joint_get_param(RID p_joint, PinJointParam p_param) const = 0;
  403. enum DampedStringParam {
  404. DAMPED_STRING_REST_LENGTH,
  405. DAMPED_STRING_STIFFNESS,
  406. DAMPED_STRING_DAMPING
  407. };
  408. virtual void damped_string_joint_set_param(RID p_joint, DampedStringParam p_param, real_t p_value) = 0;
  409. virtual real_t damped_string_joint_get_param(RID p_joint, DampedStringParam p_param) const = 0;
  410. virtual JointType joint_get_type(RID p_joint) const = 0;
  411. /* QUERY API */
  412. enum AreaBodyStatus {
  413. AREA_BODY_ADDED,
  414. AREA_BODY_REMOVED
  415. };
  416. /* MISC */
  417. virtual void free(RID p_rid) = 0;
  418. virtual void set_active(bool p_active) = 0;
  419. virtual void init() = 0;
  420. virtual void step(float p_step) = 0;
  421. virtual void sync() = 0;
  422. virtual void flush_queries() = 0;
  423. virtual void end_sync() = 0;
  424. virtual void finish() = 0;
  425. virtual bool is_flushing_queries() const = 0;
  426. virtual void set_collision_iterations(int p_iterations) = 0;
  427. enum ProcessInfo {
  428. INFO_ACTIVE_OBJECTS,
  429. INFO_COLLISION_PAIRS,
  430. INFO_ISLAND_COUNT
  431. };
  432. virtual int get_process_info(ProcessInfo p_info) = 0;
  433. Physics2DServer();
  434. ~Physics2DServer();
  435. };
  436. class Physics2DTestMotionResult : public Reference {
  437. GDCLASS(Physics2DTestMotionResult, Reference);
  438. Physics2DServer::MotionResult result;
  439. bool colliding = false;
  440. friend class Physics2DServer;
  441. protected:
  442. static void _bind_methods();
  443. public:
  444. Physics2DServer::MotionResult *get_result_ptr() const { return const_cast<Physics2DServer::MotionResult *>(&result); }
  445. //bool is_colliding() const;
  446. Vector2 get_motion() const;
  447. Vector2 get_motion_remainder() const;
  448. Vector2 get_collision_point() const;
  449. Vector2 get_collision_normal() const;
  450. Vector2 get_collider_velocity() const;
  451. ObjectID get_collider_id() const;
  452. RID get_collider_rid() const;
  453. Object *get_collider() const;
  454. int get_collider_shape() const;
  455. real_t get_collision_depth() const;
  456. real_t get_collision_safe_fraction() const;
  457. real_t get_collision_unsafe_fraction() const;
  458. };
  459. typedef Physics2DServer *(*CreatePhysics2DServerCallback)();
  460. class Physics2DServerManager {
  461. struct ClassInfo {
  462. String name;
  463. CreatePhysics2DServerCallback create_callback;
  464. ClassInfo() :
  465. name(""),
  466. create_callback(nullptr) {}
  467. ClassInfo(String p_name, CreatePhysics2DServerCallback p_create_callback) :
  468. name(p_name),
  469. create_callback(p_create_callback) {}
  470. ClassInfo(const ClassInfo &p_ci) :
  471. name(p_ci.name),
  472. create_callback(p_ci.create_callback) {}
  473. ClassInfo operator=(const ClassInfo &p_ci) {
  474. name = p_ci.name;
  475. create_callback = p_ci.create_callback;
  476. return *this;
  477. }
  478. };
  479. static Vector<ClassInfo> physics_2d_servers;
  480. static int default_server_id;
  481. static int default_server_priority;
  482. public:
  483. static const String setting_property_name;
  484. private:
  485. static void on_servers_changed();
  486. public:
  487. static void register_server(const String &p_name, CreatePhysics2DServerCallback p_creat_callback);
  488. static void set_default_server(const String &p_name, int p_priority = 0);
  489. static int find_server_id(const String &p_name);
  490. static int get_servers_count();
  491. static String get_server_name(int p_id);
  492. static Physics2DServer *new_default_server();
  493. static Physics2DServer *new_server(const String &p_name);
  494. };
  495. VARIANT_ENUM_CAST(Physics2DServer::ShapeType);
  496. VARIANT_ENUM_CAST(Physics2DServer::SpaceParameter);
  497. VARIANT_ENUM_CAST(Physics2DServer::AreaParameter);
  498. VARIANT_ENUM_CAST(Physics2DServer::AreaSpaceOverrideMode);
  499. VARIANT_ENUM_CAST(Physics2DServer::BodyMode);
  500. VARIANT_ENUM_CAST(Physics2DServer::BodyParameter);
  501. VARIANT_ENUM_CAST(Physics2DServer::BodyState);
  502. VARIANT_ENUM_CAST(Physics2DServer::CCDMode);
  503. VARIANT_ENUM_CAST(Physics2DServer::JointParam);
  504. VARIANT_ENUM_CAST(Physics2DServer::JointType);
  505. VARIANT_ENUM_CAST(Physics2DServer::DampedStringParam);
  506. //VARIANT_ENUM_CAST( Physics2DServer::ObjectType );
  507. VARIANT_ENUM_CAST(Physics2DServer::AreaBodyStatus);
  508. VARIANT_ENUM_CAST(Physics2DServer::ProcessInfo);
  509. #endif // PHYSICS_2D_SERVER_H