physics_server_2d.h 31 KB

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