physics_2d_server.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*************************************************************************/
  2. /* physics_2d_server.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_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 void add_central_force(const Vector2 &p_force) = 0;
  53. virtual void add_force(const Vector2 &p_offset, const Vector2 &p_force) = 0;
  54. virtual void add_torque(real_t p_torque) = 0;
  55. virtual void apply_central_impulse(const Vector2 &p_impulse) = 0;
  56. virtual void apply_torque_impulse(real_t p_torque) = 0;
  57. virtual void apply_impulse(const Vector2 &p_offset, const Vector2 &p_impulse) = 0;
  58. virtual void set_sleep_state(bool p_enable) = 0;
  59. virtual bool is_sleeping() const = 0;
  60. virtual int get_contact_count() const = 0;
  61. virtual Vector2 get_contact_local_position(int p_contact_idx) const = 0;
  62. virtual Vector2 get_contact_local_normal(int p_contact_idx) const = 0;
  63. virtual int get_contact_local_shape(int p_contact_idx) const = 0;
  64. virtual RID get_contact_collider(int p_contact_idx) const = 0;
  65. virtual Vector2 get_contact_collider_position(int p_contact_idx) const = 0;
  66. virtual ObjectID get_contact_collider_id(int p_contact_idx) const = 0;
  67. virtual Object *get_contact_collider_object(int p_contact_idx) const;
  68. virtual int get_contact_collider_shape(int p_contact_idx) const = 0;
  69. virtual Variant get_contact_collider_shape_metadata(int p_contact_idx) const = 0;
  70. virtual Vector2 get_contact_collider_velocity_at_position(int p_contact_idx) const = 0;
  71. virtual real_t get_step() const = 0;
  72. virtual void integrate_forces();
  73. virtual Physics2DDirectSpaceState *get_space_state() = 0;
  74. Physics2DDirectBodyState();
  75. };
  76. class Physics2DShapeQueryResult;
  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(int p_collision_mask);
  102. int 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;
  129. Object *collider;
  130. int shape;
  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;
  137. Object *collider;
  138. int shape;
  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;
  151. int shape;
  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 Physics2DShapeQueryResult : public Reference {
  159. GDCLASS(Physics2DShapeQueryResult, Reference);
  160. Vector<Physics2DDirectSpaceState::ShapeResult> result;
  161. friend class Physics2DDirectSpaceState;
  162. protected:
  163. static void _bind_methods();
  164. public:
  165. int get_result_count() const;
  166. RID get_result_rid(int p_idx) const;
  167. ObjectID get_result_object_id(int p_idx) const;
  168. Object *get_result_object(int p_idx) const;
  169. int get_result_object_shape(int p_idx) const;
  170. Physics2DShapeQueryResult();
  171. };
  172. class Physics2DTestMotionResult;
  173. class Physics2DServer : public Object {
  174. GDCLASS(Physics2DServer, Object);
  175. static Physics2DServer *singleton;
  176. 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>());
  177. protected:
  178. static void _bind_methods();
  179. public:
  180. static Physics2DServer *get_singleton();
  181. enum ShapeType {
  182. SHAPE_LINE, ///< plane:"plane"
  183. SHAPE_RAY, ///< float:"length"
  184. SHAPE_SEGMENT, ///< float:"length"
  185. SHAPE_CIRCLE, ///< float:"radius"
  186. SHAPE_RECTANGLE, ///< vec3:"extents"
  187. SHAPE_CAPSULE,
  188. SHAPE_CONVEX_POLYGON, ///< array of planes:"planes"
  189. SHAPE_CONCAVE_POLYGON, ///< Vector2 array:"triangles" , or Dictionary with "indices" (int array) and "triangles" (Vector2 array)
  190. SHAPE_CUSTOM, ///< Server-Implementation based custom shape, calling shape_create() with this value will result in an error
  191. };
  192. virtual RID line_shape_create() = 0;
  193. virtual RID ray_shape_create() = 0;
  194. virtual RID segment_shape_create() = 0;
  195. virtual RID circle_shape_create() = 0;
  196. virtual RID rectangle_shape_create() = 0;
  197. virtual RID capsule_shape_create() = 0;
  198. virtual RID convex_polygon_shape_create() = 0;
  199. virtual RID concave_polygon_shape_create() = 0;
  200. virtual void shape_set_data(RID p_shape, const Variant &p_data) = 0;
  201. virtual void shape_set_custom_solver_bias(RID p_shape, real_t p_bias) = 0;
  202. virtual ShapeType shape_get_type(RID p_shape) const = 0;
  203. virtual Variant shape_get_data(RID p_shape) const = 0;
  204. virtual real_t shape_get_custom_solver_bias(RID p_shape) const = 0;
  205. //these work well, but should be used from the main thread only
  206. 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;
  207. /* SPACE API */
  208. virtual RID space_create() = 0;
  209. virtual void space_set_active(RID p_space, bool p_active) = 0;
  210. virtual bool space_is_active(RID p_space) const = 0;
  211. enum SpaceParameter {
  212. SPACE_PARAM_CONTACT_RECYCLE_RADIUS,
  213. SPACE_PARAM_CONTACT_MAX_SEPARATION,
  214. SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION,
  215. SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD,
  216. SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD,
  217. SPACE_PARAM_BODY_TIME_TO_SLEEP,
  218. SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS,
  219. SPACE_PARAM_TEST_MOTION_MIN_CONTACT_DEPTH,
  220. };
  221. virtual void space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) = 0;
  222. virtual real_t space_get_param(RID p_space, SpaceParameter p_param) const = 0;
  223. // this function only works on physics process, errors and returns null otherwise
  224. virtual Physics2DDirectSpaceState *space_get_direct_state(RID p_space) = 0;
  225. virtual void space_set_debug_contacts(RID p_space, int p_max_contacts) = 0;
  226. virtual Vector<Vector2> space_get_contacts(RID p_space) const = 0;
  227. virtual int space_get_contact_count(RID p_space) const = 0;
  228. //missing space parameters
  229. /* AREA API */
  230. //missing attenuation? missing better override?
  231. enum AreaParameter {
  232. AREA_PARAM_GRAVITY,
  233. AREA_PARAM_GRAVITY_VECTOR,
  234. AREA_PARAM_GRAVITY_IS_POINT,
  235. AREA_PARAM_GRAVITY_DISTANCE_SCALE,
  236. AREA_PARAM_GRAVITY_POINT_ATTENUATION,
  237. AREA_PARAM_LINEAR_DAMP,
  238. AREA_PARAM_ANGULAR_DAMP,
  239. AREA_PARAM_PRIORITY
  240. };
  241. virtual RID area_create() = 0;
  242. virtual void area_set_space(RID p_area, RID p_space) = 0;
  243. virtual RID area_get_space(RID p_area) const = 0;
  244. enum AreaSpaceOverrideMode {
  245. AREA_SPACE_OVERRIDE_DISABLED,
  246. AREA_SPACE_OVERRIDE_COMBINE,
  247. AREA_SPACE_OVERRIDE_COMBINE_REPLACE, // Combines, then discards all subsequent calculations
  248. AREA_SPACE_OVERRIDE_REPLACE,
  249. AREA_SPACE_OVERRIDE_REPLACE_COMBINE // Discards all previous calculations, then keeps combining
  250. };
  251. virtual void area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode) = 0;
  252. virtual AreaSpaceOverrideMode area_get_space_override_mode(RID p_area) const = 0;
  253. virtual void area_add_shape(RID p_area, RID p_shape, const Transform2D &p_transform = Transform2D()) = 0;
  254. virtual void area_set_shape(RID p_area, int p_shape_idx, RID p_shape) = 0;
  255. virtual void area_set_shape_transform(RID p_area, int p_shape_idx, const Transform2D &p_transform) = 0;
  256. virtual int area_get_shape_count(RID p_area) const = 0;
  257. virtual RID area_get_shape(RID p_area, int p_shape_idx) const = 0;
  258. virtual Transform2D area_get_shape_transform(RID p_area, int p_shape_idx) const = 0;
  259. virtual void area_remove_shape(RID p_area, int p_shape_idx) = 0;
  260. virtual void area_clear_shapes(RID p_area) = 0;
  261. virtual void area_set_shape_disabled(RID p_area, int p_shape, bool p_disabled) = 0;
  262. virtual void area_attach_object_instance_id(RID p_area, ObjectID p_ID) = 0;
  263. virtual ObjectID area_get_object_instance_id(RID p_area) const = 0;
  264. virtual void area_attach_canvas_instance_id(RID p_area, ObjectID p_ID) = 0;
  265. virtual ObjectID area_get_canvas_instance_id(RID p_area) const = 0;
  266. virtual void area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) = 0;
  267. virtual void area_set_transform(RID p_area, const Transform2D &p_transform) = 0;
  268. virtual Variant area_get_param(RID p_parea, AreaParameter p_param) const = 0;
  269. virtual Transform2D area_get_transform(RID p_area) const = 0;
  270. virtual void area_set_collision_mask(RID p_area, uint32_t p_mask) = 0;
  271. virtual void area_set_collision_layer(RID p_area, uint32_t p_layer) = 0;
  272. virtual void area_set_monitorable(RID p_area, bool p_monitorable) = 0;
  273. virtual void area_set_pickable(RID p_area, bool p_pickable) = 0;
  274. virtual void area_set_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) = 0;
  275. virtual void area_set_area_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) = 0;
  276. /* BODY API */
  277. //missing ccd?
  278. enum BodyMode {
  279. BODY_MODE_STATIC,
  280. BODY_MODE_KINEMATIC,
  281. BODY_MODE_RIGID,
  282. BODY_MODE_CHARACTER
  283. };
  284. virtual RID body_create() = 0;
  285. virtual void body_set_space(RID p_body, RID p_space) = 0;
  286. virtual RID body_get_space(RID p_body) const = 0;
  287. virtual void body_set_mode(RID p_body, BodyMode p_mode) = 0;
  288. virtual BodyMode body_get_mode(RID p_body) const = 0;
  289. virtual void body_add_shape(RID p_body, RID p_shape, const Transform2D &p_transform = Transform2D()) = 0;
  290. virtual void body_set_shape(RID p_body, int p_shape_idx, RID p_shape) = 0;
  291. virtual void body_set_shape_transform(RID p_body, int p_shape_idx, const Transform2D &p_transform) = 0;
  292. virtual void body_set_shape_metadata(RID p_body, int p_shape_idx, const Variant &p_metadata) = 0;
  293. virtual int body_get_shape_count(RID p_body) const = 0;
  294. virtual RID body_get_shape(RID p_body, int p_shape_idx) const = 0;
  295. virtual Transform2D body_get_shape_transform(RID p_body, int p_shape_idx) const = 0;
  296. virtual Variant body_get_shape_metadata(RID p_body, int p_shape_idx) const = 0;
  297. virtual void body_set_shape_disabled(RID p_body, int p_shape, bool p_disabled) = 0;
  298. virtual void body_set_shape_as_one_way_collision(RID p_body, int p_shape, bool p_enabled, float p_margin = 0) = 0;
  299. virtual void body_remove_shape(RID p_body, int p_shape_idx) = 0;
  300. virtual void body_clear_shapes(RID p_body) = 0;
  301. virtual void body_attach_object_instance_id(RID p_body, uint32_t p_ID) = 0;
  302. virtual uint32_t body_get_object_instance_id(RID p_body) const = 0;
  303. virtual void body_attach_canvas_instance_id(RID p_body, uint32_t p_ID) = 0;
  304. virtual uint32_t body_get_canvas_instance_id(RID p_body) const = 0;
  305. enum CCDMode {
  306. CCD_MODE_DISABLED,
  307. CCD_MODE_CAST_RAY,
  308. CCD_MODE_CAST_SHAPE,
  309. };
  310. virtual void body_set_continuous_collision_detection_mode(RID p_body, CCDMode p_mode) = 0;
  311. virtual CCDMode body_get_continuous_collision_detection_mode(RID p_body) const = 0;
  312. virtual void body_set_collision_layer(RID p_body, uint32_t p_layer) = 0;
  313. virtual uint32_t body_get_collision_layer(RID p_body) const = 0;
  314. virtual void body_set_collision_mask(RID p_body, uint32_t p_mask) = 0;
  315. virtual uint32_t body_get_collision_mask(RID p_body) const = 0;
  316. // common body variables
  317. enum BodyParameter {
  318. BODY_PARAM_BOUNCE,
  319. BODY_PARAM_FRICTION,
  320. BODY_PARAM_MASS, ///< unused for static, always infinite
  321. BODY_PARAM_INERTIA, // read-only: computed from mass & shapes
  322. BODY_PARAM_GRAVITY_SCALE,
  323. BODY_PARAM_LINEAR_DAMP,
  324. BODY_PARAM_ANGULAR_DAMP,
  325. BODY_PARAM_MAX,
  326. };
  327. virtual void body_set_param(RID p_body, BodyParameter p_param, float p_value) = 0;
  328. virtual float body_get_param(RID p_body, BodyParameter p_param) const = 0;
  329. //state
  330. enum BodyState {
  331. BODY_STATE_TRANSFORM,
  332. BODY_STATE_LINEAR_VELOCITY,
  333. BODY_STATE_ANGULAR_VELOCITY,
  334. BODY_STATE_SLEEPING,
  335. BODY_STATE_CAN_SLEEP,
  336. };
  337. virtual void body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) = 0;
  338. virtual Variant body_get_state(RID p_body, BodyState p_state) const = 0;
  339. //do something about it
  340. virtual void body_set_applied_force(RID p_body, const Vector2 &p_force) = 0;
  341. virtual Vector2 body_get_applied_force(RID p_body) const = 0;
  342. virtual void body_set_applied_torque(RID p_body, float p_torque) = 0;
  343. virtual float body_get_applied_torque(RID p_body) const = 0;
  344. virtual void body_add_central_force(RID p_body, const Vector2 &p_force) = 0;
  345. virtual void body_add_force(RID p_body, const Vector2 &p_offset, const Vector2 &p_force) = 0;
  346. virtual void body_add_torque(RID p_body, float p_torque) = 0;
  347. virtual void body_apply_central_impulse(RID p_body, const Vector2 &p_impulse) = 0;
  348. virtual void body_apply_torque_impulse(RID p_body, float p_torque) = 0;
  349. virtual void body_apply_impulse(RID p_body, const Vector2 &p_offset, const Vector2 &p_impulse) = 0;
  350. virtual void body_set_axis_velocity(RID p_body, const Vector2 &p_axis_velocity) = 0;
  351. //fix
  352. virtual void body_add_collision_exception(RID p_body, RID p_body_b) = 0;
  353. virtual void body_remove_collision_exception(RID p_body, RID p_body_b) = 0;
  354. virtual void body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) = 0;
  355. virtual void body_set_max_contacts_reported(RID p_body, int p_contacts) = 0;
  356. virtual int body_get_max_contacts_reported(RID p_body) const = 0;
  357. //missing remove
  358. virtual void body_set_contacts_reported_depth_threshold(RID p_body, float p_threshold) = 0;
  359. virtual float body_get_contacts_reported_depth_threshold(RID p_body) const = 0;
  360. virtual void body_set_omit_force_integration(RID p_body, bool p_omit) = 0;
  361. virtual bool body_is_omitting_force_integration(RID p_body) const = 0;
  362. virtual void body_set_force_integration_callback(RID p_body, Object *p_receiver, const StringName &p_method, const Variant &p_udata = Variant()) = 0;
  363. 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;
  364. virtual void body_set_pickable(RID p_body, bool p_pickable) = 0;
  365. // this function only works on physics process, errors and returns null otherwise
  366. virtual Physics2DDirectBodyState *body_get_direct_state(RID p_body) = 0;
  367. struct MotionResult {
  368. Vector2 motion;
  369. Vector2 remainder;
  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. MotionResult() {
  379. collision_local_shape = 0;
  380. collider_shape = 0;
  381. collider_id = 0;
  382. }
  383. };
  384. virtual bool body_test_motion(RID p_body, const Transform2D &p_from, const Vector2 &p_motion, bool p_infinite_inertia, float p_margin = 0.001, MotionResult *r_result = NULL, bool p_exclude_raycast_shapes = true) = 0;
  385. struct SeparationResult {
  386. float collision_depth;
  387. Vector2 collision_point;
  388. Vector2 collision_normal;
  389. Vector2 collider_velocity;
  390. int collision_local_shape;
  391. ObjectID collider_id;
  392. RID collider;
  393. int collider_shape;
  394. Variant collider_metadata;
  395. };
  396. 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) = 0;
  397. /* JOINT API */
  398. enum JointType {
  399. JOINT_PIN,
  400. JOINT_GROOVE,
  401. JOINT_DAMPED_SPRING
  402. };
  403. enum JointParam {
  404. JOINT_PARAM_BIAS,
  405. JOINT_PARAM_MAX_BIAS,
  406. JOINT_PARAM_MAX_FORCE,
  407. };
  408. virtual void joint_set_param(RID p_joint, JointParam p_param, real_t p_value) = 0;
  409. virtual real_t joint_get_param(RID p_joint, JointParam p_param) const = 0;
  410. virtual void joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable) = 0;
  411. virtual bool joint_is_disabled_collisions_between_bodies(RID p_joint) const = 0;
  412. virtual RID pin_joint_create(const Vector2 &p_anchor, RID p_body_a, RID p_body_b = RID()) = 0;
  413. 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;
  414. 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;
  415. enum PinJointParam {
  416. PIN_JOINT_SOFTNESS
  417. };
  418. virtual void pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) = 0;
  419. virtual real_t pin_joint_get_param(RID p_joint, PinJointParam p_param) const = 0;
  420. enum DampedStringParam {
  421. DAMPED_STRING_REST_LENGTH,
  422. DAMPED_STRING_STIFFNESS,
  423. DAMPED_STRING_DAMPING
  424. };
  425. virtual void damped_string_joint_set_param(RID p_joint, DampedStringParam p_param, real_t p_value) = 0;
  426. virtual real_t damped_string_joint_get_param(RID p_joint, DampedStringParam p_param) const = 0;
  427. virtual JointType joint_get_type(RID p_joint) const = 0;
  428. /* QUERY API */
  429. enum AreaBodyStatus {
  430. AREA_BODY_ADDED,
  431. AREA_BODY_REMOVED
  432. };
  433. /* MISC */
  434. virtual void free(RID p_rid) = 0;
  435. virtual void set_active(bool p_active) = 0;
  436. virtual void init() = 0;
  437. virtual void step(float p_step) = 0;
  438. virtual void sync() = 0;
  439. virtual void flush_queries() = 0;
  440. virtual void end_sync() = 0;
  441. virtual void finish() = 0;
  442. virtual bool is_flushing_queries() const = 0;
  443. enum ProcessInfo {
  444. INFO_ACTIVE_OBJECTS,
  445. INFO_COLLISION_PAIRS,
  446. INFO_ISLAND_COUNT
  447. };
  448. virtual int get_process_info(ProcessInfo p_info) = 0;
  449. Physics2DServer();
  450. ~Physics2DServer();
  451. };
  452. class Physics2DTestMotionResult : public Reference {
  453. GDCLASS(Physics2DTestMotionResult, Reference);
  454. Physics2DServer::MotionResult result;
  455. bool colliding;
  456. friend class Physics2DServer;
  457. protected:
  458. static void _bind_methods();
  459. public:
  460. Physics2DServer::MotionResult *get_result_ptr() const { return const_cast<Physics2DServer::MotionResult *>(&result); }
  461. //bool is_colliding() const;
  462. Vector2 get_motion() const;
  463. Vector2 get_motion_remainder() const;
  464. Vector2 get_collision_point() const;
  465. Vector2 get_collision_normal() const;
  466. Vector2 get_collider_velocity() const;
  467. ObjectID get_collider_id() const;
  468. RID get_collider_rid() const;
  469. Object *get_collider() const;
  470. int get_collider_shape() const;
  471. Physics2DTestMotionResult();
  472. };
  473. typedef Physics2DServer *(*CreatePhysics2DServerCallback)();
  474. class Physics2DServerManager {
  475. struct ClassInfo {
  476. String name;
  477. CreatePhysics2DServerCallback create_callback;
  478. ClassInfo() :
  479. name(""),
  480. create_callback(NULL) {}
  481. ClassInfo(String p_name, CreatePhysics2DServerCallback p_create_callback) :
  482. name(p_name),
  483. create_callback(p_create_callback) {}
  484. ClassInfo(const ClassInfo &p_ci) :
  485. name(p_ci.name),
  486. create_callback(p_ci.create_callback) {}
  487. ClassInfo operator=(const ClassInfo &p_ci) {
  488. name = p_ci.name;
  489. create_callback = p_ci.create_callback;
  490. return *this;
  491. }
  492. };
  493. static Vector<ClassInfo> physics_2d_servers;
  494. static int default_server_id;
  495. static int default_server_priority;
  496. public:
  497. static const String setting_property_name;
  498. private:
  499. static void on_servers_changed();
  500. public:
  501. static void register_server(const String &p_name, CreatePhysics2DServerCallback p_creat_callback);
  502. static void set_default_server(const String &p_name, int p_priority = 0);
  503. static int find_server_id(const String &p_name);
  504. static int get_servers_count();
  505. static String get_server_name(int p_id);
  506. static Physics2DServer *new_default_server();
  507. static Physics2DServer *new_server(const String &p_name);
  508. };
  509. VARIANT_ENUM_CAST(Physics2DServer::ShapeType);
  510. VARIANT_ENUM_CAST(Physics2DServer::SpaceParameter);
  511. VARIANT_ENUM_CAST(Physics2DServer::AreaParameter);
  512. VARIANT_ENUM_CAST(Physics2DServer::AreaSpaceOverrideMode);
  513. VARIANT_ENUM_CAST(Physics2DServer::BodyMode);
  514. VARIANT_ENUM_CAST(Physics2DServer::BodyParameter);
  515. VARIANT_ENUM_CAST(Physics2DServer::BodyState);
  516. VARIANT_ENUM_CAST(Physics2DServer::CCDMode);
  517. VARIANT_ENUM_CAST(Physics2DServer::JointParam);
  518. VARIANT_ENUM_CAST(Physics2DServer::JointType);
  519. VARIANT_ENUM_CAST(Physics2DServer::DampedStringParam);
  520. //VARIANT_ENUM_CAST( Physics2DServer::ObjectType );
  521. VARIANT_ENUM_CAST(Physics2DServer::AreaBodyStatus);
  522. VARIANT_ENUM_CAST(Physics2DServer::ProcessInfo);
  523. #endif