physics_2d_server.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 "object.h"
  33. #include "reference.h"
  34. #include "resource.h"
  35. class Physics2DDirectSpaceState;
  36. class Physics2DDirectBodyState : public Object {
  37. OBJ_TYPE(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 Matrix32 &p_transform) = 0;
  51. virtual Matrix32 get_transform() const = 0;
  52. virtual void set_sleep_state(bool p_enable) = 0;
  53. virtual bool is_sleeping() const = 0;
  54. virtual int get_contact_count() const = 0;
  55. virtual Vector2 get_contact_local_pos(int p_contact_idx) const = 0;
  56. virtual Vector2 get_contact_local_normal(int p_contact_idx) const = 0;
  57. virtual int get_contact_local_shape(int p_contact_idx) const = 0;
  58. virtual RID get_contact_collider(int p_contact_idx) const = 0;
  59. virtual Vector2 get_contact_collider_pos(int p_contact_idx) const = 0;
  60. virtual ObjectID get_contact_collider_id(int p_contact_idx) const = 0;
  61. virtual Object *get_contact_collider_object(int p_contact_idx) const;
  62. virtual int get_contact_collider_shape(int p_contact_idx) const = 0;
  63. virtual Variant get_contact_collider_shape_metadata(int p_contact_idx) const = 0;
  64. virtual Vector2 get_contact_collider_velocity_at_pos(int p_contact_idx) const = 0;
  65. virtual real_t get_step() const = 0;
  66. virtual void integrate_forces();
  67. virtual Physics2DDirectSpaceState *get_space_state() = 0;
  68. Physics2DDirectBodyState();
  69. };
  70. class Physics2DShapeQueryResult;
  71. //used for script
  72. class Physics2DShapeQueryParameters : public Reference {
  73. OBJ_TYPE(Physics2DShapeQueryParameters, Reference);
  74. friend class Physics2DDirectSpaceState;
  75. RID shape;
  76. Matrix32 transform;
  77. Vector2 motion;
  78. float margin;
  79. Set<RID> exclude;
  80. uint32_t layer_mask;
  81. uint32_t object_type_mask;
  82. protected:
  83. static void _bind_methods();
  84. public:
  85. void set_shape(const RES &p_shape);
  86. void set_shape_rid(const RID &p_shape);
  87. RID get_shape_rid() const;
  88. void set_transform(const Matrix32 &p_transform);
  89. Matrix32 get_transform() const;
  90. void set_motion(const Vector2 &p_motion);
  91. Vector2 get_motion() const;
  92. void set_margin(float p_margin);
  93. float get_margin() const;
  94. void set_layer_mask(int p_layer_mask);
  95. int get_layer_mask() const;
  96. void set_object_type_mask(int p_object_type_mask);
  97. int get_object_type_mask() const;
  98. void set_exclude(const Vector<RID> &p_exclude);
  99. Vector<RID> get_exclude() const;
  100. Physics2DShapeQueryParameters();
  101. };
  102. class Physics2DDirectSpaceState : public Object {
  103. OBJ_TYPE(Physics2DDirectSpaceState, Object);
  104. Dictionary _intersect_ray(const Vector2 &p_from, const Vector2 &p_to, const Vector<RID> &p_exclude = Vector<RID>(), uint32_t p_layers = 0, uint32_t p_object_type_mask = TYPE_MASK_COLLISION);
  105. Array _intersect_point(const Vector2 &p_point, int p_max_results = 32, const Vector<RID> &p_exclude = Vector<RID>(), uint32_t p_layers = 0, uint32_t p_object_type_mask = TYPE_MASK_COLLISION);
  106. Array _intersect_shape(const Ref<Physics2DShapeQueryParameters> &p_shape_query, int p_max_results = 32);
  107. Array _cast_motion(const Ref<Physics2DShapeQueryParameters> &p_shape_query);
  108. Array _collide_shape(const Ref<Physics2DShapeQueryParameters> &p_shape_query, int p_max_results = 32);
  109. Dictionary _get_rest_info(const Ref<Physics2DShapeQueryParameters> &p_shape_query);
  110. protected:
  111. static void _bind_methods();
  112. public:
  113. enum ObjectTypeMask {
  114. TYPE_MASK_STATIC_BODY = 1 << 0,
  115. TYPE_MASK_KINEMATIC_BODY = 1 << 1,
  116. TYPE_MASK_RIGID_BODY = 1 << 2,
  117. TYPE_MASK_CHARACTER_BODY = 1 << 3,
  118. TYPE_MASK_AREA = 1 << 4,
  119. TYPE_MASK_COLLISION = TYPE_MASK_STATIC_BODY | TYPE_MASK_CHARACTER_BODY | TYPE_MASK_KINEMATIC_BODY | TYPE_MASK_RIGID_BODY
  120. };
  121. struct RayResult {
  122. Vector2 position;
  123. Vector2 normal;
  124. RID rid;
  125. ObjectID collider_id;
  126. Object *collider;
  127. int shape;
  128. Variant metadata;
  129. };
  130. 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_layer_mask = 0xFFFFFFFF, uint32_t p_object_type_mask = TYPE_MASK_COLLISION) = 0;
  131. struct ShapeResult {
  132. RID rid;
  133. ObjectID collider_id;
  134. Object *collider;
  135. int shape;
  136. Variant metadata;
  137. };
  138. 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_layer_mask = 0xFFFFFFFF, uint32_t p_object_type_mask = TYPE_MASK_COLLISION, bool p_pick_point = false) = 0;
  139. virtual int intersect_shape(const RID &p_shape, const Matrix32 &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_layer_mask = 0xFFFFFFFF, uint32_t p_object_type_mask = TYPE_MASK_COLLISION) = 0;
  140. virtual bool cast_motion(const RID &p_shape, const Matrix32 &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_layer_mask = 0xFFFFFFFF, uint32_t p_object_type_mask = TYPE_MASK_COLLISION) = 0;
  141. virtual bool collide_shape(RID p_shape, const Matrix32 &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_layer_mask = 0xFFFFFFFF, uint32_t p_object_type_mask = TYPE_MASK_COLLISION) = 0;
  142. struct ShapeRestInfo {
  143. Vector2 point;
  144. Vector2 normal;
  145. RID rid;
  146. ObjectID collider_id;
  147. int shape;
  148. Vector2 linear_velocity; //velocity at contact point
  149. Variant metadata;
  150. };
  151. virtual bool rest_info(RID p_shape, const Matrix32 &p_shape_xform, const Vector2 &p_motion, float p_margin, ShapeRestInfo *r_info, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_layer_mask = 0xFFFFFFFF, uint32_t p_object_type_mask = TYPE_MASK_COLLISION) = 0;
  152. Physics2DDirectSpaceState();
  153. };
  154. class Physics2DShapeQueryResult : public Reference {
  155. OBJ_TYPE(Physics2DShapeQueryResult, Reference);
  156. Vector<Physics2DDirectSpaceState::ShapeResult> result;
  157. friend class Physics2DDirectSpaceState;
  158. protected:
  159. static void _bind_methods();
  160. public:
  161. int get_result_count() const;
  162. RID get_result_rid(int p_idx) const;
  163. ObjectID get_result_object_id(int p_idx) const;
  164. Object *get_result_object(int p_idx) const;
  165. int get_result_object_shape(int p_idx) const;
  166. Physics2DShapeQueryResult();
  167. };
  168. class Physics2DTestMotionResult;
  169. class Physics2DServer : public Object {
  170. OBJ_TYPE(Physics2DServer, Object);
  171. static Physics2DServer *singleton;
  172. virtual bool _body_test_motion(RID p_body, const Vector2 &p_motion, float p_margin = 0.08, const Ref<Physics2DTestMotionResult> &p_result = Ref<Physics2DTestMotionResult>());
  173. virtual bool _body_test_motion_from(RID p_body, const Matrix32 &p_from, const Vector2 &p_motion, float p_margin = 0.08, const Ref<Physics2DTestMotionResult> &p_result = Ref<Physics2DTestMotionResult>());
  174. protected:
  175. static void _bind_methods();
  176. public:
  177. static Physics2DServer *get_singleton();
  178. enum ShapeType {
  179. SHAPE_LINE, ///< plane:"plane"
  180. SHAPE_RAY, ///< float:"length"
  181. SHAPE_SEGMENT, ///< float:"length"
  182. SHAPE_CIRCLE, ///< float:"radius"
  183. SHAPE_RECTANGLE, ///< vec3:"extents"
  184. SHAPE_CAPSULE,
  185. SHAPE_CONVEX_POLYGON, ///< array of planes:"planes"
  186. SHAPE_CONCAVE_POLYGON, ///< Vector2 array:"triangles" , or Dictionary with "indices" (int array) and "triangles" (Vector2 array)
  187. SHAPE_CUSTOM, ///< Server-Implementation based custom shape, calling shape_create() with this value will result in an error
  188. };
  189. virtual RID shape_create(ShapeType p_shape) = 0;
  190. virtual void shape_set_data(RID p_shape, const Variant &p_data) = 0;
  191. virtual void shape_set_custom_solver_bias(RID p_shape, real_t p_bias) = 0;
  192. virtual ShapeType shape_get_type(RID p_shape) const = 0;
  193. virtual Variant shape_get_data(RID p_shape) const = 0;
  194. virtual real_t shape_get_custom_solver_bias(RID p_shape) const = 0;
  195. //these work well, but should be used from the main thread only
  196. virtual bool shape_collide(RID p_shape_A, const Matrix32 &p_xform_A, const Vector2 &p_motion_A, RID p_shape_B, const Matrix32 &p_xform_B, const Vector2 &p_motion_B, Vector2 *r_results, int p_result_max, int &r_result_count) = 0;
  197. /* SPACE API */
  198. virtual RID space_create() = 0;
  199. virtual void space_set_active(RID p_space, bool p_active) = 0;
  200. virtual bool space_is_active(RID p_space) const = 0;
  201. enum SpaceParameter {
  202. SPACE_PARAM_CONTACT_RECYCLE_RADIUS,
  203. SPACE_PARAM_CONTACT_MAX_SEPARATION,
  204. SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION,
  205. SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_TRESHOLD,
  206. SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_TRESHOLD,
  207. SPACE_PARAM_BODY_TIME_TO_SLEEP,
  208. SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS,
  209. };
  210. virtual void space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) = 0;
  211. virtual real_t space_get_param(RID p_space, SpaceParameter p_param) const = 0;
  212. // this function only works on fixed process, errors and returns null otherwise
  213. virtual Physics2DDirectSpaceState *space_get_direct_state(RID p_space) = 0;
  214. virtual void space_set_debug_contacts(RID p_space, int p_max_contacts) = 0;
  215. virtual Vector<Vector2> space_get_contacts(RID p_space) const = 0;
  216. virtual int space_get_contact_count(RID p_space) const = 0;
  217. //missing space parameters
  218. /* AREA API */
  219. //missing attenuation? missing better override?
  220. enum AreaParameter {
  221. AREA_PARAM_GRAVITY,
  222. AREA_PARAM_GRAVITY_VECTOR,
  223. AREA_PARAM_GRAVITY_IS_POINT,
  224. AREA_PARAM_GRAVITY_DISTANCE_SCALE,
  225. AREA_PARAM_GRAVITY_POINT_ATTENUATION,
  226. AREA_PARAM_LINEAR_DAMP,
  227. AREA_PARAM_ANGULAR_DAMP,
  228. AREA_PARAM_PRIORITY
  229. };
  230. virtual RID area_create() = 0;
  231. virtual void area_set_space(RID p_area, RID p_space) = 0;
  232. virtual RID area_get_space(RID p_area) const = 0;
  233. enum AreaSpaceOverrideMode {
  234. AREA_SPACE_OVERRIDE_DISABLED,
  235. AREA_SPACE_OVERRIDE_COMBINE,
  236. AREA_SPACE_OVERRIDE_COMBINE_REPLACE, // Combines, then discards all subsequent calculations
  237. AREA_SPACE_OVERRIDE_REPLACE,
  238. AREA_SPACE_OVERRIDE_REPLACE_COMBINE // Discards all previous calculations, then keeps combining
  239. };
  240. virtual void area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode) = 0;
  241. virtual AreaSpaceOverrideMode area_get_space_override_mode(RID p_area) const = 0;
  242. virtual void area_add_shape(RID p_area, RID p_shape, const Matrix32 &p_transform = Matrix32()) = 0;
  243. virtual void area_set_shape(RID p_area, int p_shape_idx, RID p_shape) = 0;
  244. virtual void area_set_shape_transform(RID p_area, int p_shape_idx, const Matrix32 &p_transform) = 0;
  245. virtual int area_get_shape_count(RID p_area) const = 0;
  246. virtual RID area_get_shape(RID p_area, int p_shape_idx) const = 0;
  247. virtual Matrix32 area_get_shape_transform(RID p_area, int p_shape_idx) const = 0;
  248. virtual void area_remove_shape(RID p_area, int p_shape_idx) = 0;
  249. virtual void area_clear_shapes(RID p_area) = 0;
  250. virtual void area_attach_object_instance_ID(RID p_area, ObjectID p_ID) = 0;
  251. virtual ObjectID area_get_object_instance_ID(RID p_area) const = 0;
  252. virtual void area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) = 0;
  253. virtual void area_set_transform(RID p_area, const Matrix32 &p_transform) = 0;
  254. virtual Variant area_get_param(RID p_parea, AreaParameter p_param) const = 0;
  255. virtual Matrix32 area_get_transform(RID p_area) const = 0;
  256. virtual void area_set_collision_mask(RID p_area, uint32_t p_mask) = 0;
  257. virtual void area_set_layer_mask(RID p_area, uint32_t p_mask) = 0;
  258. virtual void area_set_monitorable(RID p_area, bool p_monitorable) = 0;
  259. virtual void area_set_pickable(RID p_area, bool p_pickable) = 0;
  260. virtual void area_set_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) = 0;
  261. virtual void area_set_area_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) = 0;
  262. /* BODY API */
  263. //missing ccd?
  264. enum BodyMode {
  265. BODY_MODE_STATIC,
  266. BODY_MODE_KINEMATIC,
  267. BODY_MODE_RIGID,
  268. BODY_MODE_CHARACTER
  269. //BODY_MODE_SOFT ??
  270. };
  271. virtual RID body_create(BodyMode p_mode = BODY_MODE_RIGID, bool p_init_sleeping = false) = 0;
  272. virtual void body_set_space(RID p_body, RID p_space) = 0;
  273. virtual RID body_get_space(RID p_body) const = 0;
  274. virtual void body_set_mode(RID p_body, BodyMode p_mode) = 0;
  275. virtual BodyMode body_get_mode(RID p_body) const = 0;
  276. virtual void body_add_shape(RID p_body, RID p_shape, const Matrix32 &p_transform = Matrix32()) = 0;
  277. virtual void body_set_shape(RID p_body, int p_shape_idx, RID p_shape) = 0;
  278. virtual void body_set_shape_transform(RID p_body, int p_shape_idx, const Matrix32 &p_transform) = 0;
  279. virtual void body_set_shape_metadata(RID p_body, int p_shape_idx, const Variant &p_metadata) = 0;
  280. virtual int body_get_shape_count(RID p_body) const = 0;
  281. virtual RID body_get_shape(RID p_body, int p_shape_idx) const = 0;
  282. virtual Matrix32 body_get_shape_transform(RID p_body, int p_shape_idx) const = 0;
  283. virtual Variant body_get_shape_metadata(RID p_body, int p_shape_idx) const = 0;
  284. virtual void body_set_shape_as_trigger(RID p_body, int p_shape_idx, bool p_enable) = 0;
  285. virtual bool body_is_shape_set_as_trigger(RID p_body, int p_shape_idx) const = 0;
  286. virtual void body_remove_shape(RID p_body, int p_shape_idx) = 0;
  287. virtual void body_clear_shapes(RID p_body) = 0;
  288. virtual void body_attach_object_instance_ID(RID p_body, uint32_t p_ID) = 0;
  289. virtual uint32_t body_get_object_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_layer_mask(RID p_body, uint32_t p_mask) = 0;
  298. virtual uint32_t body_get_layer_mask(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_force(RID p_body, const Vector2 &p_offset, const Vector2 &p_force) = 0;
  330. virtual void body_apply_impulse(RID p_body, const Vector2 &p_offset, const Vector2 &p_impulse) = 0;
  331. virtual void body_set_axis_velocity(RID p_body, const Vector2 &p_axis_velocity) = 0;
  332. //fix
  333. virtual void body_add_collision_exception(RID p_body, RID p_body_b) = 0;
  334. virtual void body_remove_collision_exception(RID p_body, RID p_body_b) = 0;
  335. virtual void body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) = 0;
  336. virtual void body_set_max_contacts_reported(RID p_body, int p_contacts) = 0;
  337. virtual int body_get_max_contacts_reported(RID p_body) const = 0;
  338. virtual void body_set_one_way_collision_direction(RID p_body, const Vector2 &p_direction) = 0;
  339. virtual Vector2 body_get_one_way_collision_direction(RID p_body) const = 0;
  340. virtual void body_set_one_way_collision_max_depth(RID p_body, float p_max_depth) = 0;
  341. virtual float body_get_one_way_collision_max_depth(RID p_body) const = 0;
  342. //missing remove
  343. virtual void body_set_contacts_reported_depth_treshold(RID p_body, float p_treshold) = 0;
  344. virtual float body_get_contacts_reported_depth_treshold(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 Matrix32 &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. struct MotionResult {
  351. Vector2 motion;
  352. Vector2 remainder;
  353. Vector2 collision_point;
  354. Vector2 collision_normal;
  355. Vector2 collider_velocity;
  356. ObjectID collider_id;
  357. RID collider;
  358. int collider_shape;
  359. Variant collider_metadata;
  360. };
  361. virtual bool body_test_motion(RID p_body, const Vector2 &p_motion, float p_margin = 0.001, MotionResult *r_result = NULL) = 0;
  362. virtual bool body_test_motion_from(RID p_body, const Matrix32 &p_from, const Vector2 &p_motion, float p_margin = 0.001, MotionResult *r_result = NULL) = 0;
  363. /* JOINT API */
  364. enum JointType {
  365. JOINT_PIN,
  366. JOINT_GROOVE,
  367. JOINT_DAMPED_SPRING
  368. };
  369. enum JointParam {
  370. JOINT_PARAM_BIAS,
  371. JOINT_PARAM_MAX_BIAS,
  372. JOINT_PARAM_MAX_FORCE,
  373. };
  374. virtual void joint_set_param(RID p_joint, JointParam p_param, real_t p_value) = 0;
  375. virtual real_t joint_get_param(RID p_joint, JointParam p_param) const = 0;
  376. virtual RID pin_joint_create(const Vector2 &p_anchor, RID p_body_a, RID p_body_b = RID()) = 0;
  377. 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;
  378. 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;
  379. enum PinJointParam {
  380. PIN_JOINT_SOFTNESS
  381. };
  382. virtual void pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) = 0;
  383. virtual real_t pin_joint_get_param(RID p_joint, PinJointParam p_param) const = 0;
  384. enum DampedStringParam {
  385. DAMPED_STRING_REST_LENGTH,
  386. DAMPED_STRING_STIFFNESS,
  387. DAMPED_STRING_DAMPING
  388. };
  389. virtual void damped_string_joint_set_param(RID p_joint, DampedStringParam p_param, real_t p_value) = 0;
  390. virtual real_t damped_string_joint_get_param(RID p_joint, DampedStringParam p_param) const = 0;
  391. virtual JointType joint_get_type(RID p_joint) const = 0;
  392. /* QUERY API */
  393. enum AreaBodyStatus {
  394. AREA_BODY_ADDED,
  395. AREA_BODY_REMOVED
  396. };
  397. /* MISC */
  398. virtual void free(RID p_rid) = 0;
  399. virtual void set_active(bool p_active) = 0;
  400. virtual void init() = 0;
  401. virtual void step(float p_step) = 0;
  402. virtual void sync() = 0;
  403. virtual void flush_queries() = 0;
  404. virtual void end_sync() = 0;
  405. virtual void finish() = 0;
  406. enum ProcessInfo {
  407. INFO_ACTIVE_OBJECTS,
  408. INFO_COLLISION_PAIRS,
  409. INFO_ISLAND_COUNT,
  410. INFO_STEP_TIME,
  411. INFO_BROAD_PHASE_TIME
  412. };
  413. virtual int get_process_info(ProcessInfo p_info) = 0;
  414. Physics2DServer();
  415. ~Physics2DServer();
  416. };
  417. class Physics2DTestMotionResult : public Reference {
  418. OBJ_TYPE(Physics2DTestMotionResult, Reference);
  419. Physics2DServer::MotionResult result;
  420. bool colliding;
  421. friend class Physics2DServer;
  422. protected:
  423. static void _bind_methods();
  424. public:
  425. Physics2DServer::MotionResult *get_result_ptr() const { return const_cast<Physics2DServer::MotionResult *>(&result); }
  426. //bool is_colliding() const;
  427. Vector2 get_motion() const;
  428. Vector2 get_motion_remainder() const;
  429. Vector2 get_collision_point() const;
  430. Vector2 get_collision_normal() const;
  431. Vector2 get_collider_velocity() const;
  432. ObjectID get_collider_id() const;
  433. RID get_collider_rid() const;
  434. Object *get_collider() const;
  435. int get_collider_shape() const;
  436. Physics2DTestMotionResult();
  437. };
  438. VARIANT_ENUM_CAST(Physics2DServer::ShapeType);
  439. VARIANT_ENUM_CAST(Physics2DServer::SpaceParameter);
  440. VARIANT_ENUM_CAST(Physics2DServer::AreaParameter);
  441. VARIANT_ENUM_CAST(Physics2DServer::AreaSpaceOverrideMode);
  442. VARIANT_ENUM_CAST(Physics2DServer::BodyMode);
  443. VARIANT_ENUM_CAST(Physics2DServer::BodyParameter);
  444. VARIANT_ENUM_CAST(Physics2DServer::BodyState);
  445. VARIANT_ENUM_CAST(Physics2DServer::CCDMode);
  446. VARIANT_ENUM_CAST(Physics2DServer::JointParam);
  447. VARIANT_ENUM_CAST(Physics2DServer::JointType);
  448. VARIANT_ENUM_CAST(Physics2DServer::DampedStringParam);
  449. //VARIANT_ENUM_CAST( Physics2DServer::ObjectType );
  450. VARIANT_ENUM_CAST(Physics2DServer::AreaBodyStatus);
  451. VARIANT_ENUM_CAST(Physics2DServer::ProcessInfo);
  452. #endif