physics_server_2d.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /**************************************************************************/
  2. /* physics_server_2d.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_SERVER_2D_H
  31. #define PHYSICS_SERVER_2D_H
  32. #include "core/io/resource.h"
  33. #include "core/object/class_db.h"
  34. #include "core/object/ref_counted.h"
  35. class PhysicsDirectSpaceState2D;
  36. template <typename T>
  37. class TypedArray;
  38. class PhysicsDirectBodyState2D : public Object {
  39. GDCLASS(PhysicsDirectBodyState2D, Object);
  40. protected:
  41. static void _bind_methods();
  42. public:
  43. virtual Vector2 get_total_gravity() const = 0; // get gravity vector working on this body space/area
  44. virtual real_t get_total_linear_damp() const = 0; // get density of this body space/area
  45. virtual real_t get_total_angular_damp() const = 0; // get density of this body space/area
  46. virtual Vector2 get_center_of_mass() const = 0;
  47. virtual Vector2 get_center_of_mass_local() const = 0;
  48. virtual real_t get_inverse_mass() const = 0; // get the mass
  49. virtual real_t get_inverse_inertia() const = 0; // get density of this body space
  50. virtual void set_linear_velocity(const Vector2 &p_velocity) = 0;
  51. virtual Vector2 get_linear_velocity() const = 0;
  52. virtual void set_angular_velocity(real_t p_velocity) = 0;
  53. virtual real_t get_angular_velocity() const = 0;
  54. virtual void set_transform(const Transform2D &p_transform) = 0;
  55. virtual Transform2D get_transform() const = 0;
  56. virtual Vector2 get_velocity_at_local_position(const Vector2 &p_position) const = 0;
  57. virtual void apply_central_impulse(const Vector2 &p_impulse) = 0;
  58. virtual void apply_torque_impulse(real_t p_torque) = 0;
  59. virtual void apply_impulse(const Vector2 &p_impulse, const Vector2 &p_position = Vector2()) = 0;
  60. virtual void apply_central_force(const Vector2 &p_force) = 0;
  61. virtual void apply_force(const Vector2 &p_force, const Vector2 &p_position = Vector2()) = 0;
  62. virtual void apply_torque(real_t p_torque) = 0;
  63. virtual void add_constant_central_force(const Vector2 &p_force) = 0;
  64. virtual void add_constant_force(const Vector2 &p_force, const Vector2 &p_position = Vector2()) = 0;
  65. virtual void add_constant_torque(real_t p_torque) = 0;
  66. virtual void set_constant_force(const Vector2 &p_force) = 0;
  67. virtual Vector2 get_constant_force() const = 0;
  68. virtual void set_constant_torque(real_t p_torque) = 0;
  69. virtual real_t get_constant_torque() const = 0;
  70. virtual void set_sleep_state(bool p_enable) = 0;
  71. virtual bool is_sleeping() const = 0;
  72. virtual int get_contact_count() const = 0;
  73. virtual Vector2 get_contact_local_position(int p_contact_idx) const = 0;
  74. virtual Vector2 get_contact_local_normal(int p_contact_idx) const = 0;
  75. virtual int get_contact_local_shape(int p_contact_idx) const = 0;
  76. virtual RID get_contact_collider(int p_contact_idx) const = 0;
  77. virtual Vector2 get_contact_collider_position(int p_contact_idx) const = 0;
  78. virtual ObjectID get_contact_collider_id(int p_contact_idx) const = 0;
  79. virtual Object *get_contact_collider_object(int p_contact_idx) const;
  80. virtual int get_contact_collider_shape(int p_contact_idx) const = 0;
  81. virtual Vector2 get_contact_collider_velocity_at_position(int p_contact_idx) const = 0;
  82. virtual Vector2 get_contact_impulse(int p_contact_idx) const = 0;
  83. virtual real_t get_step() const = 0;
  84. virtual void integrate_forces();
  85. virtual PhysicsDirectSpaceState2D *get_space_state() = 0;
  86. PhysicsDirectBodyState2D();
  87. };
  88. class PhysicsRayQueryParameters2D;
  89. class PhysicsPointQueryParameters2D;
  90. class PhysicsShapeQueryParameters2D;
  91. class PhysicsDirectSpaceState2D : public Object {
  92. GDCLASS(PhysicsDirectSpaceState2D, Object);
  93. Dictionary _intersect_ray(const Ref<PhysicsRayQueryParameters2D> &p_ray_query);
  94. TypedArray<Dictionary> _intersect_point(const Ref<PhysicsPointQueryParameters2D> &p_point_query, int p_max_results = 32);
  95. TypedArray<Dictionary> _intersect_shape(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query, int p_max_results = 32);
  96. Vector<real_t> _cast_motion(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query);
  97. TypedArray<Vector2> _collide_shape(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query, int p_max_results = 32);
  98. Dictionary _get_rest_info(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query);
  99. protected:
  100. static void _bind_methods();
  101. public:
  102. struct RayParameters {
  103. Vector2 from;
  104. Vector2 to;
  105. HashSet<RID> exclude;
  106. uint32_t collision_mask = UINT32_MAX;
  107. bool collide_with_bodies = true;
  108. bool collide_with_areas = false;
  109. bool hit_from_inside = false;
  110. };
  111. struct RayResult {
  112. Vector2 position;
  113. Vector2 normal;
  114. RID rid;
  115. ObjectID collider_id;
  116. Object *collider = nullptr;
  117. int shape = 0;
  118. };
  119. virtual bool intersect_ray(const RayParameters &p_parameters, RayResult &r_result) = 0;
  120. struct ShapeResult {
  121. RID rid;
  122. ObjectID collider_id;
  123. Object *collider = nullptr;
  124. int shape = 0;
  125. };
  126. struct PointParameters {
  127. Vector2 position;
  128. ObjectID canvas_instance_id;
  129. HashSet<RID> exclude;
  130. uint32_t collision_mask = UINT32_MAX;
  131. bool collide_with_bodies = true;
  132. bool collide_with_areas = false;
  133. bool pick_point = false;
  134. };
  135. virtual int intersect_point(const PointParameters &p_parameters, ShapeResult *r_results, int p_result_max) = 0;
  136. struct ShapeParameters {
  137. RID shape_rid;
  138. Transform2D transform;
  139. Vector2 motion;
  140. real_t margin = 0.0;
  141. HashSet<RID> exclude;
  142. uint32_t collision_mask = UINT32_MAX;
  143. bool collide_with_bodies = true;
  144. bool collide_with_areas = false;
  145. };
  146. struct ShapeRestInfo {
  147. Vector2 point;
  148. Vector2 normal;
  149. RID rid;
  150. ObjectID collider_id;
  151. int shape = 0;
  152. Vector2 linear_velocity; // Velocity at contact point.
  153. };
  154. virtual int intersect_shape(const ShapeParameters &p_parameters, ShapeResult *r_results, int p_result_max) = 0;
  155. virtual bool cast_motion(const ShapeParameters &p_parameters, real_t &p_closest_safe, real_t &p_closest_unsafe) = 0;
  156. virtual bool collide_shape(const ShapeParameters &p_parameters, Vector2 *r_results, int p_result_max, int &r_result_count) = 0;
  157. virtual bool rest_info(const ShapeParameters &p_parameters, ShapeRestInfo *r_info) = 0;
  158. PhysicsDirectSpaceState2D();
  159. };
  160. class PhysicsTestMotionParameters2D;
  161. class PhysicsTestMotionResult2D;
  162. class PhysicsServer2D : public Object {
  163. GDCLASS(PhysicsServer2D, Object);
  164. static PhysicsServer2D *singleton;
  165. virtual bool _body_test_motion(RID p_body, const Ref<PhysicsTestMotionParameters2D> &p_parameters, const Ref<PhysicsTestMotionResult2D> &p_result = Ref<PhysicsTestMotionResult2D>());
  166. protected:
  167. static void _bind_methods();
  168. public:
  169. static PhysicsServer2D *get_singleton();
  170. enum ShapeType {
  171. SHAPE_WORLD_BOUNDARY, ///< plane:"plane"
  172. SHAPE_SEPARATION_RAY, ///< float:"length"
  173. SHAPE_SEGMENT, ///< float:"length"
  174. SHAPE_CIRCLE, ///< float:"radius"
  175. SHAPE_RECTANGLE, ///< vec3:"extents"
  176. SHAPE_CAPSULE,
  177. SHAPE_CONVEX_POLYGON, ///< array of planes:"planes"
  178. SHAPE_CONCAVE_POLYGON, ///< Vector2 array:"triangles" , or Dictionary with "indices" (int array) and "triangles" (Vector2 array)
  179. SHAPE_CUSTOM, ///< Server-Implementation based custom shape, calling shape_create() with this value will result in an error
  180. };
  181. virtual RID world_boundary_shape_create() = 0;
  182. virtual RID separation_ray_shape_create() = 0;
  183. virtual RID segment_shape_create() = 0;
  184. virtual RID circle_shape_create() = 0;
  185. virtual RID rectangle_shape_create() = 0;
  186. virtual RID capsule_shape_create() = 0;
  187. virtual RID convex_polygon_shape_create() = 0;
  188. virtual RID concave_polygon_shape_create() = 0;
  189. virtual void shape_set_data(RID p_shape, const Variant &p_data) = 0;
  190. virtual void shape_set_custom_solver_bias(RID p_shape, real_t p_bias) = 0;
  191. virtual ShapeType shape_get_type(RID p_shape) const = 0;
  192. virtual Variant shape_get_data(RID p_shape) const = 0;
  193. virtual real_t shape_get_custom_solver_bias(RID p_shape) const = 0;
  194. //these work well, but should be used from the main thread only
  195. 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;
  196. /* SPACE API */
  197. virtual RID space_create() = 0;
  198. virtual void space_set_active(RID p_space, bool p_active) = 0;
  199. virtual bool space_is_active(RID p_space) const = 0;
  200. enum SpaceParameter {
  201. SPACE_PARAM_CONTACT_RECYCLE_RADIUS,
  202. SPACE_PARAM_CONTACT_MAX_SEPARATION,
  203. SPACE_PARAM_CONTACT_MAX_ALLOWED_PENETRATION,
  204. SPACE_PARAM_CONTACT_DEFAULT_BIAS,
  205. SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD,
  206. SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD,
  207. SPACE_PARAM_BODY_TIME_TO_SLEEP,
  208. SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS,
  209. SPACE_PARAM_SOLVER_ITERATIONS,
  210. };
  211. virtual void space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) = 0;
  212. virtual real_t space_get_param(RID p_space, SpaceParameter p_param) const = 0;
  213. // this function only works on physics process, errors and returns null otherwise
  214. virtual PhysicsDirectSpaceState2D *space_get_direct_state(RID p_space) = 0;
  215. virtual void space_set_debug_contacts(RID p_space, int p_max_contacts) = 0;
  216. virtual Vector<Vector2> space_get_contacts(RID p_space) const = 0;
  217. virtual int space_get_contact_count(RID p_space) const = 0;
  218. //missing space parameters
  219. /* AREA API */
  220. //missing attenuation? missing better override?
  221. enum AreaParameter {
  222. AREA_PARAM_GRAVITY_OVERRIDE_MODE,
  223. AREA_PARAM_GRAVITY,
  224. AREA_PARAM_GRAVITY_VECTOR,
  225. AREA_PARAM_GRAVITY_IS_POINT,
  226. AREA_PARAM_GRAVITY_POINT_UNIT_DISTANCE,
  227. AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE,
  228. AREA_PARAM_LINEAR_DAMP,
  229. AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE,
  230. AREA_PARAM_ANGULAR_DAMP,
  231. AREA_PARAM_PRIORITY
  232. };
  233. virtual RID area_create() = 0;
  234. virtual void area_set_space(RID p_area, RID p_space) = 0;
  235. virtual RID area_get_space(RID p_area) const = 0;
  236. enum AreaSpaceOverrideMode {
  237. AREA_SPACE_OVERRIDE_DISABLED,
  238. AREA_SPACE_OVERRIDE_COMBINE,
  239. AREA_SPACE_OVERRIDE_COMBINE_REPLACE, // Combines, then discards all subsequent calculations
  240. AREA_SPACE_OVERRIDE_REPLACE,
  241. AREA_SPACE_OVERRIDE_REPLACE_COMBINE // Discards all previous calculations, then keeps combining
  242. };
  243. virtual void area_add_shape(RID p_area, RID p_shape, const Transform2D &p_transform = Transform2D(), bool p_disabled = false) = 0;
  244. virtual void area_set_shape(RID p_area, int p_shape_idx, RID p_shape) = 0;
  245. virtual void area_set_shape_transform(RID p_area, int p_shape_idx, const Transform2D &p_transform) = 0;
  246. virtual int area_get_shape_count(RID p_area) const = 0;
  247. virtual RID area_get_shape(RID p_area, int p_shape_idx) const = 0;
  248. virtual Transform2D area_get_shape_transform(RID p_area, int p_shape_idx) const = 0;
  249. virtual void area_remove_shape(RID p_area, int p_shape_idx) = 0;
  250. virtual void area_clear_shapes(RID p_area) = 0;
  251. virtual void area_set_shape_disabled(RID p_area, int p_shape, bool p_disabled) = 0;
  252. virtual void area_attach_object_instance_id(RID p_area, ObjectID p_id) = 0;
  253. virtual ObjectID area_get_object_instance_id(RID p_area) const = 0;
  254. virtual void area_attach_canvas_instance_id(RID p_area, ObjectID p_id) = 0;
  255. virtual ObjectID area_get_canvas_instance_id(RID p_area) const = 0;
  256. virtual void area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) = 0;
  257. virtual void area_set_transform(RID p_area, const Transform2D &p_transform) = 0;
  258. virtual Variant area_get_param(RID p_parea, AreaParameter p_param) const = 0;
  259. virtual Transform2D area_get_transform(RID p_area) const = 0;
  260. virtual void area_set_collision_layer(RID p_area, uint32_t p_layer) = 0;
  261. virtual uint32_t area_get_collision_layer(RID p_area) const = 0;
  262. virtual void area_set_collision_mask(RID p_area, uint32_t p_mask) = 0;
  263. virtual uint32_t area_get_collision_mask(RID p_area) const = 0;
  264. virtual void area_set_monitorable(RID p_area, bool p_monitorable) = 0;
  265. virtual void area_set_pickable(RID p_area, bool p_pickable) = 0;
  266. virtual void area_set_monitor_callback(RID p_area, const Callable &p_callback) = 0;
  267. virtual void area_set_area_monitor_callback(RID p_area, const Callable &p_callback) = 0;
  268. /* BODY API */
  269. //missing ccd?
  270. enum BodyMode {
  271. BODY_MODE_STATIC,
  272. BODY_MODE_KINEMATIC,
  273. BODY_MODE_RIGID,
  274. BODY_MODE_RIGID_LINEAR,
  275. };
  276. virtual RID body_create() = 0;
  277. virtual void body_set_space(RID p_body, RID p_space) = 0;
  278. virtual RID body_get_space(RID p_body) const = 0;
  279. virtual void body_set_mode(RID p_body, BodyMode p_mode) = 0;
  280. virtual BodyMode body_get_mode(RID p_body) const = 0;
  281. virtual void body_add_shape(RID p_body, RID p_shape, const Transform2D &p_transform = Transform2D(), bool p_disabled = false) = 0;
  282. virtual void body_set_shape(RID p_body, int p_shape_idx, RID p_shape) = 0;
  283. virtual void body_set_shape_transform(RID p_body, int p_shape_idx, const Transform2D &p_transform) = 0;
  284. virtual int body_get_shape_count(RID p_body) const = 0;
  285. virtual RID body_get_shape(RID p_body, int p_shape_idx) const = 0;
  286. virtual Transform2D body_get_shape_transform(RID p_body, int p_shape_idx) const = 0;
  287. virtual void body_set_shape_disabled(RID p_body, int p_shape, bool p_disabled) = 0;
  288. virtual void body_set_shape_as_one_way_collision(RID p_body, int p_shape, bool p_enabled, real_t p_margin = 0) = 0;
  289. virtual void body_remove_shape(RID p_body, int p_shape_idx) = 0;
  290. virtual void body_clear_shapes(RID p_body) = 0;
  291. virtual void body_attach_object_instance_id(RID p_body, ObjectID p_id) = 0;
  292. virtual ObjectID body_get_object_instance_id(RID p_body) const = 0;
  293. virtual void body_attach_canvas_instance_id(RID p_body, ObjectID p_id) = 0;
  294. virtual ObjectID body_get_canvas_instance_id(RID p_body) const = 0;
  295. enum CCDMode {
  296. CCD_MODE_DISABLED,
  297. CCD_MODE_CAST_RAY,
  298. CCD_MODE_CAST_SHAPE,
  299. };
  300. virtual void body_set_continuous_collision_detection_mode(RID p_body, CCDMode p_mode) = 0;
  301. virtual CCDMode body_get_continuous_collision_detection_mode(RID p_body) const = 0;
  302. virtual void body_set_collision_layer(RID p_body, uint32_t p_layer) = 0;
  303. virtual uint32_t body_get_collision_layer(RID p_body) const = 0;
  304. virtual void body_set_collision_mask(RID p_body, uint32_t p_mask) = 0;
  305. virtual uint32_t body_get_collision_mask(RID p_body) const = 0;
  306. virtual void body_set_collision_priority(RID p_body, real_t p_priority) = 0;
  307. virtual real_t body_get_collision_priority(RID p_body) const = 0;
  308. // common body variables
  309. enum BodyParameter {
  310. BODY_PARAM_BOUNCE,
  311. BODY_PARAM_FRICTION,
  312. BODY_PARAM_MASS, ///< unused for static, always infinite
  313. BODY_PARAM_INERTIA,
  314. BODY_PARAM_CENTER_OF_MASS,
  315. BODY_PARAM_GRAVITY_SCALE,
  316. BODY_PARAM_LINEAR_DAMP_MODE,
  317. BODY_PARAM_ANGULAR_DAMP_MODE,
  318. BODY_PARAM_LINEAR_DAMP,
  319. BODY_PARAM_ANGULAR_DAMP,
  320. BODY_PARAM_MAX,
  321. };
  322. enum BodyDampMode {
  323. BODY_DAMP_MODE_COMBINE,
  324. BODY_DAMP_MODE_REPLACE,
  325. };
  326. virtual void body_set_param(RID p_body, BodyParameter p_param, const Variant &p_value) = 0;
  327. virtual Variant body_get_param(RID p_body, BodyParameter p_param) const = 0;
  328. virtual void body_reset_mass_properties(RID p_body) = 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. virtual void body_apply_central_impulse(RID p_body, const Vector2 &p_impulse) = 0;
  340. virtual void body_apply_torque_impulse(RID p_body, real_t p_torque) = 0;
  341. virtual void body_apply_impulse(RID p_body, const Vector2 &p_impulse, const Vector2 &p_position = Vector2()) = 0;
  342. virtual void body_apply_central_force(RID p_body, const Vector2 &p_force) = 0;
  343. virtual void body_apply_force(RID p_body, const Vector2 &p_force, const Vector2 &p_position = Vector2()) = 0;
  344. virtual void body_apply_torque(RID p_body, real_t p_torque) = 0;
  345. virtual void body_add_constant_central_force(RID p_body, const Vector2 &p_force) = 0;
  346. virtual void body_add_constant_force(RID p_body, const Vector2 &p_force, const Vector2 &p_position = Vector2()) = 0;
  347. virtual void body_add_constant_torque(RID p_body, real_t p_torque) = 0;
  348. virtual void body_set_constant_force(RID p_body, const Vector2 &p_force) = 0;
  349. virtual Vector2 body_get_constant_force(RID p_body) const = 0;
  350. virtual void body_set_constant_torque(RID p_body, real_t p_torque) = 0;
  351. virtual real_t body_get_constant_torque(RID p_body) const = 0;
  352. virtual void body_set_axis_velocity(RID p_body, const Vector2 &p_axis_velocity) = 0;
  353. //fix
  354. virtual void body_add_collision_exception(RID p_body, RID p_body_b) = 0;
  355. virtual void body_remove_collision_exception(RID p_body, RID p_body_b) = 0;
  356. virtual void body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) = 0;
  357. virtual void body_set_max_contacts_reported(RID p_body, int p_contacts) = 0;
  358. virtual int body_get_max_contacts_reported(RID p_body) const = 0;
  359. //missing remove
  360. virtual void body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) = 0;
  361. virtual real_t body_get_contacts_reported_depth_threshold(RID p_body) const = 0;
  362. virtual void body_set_omit_force_integration(RID p_body, bool p_omit) = 0;
  363. virtual bool body_is_omitting_force_integration(RID p_body) const = 0;
  364. virtual void body_set_state_sync_callback(RID p_body, const Callable &p_callable) = 0;
  365. virtual void body_set_force_integration_callback(RID p_body, const Callable &p_callable, const Variant &p_udata = Variant()) = 0;
  366. 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;
  367. virtual void body_set_pickable(RID p_body, bool p_pickable) = 0;
  368. // this function only works on physics process, errors and returns null otherwise
  369. virtual PhysicsDirectBodyState2D *body_get_direct_state(RID p_body) = 0;
  370. struct MotionParameters {
  371. Transform2D from;
  372. Vector2 motion;
  373. real_t margin = 0.08;
  374. bool collide_separation_ray = false;
  375. HashSet<RID> exclude_bodies;
  376. HashSet<ObjectID> exclude_objects;
  377. bool recovery_as_collision = false;
  378. MotionParameters() {}
  379. MotionParameters(const Transform2D &p_from, const Vector2 &p_motion, real_t p_margin = 0.08) :
  380. from(p_from),
  381. motion(p_motion),
  382. margin(p_margin) {}
  383. };
  384. struct MotionResult {
  385. Vector2 travel;
  386. Vector2 remainder;
  387. Vector2 collision_point;
  388. Vector2 collision_normal;
  389. Vector2 collider_velocity;
  390. real_t collision_depth = 0.0;
  391. real_t collision_safe_fraction = 0.0;
  392. real_t collision_unsafe_fraction = 0.0;
  393. int collision_local_shape = 0;
  394. ObjectID collider_id;
  395. RID collider;
  396. int collider_shape = 0;
  397. real_t get_angle(Vector2 p_up_direction) const {
  398. return Math::acos(collision_normal.dot(p_up_direction));
  399. }
  400. };
  401. virtual bool body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result = nullptr) = 0;
  402. /* JOINT API */
  403. virtual RID joint_create() = 0;
  404. virtual void joint_clear(RID p_joint) = 0;
  405. enum JointType {
  406. JOINT_TYPE_PIN,
  407. JOINT_TYPE_GROOVE,
  408. JOINT_TYPE_DAMPED_SPRING,
  409. JOINT_TYPE_MAX
  410. };
  411. enum JointParam {
  412. JOINT_PARAM_BIAS,
  413. JOINT_PARAM_MAX_BIAS,
  414. JOINT_PARAM_MAX_FORCE,
  415. };
  416. virtual void joint_set_param(RID p_joint, JointParam p_param, real_t p_value) = 0;
  417. virtual real_t joint_get_param(RID p_joint, JointParam p_param) const = 0;
  418. virtual void joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable) = 0;
  419. virtual bool joint_is_disabled_collisions_between_bodies(RID p_joint) const = 0;
  420. virtual void joint_make_pin(RID p_joint, const Vector2 &p_anchor, RID p_body_a, RID p_body_b = RID()) = 0;
  421. virtual void joint_make_groove(RID p_joint, const Vector2 &p_a_groove1, const Vector2 &p_a_groove2, const Vector2 &p_b_anchor, RID p_body_a, RID p_body_b) = 0;
  422. virtual void joint_make_damped_spring(RID p_joint, const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, RID p_body_a, RID p_body_b = RID()) = 0;
  423. enum PinJointParam {
  424. PIN_JOINT_SOFTNESS
  425. };
  426. virtual void pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) = 0;
  427. virtual real_t pin_joint_get_param(RID p_joint, PinJointParam p_param) const = 0;
  428. enum DampedSpringParam {
  429. DAMPED_SPRING_REST_LENGTH,
  430. DAMPED_SPRING_STIFFNESS,
  431. DAMPED_SPRING_DAMPING
  432. };
  433. virtual void damped_spring_joint_set_param(RID p_joint, DampedSpringParam p_param, real_t p_value) = 0;
  434. virtual real_t damped_spring_joint_get_param(RID p_joint, DampedSpringParam p_param) const = 0;
  435. virtual JointType joint_get_type(RID p_joint) const = 0;
  436. /* QUERY API */
  437. enum AreaBodyStatus {
  438. AREA_BODY_ADDED,
  439. AREA_BODY_REMOVED
  440. };
  441. /* MISC */
  442. virtual void free(RID p_rid) = 0;
  443. virtual void set_active(bool p_active) = 0;
  444. virtual void init() = 0;
  445. virtual void step(real_t p_step) = 0;
  446. virtual void sync() = 0;
  447. virtual void flush_queries() = 0;
  448. virtual void end_sync() = 0;
  449. virtual void finish() = 0;
  450. virtual bool is_flushing_queries() const = 0;
  451. enum ProcessInfo {
  452. INFO_ACTIVE_OBJECTS,
  453. INFO_COLLISION_PAIRS,
  454. INFO_ISLAND_COUNT
  455. };
  456. virtual int get_process_info(ProcessInfo p_info) = 0;
  457. PhysicsServer2D();
  458. ~PhysicsServer2D();
  459. };
  460. class PhysicsRayQueryParameters2D : public RefCounted {
  461. GDCLASS(PhysicsRayQueryParameters2D, RefCounted);
  462. PhysicsDirectSpaceState2D::RayParameters parameters;
  463. protected:
  464. static void _bind_methods();
  465. public:
  466. static Ref<PhysicsRayQueryParameters2D> create(Vector2 p_from, Vector2 p_to, uint32_t p_mask, const TypedArray<RID> &p_exclude);
  467. const PhysicsDirectSpaceState2D::RayParameters &get_parameters() const { return parameters; }
  468. void set_from(const Vector2 &p_from) { parameters.from = p_from; }
  469. const Vector2 &get_from() const { return parameters.from; }
  470. void set_to(const Vector2 &p_to) { parameters.to = p_to; }
  471. const Vector2 &get_to() const { return parameters.to; }
  472. void set_collision_mask(uint32_t p_mask) { parameters.collision_mask = p_mask; }
  473. uint32_t get_collision_mask() const { return parameters.collision_mask; }
  474. void set_collide_with_bodies(bool p_enable) { parameters.collide_with_bodies = p_enable; }
  475. bool is_collide_with_bodies_enabled() const { return parameters.collide_with_bodies; }
  476. void set_collide_with_areas(bool p_enable) { parameters.collide_with_areas = p_enable; }
  477. bool is_collide_with_areas_enabled() const { return parameters.collide_with_areas; }
  478. void set_hit_from_inside(bool p_enable) { parameters.hit_from_inside = p_enable; }
  479. bool is_hit_from_inside_enabled() const { return parameters.hit_from_inside; }
  480. void set_exclude(const TypedArray<RID> &p_exclude);
  481. TypedArray<RID> get_exclude() const;
  482. };
  483. class PhysicsPointQueryParameters2D : public RefCounted {
  484. GDCLASS(PhysicsPointQueryParameters2D, RefCounted);
  485. PhysicsDirectSpaceState2D::PointParameters parameters;
  486. protected:
  487. static void _bind_methods();
  488. public:
  489. const PhysicsDirectSpaceState2D::PointParameters &get_parameters() const { return parameters; }
  490. void set_position(const Vector2 &p_position) { parameters.position = p_position; }
  491. const Vector2 &get_position() const { return parameters.position; }
  492. void set_canvas_instance_id(ObjectID p_canvas_instance_id) { parameters.canvas_instance_id = p_canvas_instance_id; }
  493. ObjectID get_canvas_instance_id() const { return parameters.canvas_instance_id; }
  494. void set_collision_mask(uint32_t p_mask) { parameters.collision_mask = p_mask; }
  495. uint32_t get_collision_mask() const { return parameters.collision_mask; }
  496. void set_collide_with_bodies(bool p_enable) { parameters.collide_with_bodies = p_enable; }
  497. bool is_collide_with_bodies_enabled() const { return parameters.collide_with_bodies; }
  498. void set_collide_with_areas(bool p_enable) { parameters.collide_with_areas = p_enable; }
  499. bool is_collide_with_areas_enabled() const { return parameters.collide_with_areas; }
  500. void set_exclude(const TypedArray<RID> &p_exclude);
  501. TypedArray<RID> get_exclude() const;
  502. };
  503. class PhysicsShapeQueryParameters2D : public RefCounted {
  504. GDCLASS(PhysicsShapeQueryParameters2D, RefCounted);
  505. PhysicsDirectSpaceState2D::ShapeParameters parameters;
  506. Ref<Resource> shape_ref;
  507. protected:
  508. static void _bind_methods();
  509. public:
  510. const PhysicsDirectSpaceState2D::ShapeParameters &get_parameters() const { return parameters; }
  511. void set_shape(const Ref<Resource> &p_shape_ref);
  512. Ref<Resource> get_shape() const { return shape_ref; }
  513. void set_shape_rid(const RID &p_shape);
  514. RID get_shape_rid() const { return parameters.shape_rid; }
  515. void set_transform(const Transform2D &p_transform) { parameters.transform = p_transform; }
  516. const Transform2D &get_transform() const { return parameters.transform; }
  517. void set_motion(const Vector2 &p_motion) { parameters.motion = p_motion; }
  518. const Vector2 &get_motion() const { return parameters.motion; }
  519. void set_margin(real_t p_margin) { parameters.margin = p_margin; }
  520. real_t get_margin() const { return parameters.margin; }
  521. void set_collision_mask(uint32_t p_mask) { parameters.collision_mask = p_mask; }
  522. uint32_t get_collision_mask() const { return parameters.collision_mask; }
  523. void set_collide_with_bodies(bool p_enable) { parameters.collide_with_bodies = p_enable; }
  524. bool is_collide_with_bodies_enabled() const { return parameters.collide_with_bodies; }
  525. void set_collide_with_areas(bool p_enable) { parameters.collide_with_areas = p_enable; }
  526. bool is_collide_with_areas_enabled() const { return parameters.collide_with_areas; }
  527. void set_exclude(const TypedArray<RID> &p_exclude);
  528. TypedArray<RID> get_exclude() const;
  529. };
  530. class PhysicsTestMotionParameters2D : public RefCounted {
  531. GDCLASS(PhysicsTestMotionParameters2D, RefCounted);
  532. PhysicsServer2D::MotionParameters parameters;
  533. protected:
  534. static void _bind_methods();
  535. public:
  536. const PhysicsServer2D::MotionParameters &get_parameters() const { return parameters; }
  537. const Transform2D &get_from() const { return parameters.from; }
  538. void set_from(const Transform2D &p_from) { parameters.from = p_from; }
  539. const Vector2 &get_motion() const { return parameters.motion; }
  540. void set_motion(const Vector2 &p_motion) { parameters.motion = p_motion; }
  541. real_t get_margin() const { return parameters.margin; }
  542. void set_margin(real_t p_margin) { parameters.margin = p_margin; }
  543. bool is_collide_separation_ray_enabled() const { return parameters.collide_separation_ray; }
  544. void set_collide_separation_ray_enabled(bool p_enabled) { parameters.collide_separation_ray = p_enabled; }
  545. TypedArray<RID> get_exclude_bodies() const;
  546. void set_exclude_bodies(const TypedArray<RID> &p_exclude);
  547. TypedArray<uint64_t> get_exclude_objects() const;
  548. void set_exclude_objects(const TypedArray<uint64_t> &p_exclude);
  549. bool is_recovery_as_collision_enabled() const { return parameters.recovery_as_collision; }
  550. void set_recovery_as_collision_enabled(bool p_enabled) { parameters.recovery_as_collision = p_enabled; }
  551. };
  552. class PhysicsTestMotionResult2D : public RefCounted {
  553. GDCLASS(PhysicsTestMotionResult2D, RefCounted);
  554. PhysicsServer2D::MotionResult result;
  555. protected:
  556. static void _bind_methods();
  557. public:
  558. PhysicsServer2D::MotionResult *get_result_ptr() const { return const_cast<PhysicsServer2D::MotionResult *>(&result); }
  559. Vector2 get_travel() const;
  560. Vector2 get_remainder() const;
  561. Vector2 get_collision_point() const;
  562. Vector2 get_collision_normal() const;
  563. Vector2 get_collider_velocity() const;
  564. ObjectID get_collider_id() const;
  565. RID get_collider_rid() const;
  566. Object *get_collider() const;
  567. int get_collider_shape() const;
  568. int get_collision_local_shape() const;
  569. real_t get_collision_depth() const;
  570. real_t get_collision_safe_fraction() const;
  571. real_t get_collision_unsafe_fraction() const;
  572. };
  573. class PhysicsServer2DManager : public Object {
  574. GDCLASS(PhysicsServer2DManager, Object);
  575. static PhysicsServer2DManager *singleton;
  576. struct ClassInfo {
  577. String name;
  578. Callable create_callback;
  579. ClassInfo() {}
  580. ClassInfo(String p_name, Callable p_create_callback) :
  581. name(p_name),
  582. create_callback(p_create_callback) {}
  583. ClassInfo(const ClassInfo &p_ci) :
  584. name(p_ci.name),
  585. create_callback(p_ci.create_callback) {}
  586. void operator=(const ClassInfo &p_ci) {
  587. name = p_ci.name;
  588. create_callback = p_ci.create_callback;
  589. }
  590. };
  591. Vector<ClassInfo> physics_2d_servers;
  592. int default_server_id = -1;
  593. int default_server_priority = -1;
  594. void on_servers_changed();
  595. protected:
  596. static void _bind_methods();
  597. public:
  598. static const String setting_property_name;
  599. static PhysicsServer2DManager *get_singleton();
  600. void register_server(const String &p_name, const Callable &p_create_callback);
  601. void set_default_server(const String &p_name, int p_priority = 0);
  602. int find_server_id(const String &p_name);
  603. int get_servers_count();
  604. String get_server_name(int p_id);
  605. PhysicsServer2D *new_default_server();
  606. PhysicsServer2D *new_server(const String &p_name);
  607. PhysicsServer2DManager();
  608. ~PhysicsServer2DManager();
  609. };
  610. VARIANT_ENUM_CAST(PhysicsServer2D::ShapeType);
  611. VARIANT_ENUM_CAST(PhysicsServer2D::SpaceParameter);
  612. VARIANT_ENUM_CAST(PhysicsServer2D::AreaParameter);
  613. VARIANT_ENUM_CAST(PhysicsServer2D::AreaSpaceOverrideMode);
  614. VARIANT_ENUM_CAST(PhysicsServer2D::BodyMode);
  615. VARIANT_ENUM_CAST(PhysicsServer2D::BodyParameter);
  616. VARIANT_ENUM_CAST(PhysicsServer2D::BodyDampMode);
  617. VARIANT_ENUM_CAST(PhysicsServer2D::BodyState);
  618. VARIANT_ENUM_CAST(PhysicsServer2D::CCDMode);
  619. VARIANT_ENUM_CAST(PhysicsServer2D::JointParam);
  620. VARIANT_ENUM_CAST(PhysicsServer2D::JointType);
  621. VARIANT_ENUM_CAST(PhysicsServer2D::PinJointParam);
  622. VARIANT_ENUM_CAST(PhysicsServer2D::DampedSpringParam);
  623. VARIANT_ENUM_CAST(PhysicsServer2D::AreaBodyStatus);
  624. VARIANT_ENUM_CAST(PhysicsServer2D::ProcessInfo);
  625. #endif // PHYSICS_SERVER_2D_H