physics_server_2d.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. /**************************************************************************/
  2. /* physics_server_2d.cpp */
  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. #include "physics_server_2d.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/string/print_string.h"
  33. #include "core/variant/typed_array.h"
  34. PhysicsServer2D *PhysicsServer2D::singleton = nullptr;
  35. void PhysicsDirectBodyState2D::integrate_forces() {
  36. real_t step = get_step();
  37. Vector2 lv = get_linear_velocity();
  38. lv += get_total_gravity() * step;
  39. real_t av = get_angular_velocity();
  40. real_t damp = 1.0 - step * get_total_linear_damp();
  41. if (damp < 0) { // reached zero in the given time
  42. damp = 0;
  43. }
  44. lv *= damp;
  45. damp = 1.0 - step * get_total_angular_damp();
  46. if (damp < 0) { // reached zero in the given time
  47. damp = 0;
  48. }
  49. av *= damp;
  50. set_linear_velocity(lv);
  51. set_angular_velocity(av);
  52. }
  53. Object *PhysicsDirectBodyState2D::get_contact_collider_object(int p_contact_idx) const {
  54. ObjectID objid = get_contact_collider_id(p_contact_idx);
  55. Object *obj = ObjectDB::get_instance(objid);
  56. return obj;
  57. }
  58. PhysicsServer2D *PhysicsServer2D::get_singleton() {
  59. return singleton;
  60. }
  61. void PhysicsDirectBodyState2D::_bind_methods() {
  62. ClassDB::bind_method(D_METHOD("get_total_gravity"), &PhysicsDirectBodyState2D::get_total_gravity);
  63. ClassDB::bind_method(D_METHOD("get_total_linear_damp"), &PhysicsDirectBodyState2D::get_total_linear_damp);
  64. ClassDB::bind_method(D_METHOD("get_total_angular_damp"), &PhysicsDirectBodyState2D::get_total_angular_damp);
  65. ClassDB::bind_method(D_METHOD("get_center_of_mass"), &PhysicsDirectBodyState2D::get_center_of_mass);
  66. ClassDB::bind_method(D_METHOD("get_center_of_mass_local"), &PhysicsDirectBodyState2D::get_center_of_mass_local);
  67. ClassDB::bind_method(D_METHOD("get_inverse_mass"), &PhysicsDirectBodyState2D::get_inverse_mass);
  68. ClassDB::bind_method(D_METHOD("get_inverse_inertia"), &PhysicsDirectBodyState2D::get_inverse_inertia);
  69. ClassDB::bind_method(D_METHOD("set_linear_velocity", "velocity"), &PhysicsDirectBodyState2D::set_linear_velocity);
  70. ClassDB::bind_method(D_METHOD("get_linear_velocity"), &PhysicsDirectBodyState2D::get_linear_velocity);
  71. ClassDB::bind_method(D_METHOD("set_angular_velocity", "velocity"), &PhysicsDirectBodyState2D::set_angular_velocity);
  72. ClassDB::bind_method(D_METHOD("get_angular_velocity"), &PhysicsDirectBodyState2D::get_angular_velocity);
  73. ClassDB::bind_method(D_METHOD("set_transform", "transform"), &PhysicsDirectBodyState2D::set_transform);
  74. ClassDB::bind_method(D_METHOD("get_transform"), &PhysicsDirectBodyState2D::get_transform);
  75. ClassDB::bind_method(D_METHOD("get_velocity_at_local_position", "local_position"), &PhysicsDirectBodyState2D::get_velocity_at_local_position);
  76. ClassDB::bind_method(D_METHOD("apply_central_impulse", "impulse"), &PhysicsDirectBodyState2D::apply_central_impulse);
  77. ClassDB::bind_method(D_METHOD("apply_torque_impulse", "impulse"), &PhysicsDirectBodyState2D::apply_torque_impulse);
  78. ClassDB::bind_method(D_METHOD("apply_impulse", "impulse", "position"), &PhysicsDirectBodyState2D::apply_impulse, Vector2());
  79. ClassDB::bind_method(D_METHOD("apply_central_force", "force"), &PhysicsDirectBodyState2D::apply_central_force, Vector2());
  80. ClassDB::bind_method(D_METHOD("apply_force", "force", "position"), &PhysicsDirectBodyState2D::apply_force, Vector2());
  81. ClassDB::bind_method(D_METHOD("apply_torque", "torque"), &PhysicsDirectBodyState2D::apply_torque);
  82. ClassDB::bind_method(D_METHOD("add_constant_central_force", "force"), &PhysicsDirectBodyState2D::add_constant_central_force, Vector2());
  83. ClassDB::bind_method(D_METHOD("add_constant_force", "force", "position"), &PhysicsDirectBodyState2D::add_constant_force, Vector2());
  84. ClassDB::bind_method(D_METHOD("add_constant_torque", "torque"), &PhysicsDirectBodyState2D::add_constant_torque);
  85. ClassDB::bind_method(D_METHOD("set_constant_force", "force"), &PhysicsDirectBodyState2D::set_constant_force);
  86. ClassDB::bind_method(D_METHOD("get_constant_force"), &PhysicsDirectBodyState2D::get_constant_force);
  87. ClassDB::bind_method(D_METHOD("set_constant_torque", "torque"), &PhysicsDirectBodyState2D::set_constant_torque);
  88. ClassDB::bind_method(D_METHOD("get_constant_torque"), &PhysicsDirectBodyState2D::get_constant_torque);
  89. ClassDB::bind_method(D_METHOD("set_sleep_state", "enabled"), &PhysicsDirectBodyState2D::set_sleep_state);
  90. ClassDB::bind_method(D_METHOD("is_sleeping"), &PhysicsDirectBodyState2D::is_sleeping);
  91. ClassDB::bind_method(D_METHOD("get_contact_count"), &PhysicsDirectBodyState2D::get_contact_count);
  92. ClassDB::bind_method(D_METHOD("get_contact_local_position", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_local_position);
  93. ClassDB::bind_method(D_METHOD("get_contact_local_normal", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_local_normal);
  94. ClassDB::bind_method(D_METHOD("get_contact_local_shape", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_local_shape);
  95. ClassDB::bind_method(D_METHOD("get_contact_collider", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider);
  96. ClassDB::bind_method(D_METHOD("get_contact_collider_position", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider_position);
  97. ClassDB::bind_method(D_METHOD("get_contact_collider_id", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider_id);
  98. ClassDB::bind_method(D_METHOD("get_contact_collider_object", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider_object);
  99. ClassDB::bind_method(D_METHOD("get_contact_collider_shape", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider_shape);
  100. ClassDB::bind_method(D_METHOD("get_contact_collider_velocity_at_position", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider_velocity_at_position);
  101. ClassDB::bind_method(D_METHOD("get_contact_impulse", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_impulse);
  102. ClassDB::bind_method(D_METHOD("get_step"), &PhysicsDirectBodyState2D::get_step);
  103. ClassDB::bind_method(D_METHOD("integrate_forces"), &PhysicsDirectBodyState2D::integrate_forces);
  104. ClassDB::bind_method(D_METHOD("get_space_state"), &PhysicsDirectBodyState2D::get_space_state);
  105. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "step"), "", "get_step");
  106. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inverse_mass"), "", "get_inverse_mass");
  107. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inverse_inertia"), "", "get_inverse_inertia");
  108. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "total_angular_damp"), "", "get_total_angular_damp");
  109. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "total_linear_damp"), "", "get_total_linear_damp");
  110. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "total_gravity"), "", "get_total_gravity");
  111. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "center_of_mass"), "", "get_center_of_mass");
  112. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "center_of_mass_local"), "", "get_center_of_mass_local");
  113. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_velocity"), "set_angular_velocity", "get_angular_velocity");
  114. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "linear_velocity"), "set_linear_velocity", "get_linear_velocity");
  115. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sleeping"), "set_sleep_state", "is_sleeping");
  116. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "transform"), "set_transform", "get_transform");
  117. }
  118. PhysicsDirectBodyState2D::PhysicsDirectBodyState2D() {}
  119. ///////////////////////////////////////////////////////
  120. Ref<PhysicsRayQueryParameters2D> PhysicsRayQueryParameters2D::create(Vector2 p_from, Vector2 p_to, uint32_t p_mask, const TypedArray<RID> &p_exclude) {
  121. Ref<PhysicsRayQueryParameters2D> params;
  122. params.instantiate();
  123. params->set_from(p_from);
  124. params->set_to(p_to);
  125. params->set_collision_mask(p_mask);
  126. params->set_exclude(p_exclude);
  127. return params;
  128. }
  129. void PhysicsRayQueryParameters2D::set_exclude(const TypedArray<RID> &p_exclude) {
  130. parameters.exclude.clear();
  131. for (int i = 0; i < p_exclude.size(); i++) {
  132. parameters.exclude.insert(p_exclude[i]);
  133. }
  134. }
  135. TypedArray<RID> PhysicsRayQueryParameters2D::get_exclude() const {
  136. TypedArray<RID> ret;
  137. ret.resize(parameters.exclude.size());
  138. int idx = 0;
  139. for (const RID &E : parameters.exclude) {
  140. ret[idx++] = E;
  141. }
  142. return ret;
  143. }
  144. void PhysicsRayQueryParameters2D::_bind_methods() {
  145. ClassDB::bind_static_method("PhysicsRayQueryParameters2D", D_METHOD("create", "from", "to", "collision_mask", "exclude"), &PhysicsRayQueryParameters2D::create, DEFVAL(UINT32_MAX), DEFVAL(TypedArray<RID>()));
  146. ClassDB::bind_method(D_METHOD("set_from", "from"), &PhysicsRayQueryParameters2D::set_from);
  147. ClassDB::bind_method(D_METHOD("get_from"), &PhysicsRayQueryParameters2D::get_from);
  148. ClassDB::bind_method(D_METHOD("set_to", "to"), &PhysicsRayQueryParameters2D::set_to);
  149. ClassDB::bind_method(D_METHOD("get_to"), &PhysicsRayQueryParameters2D::get_to);
  150. ClassDB::bind_method(D_METHOD("set_collision_mask", "collision_mask"), &PhysicsRayQueryParameters2D::set_collision_mask);
  151. ClassDB::bind_method(D_METHOD("get_collision_mask"), &PhysicsRayQueryParameters2D::get_collision_mask);
  152. ClassDB::bind_method(D_METHOD("set_exclude", "exclude"), &PhysicsRayQueryParameters2D::set_exclude);
  153. ClassDB::bind_method(D_METHOD("get_exclude"), &PhysicsRayQueryParameters2D::get_exclude);
  154. ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &PhysicsRayQueryParameters2D::set_collide_with_bodies);
  155. ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &PhysicsRayQueryParameters2D::is_collide_with_bodies_enabled);
  156. ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &PhysicsRayQueryParameters2D::set_collide_with_areas);
  157. ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &PhysicsRayQueryParameters2D::is_collide_with_areas_enabled);
  158. ClassDB::bind_method(D_METHOD("set_hit_from_inside", "enable"), &PhysicsRayQueryParameters2D::set_hit_from_inside);
  159. ClassDB::bind_method(D_METHOD("is_hit_from_inside_enabled"), &PhysicsRayQueryParameters2D::is_hit_from_inside_enabled);
  160. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "from"), "set_from", "get_from");
  161. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "to"), "set_to", "get_to");
  162. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
  163. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude", PROPERTY_HINT_ARRAY_TYPE, "RID"), "set_exclude", "get_exclude");
  164. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies"), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
  165. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas"), "set_collide_with_areas", "is_collide_with_areas_enabled");
  166. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hit_from_inside"), "set_hit_from_inside", "is_hit_from_inside_enabled");
  167. }
  168. ///////////////////////////////////////////////////////
  169. void PhysicsPointQueryParameters2D::set_exclude(const TypedArray<RID> &p_exclude) {
  170. parameters.exclude.clear();
  171. for (int i = 0; i < p_exclude.size(); i++) {
  172. parameters.exclude.insert(p_exclude[i]);
  173. }
  174. }
  175. TypedArray<RID> PhysicsPointQueryParameters2D::get_exclude() const {
  176. TypedArray<RID> ret;
  177. ret.resize(parameters.exclude.size());
  178. int idx = 0;
  179. for (const RID &E : parameters.exclude) {
  180. ret[idx++] = E;
  181. }
  182. return ret;
  183. }
  184. void PhysicsPointQueryParameters2D::_bind_methods() {
  185. ClassDB::bind_method(D_METHOD("set_position", "position"), &PhysicsPointQueryParameters2D::set_position);
  186. ClassDB::bind_method(D_METHOD("get_position"), &PhysicsPointQueryParameters2D::get_position);
  187. ClassDB::bind_method(D_METHOD("set_canvas_instance_id", "canvas_instance_id"), &PhysicsPointQueryParameters2D::set_canvas_instance_id);
  188. ClassDB::bind_method(D_METHOD("get_canvas_instance_id"), &PhysicsPointQueryParameters2D::get_canvas_instance_id);
  189. ClassDB::bind_method(D_METHOD("set_collision_mask", "collision_mask"), &PhysicsPointQueryParameters2D::set_collision_mask);
  190. ClassDB::bind_method(D_METHOD("get_collision_mask"), &PhysicsPointQueryParameters2D::get_collision_mask);
  191. ClassDB::bind_method(D_METHOD("set_exclude", "exclude"), &PhysicsPointQueryParameters2D::set_exclude);
  192. ClassDB::bind_method(D_METHOD("get_exclude"), &PhysicsPointQueryParameters2D::get_exclude);
  193. ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &PhysicsPointQueryParameters2D::set_collide_with_bodies);
  194. ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &PhysicsPointQueryParameters2D::is_collide_with_bodies_enabled);
  195. ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &PhysicsPointQueryParameters2D::set_collide_with_areas);
  196. ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &PhysicsPointQueryParameters2D::is_collide_with_areas_enabled);
  197. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
  198. ADD_PROPERTY(PropertyInfo(Variant::INT, "canvas_instance_id", PROPERTY_HINT_OBJECT_ID), "set_canvas_instance_id", "get_canvas_instance_id");
  199. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
  200. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude", PROPERTY_HINT_ARRAY_TYPE, "RID"), "set_exclude", "get_exclude");
  201. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies"), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
  202. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas"), "set_collide_with_areas", "is_collide_with_areas_enabled");
  203. }
  204. ///////////////////////////////////////////////////////
  205. void PhysicsShapeQueryParameters2D::set_shape(const Ref<Resource> &p_shape_ref) {
  206. ERR_FAIL_COND(p_shape_ref.is_null());
  207. shape_ref = p_shape_ref;
  208. parameters.shape_rid = p_shape_ref->get_rid();
  209. }
  210. void PhysicsShapeQueryParameters2D::set_shape_rid(const RID &p_shape) {
  211. if (parameters.shape_rid != p_shape) {
  212. shape_ref = Ref<Resource>();
  213. parameters.shape_rid = p_shape;
  214. }
  215. }
  216. void PhysicsShapeQueryParameters2D::set_exclude(const TypedArray<RID> &p_exclude) {
  217. parameters.exclude.clear();
  218. for (int i = 0; i < p_exclude.size(); i++) {
  219. parameters.exclude.insert(p_exclude[i]);
  220. }
  221. }
  222. TypedArray<RID> PhysicsShapeQueryParameters2D::get_exclude() const {
  223. TypedArray<RID> ret;
  224. ret.resize(parameters.exclude.size());
  225. int idx = 0;
  226. for (const RID &E : parameters.exclude) {
  227. ret[idx++] = E;
  228. }
  229. return ret;
  230. }
  231. void PhysicsShapeQueryParameters2D::_bind_methods() {
  232. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &PhysicsShapeQueryParameters2D::set_shape);
  233. ClassDB::bind_method(D_METHOD("get_shape"), &PhysicsShapeQueryParameters2D::get_shape);
  234. ClassDB::bind_method(D_METHOD("set_shape_rid", "shape"), &PhysicsShapeQueryParameters2D::set_shape_rid);
  235. ClassDB::bind_method(D_METHOD("get_shape_rid"), &PhysicsShapeQueryParameters2D::get_shape_rid);
  236. ClassDB::bind_method(D_METHOD("set_transform", "transform"), &PhysicsShapeQueryParameters2D::set_transform);
  237. ClassDB::bind_method(D_METHOD("get_transform"), &PhysicsShapeQueryParameters2D::get_transform);
  238. ClassDB::bind_method(D_METHOD("set_motion", "motion"), &PhysicsShapeQueryParameters2D::set_motion);
  239. ClassDB::bind_method(D_METHOD("get_motion"), &PhysicsShapeQueryParameters2D::get_motion);
  240. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &PhysicsShapeQueryParameters2D::set_margin);
  241. ClassDB::bind_method(D_METHOD("get_margin"), &PhysicsShapeQueryParameters2D::get_margin);
  242. ClassDB::bind_method(D_METHOD("set_collision_mask", "collision_mask"), &PhysicsShapeQueryParameters2D::set_collision_mask);
  243. ClassDB::bind_method(D_METHOD("get_collision_mask"), &PhysicsShapeQueryParameters2D::get_collision_mask);
  244. ClassDB::bind_method(D_METHOD("set_exclude", "exclude"), &PhysicsShapeQueryParameters2D::set_exclude);
  245. ClassDB::bind_method(D_METHOD("get_exclude"), &PhysicsShapeQueryParameters2D::get_exclude);
  246. ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &PhysicsShapeQueryParameters2D::set_collide_with_bodies);
  247. ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &PhysicsShapeQueryParameters2D::is_collide_with_bodies_enabled);
  248. ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &PhysicsShapeQueryParameters2D::set_collide_with_areas);
  249. ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &PhysicsShapeQueryParameters2D::is_collide_with_areas_enabled);
  250. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
  251. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude", PROPERTY_HINT_ARRAY_TYPE, "RID"), "set_exclude", "get_exclude");
  252. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin", PROPERTY_HINT_RANGE, "0,100,0.01"), "set_margin", "get_margin");
  253. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion"), "set_motion", "get_motion");
  254. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");
  255. ADD_PROPERTY(PropertyInfo(Variant::RID, "shape_rid"), "set_shape_rid", "get_shape_rid");
  256. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "transform"), "set_transform", "get_transform");
  257. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies"), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
  258. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas"), "set_collide_with_areas", "is_collide_with_areas_enabled");
  259. }
  260. ///////////////////////////////////////////////////////
  261. Dictionary PhysicsDirectSpaceState2D::_intersect_ray(const Ref<PhysicsRayQueryParameters2D> &p_ray_query) {
  262. ERR_FAIL_COND_V(!p_ray_query.is_valid(), Dictionary());
  263. RayResult result;
  264. bool res = intersect_ray(p_ray_query->get_parameters(), result);
  265. if (!res) {
  266. return Dictionary();
  267. }
  268. Dictionary d;
  269. d["position"] = result.position;
  270. d["normal"] = result.normal;
  271. d["collider_id"] = result.collider_id;
  272. d["collider"] = result.collider;
  273. d["shape"] = result.shape;
  274. d["rid"] = result.rid;
  275. return d;
  276. }
  277. TypedArray<Dictionary> PhysicsDirectSpaceState2D::_intersect_point(const Ref<PhysicsPointQueryParameters2D> &p_point_query, int p_max_results) {
  278. ERR_FAIL_COND_V(p_point_query.is_null(), Array());
  279. Vector<ShapeResult> ret;
  280. ret.resize(p_max_results);
  281. int rc = intersect_point(p_point_query->get_parameters(), ret.ptrw(), ret.size());
  282. if (rc == 0) {
  283. return TypedArray<Dictionary>();
  284. }
  285. TypedArray<Dictionary> r;
  286. r.resize(rc);
  287. for (int i = 0; i < rc; i++) {
  288. Dictionary d;
  289. d["rid"] = ret[i].rid;
  290. d["collider_id"] = ret[i].collider_id;
  291. d["collider"] = ret[i].collider;
  292. d["shape"] = ret[i].shape;
  293. r[i] = d;
  294. }
  295. return r;
  296. }
  297. TypedArray<Dictionary> PhysicsDirectSpaceState2D::_intersect_shape(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query, int p_max_results) {
  298. ERR_FAIL_COND_V(!p_shape_query.is_valid(), TypedArray<Dictionary>());
  299. Vector<ShapeResult> sr;
  300. sr.resize(p_max_results);
  301. int rc = intersect_shape(p_shape_query->get_parameters(), sr.ptrw(), sr.size());
  302. TypedArray<Dictionary> ret;
  303. ret.resize(rc);
  304. for (int i = 0; i < rc; i++) {
  305. Dictionary d;
  306. d["rid"] = sr[i].rid;
  307. d["collider_id"] = sr[i].collider_id;
  308. d["collider"] = sr[i].collider;
  309. d["shape"] = sr[i].shape;
  310. ret[i] = d;
  311. }
  312. return ret;
  313. }
  314. Vector<real_t> PhysicsDirectSpaceState2D::_cast_motion(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query) {
  315. ERR_FAIL_COND_V(!p_shape_query.is_valid(), Vector<real_t>());
  316. real_t closest_safe, closest_unsafe;
  317. bool res = cast_motion(p_shape_query->get_parameters(), closest_safe, closest_unsafe);
  318. if (!res) {
  319. return Vector<real_t>();
  320. }
  321. Vector<real_t> ret;
  322. ret.resize(2);
  323. ret.write[0] = closest_safe;
  324. ret.write[1] = closest_unsafe;
  325. return ret;
  326. }
  327. TypedArray<Vector2> PhysicsDirectSpaceState2D::_collide_shape(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query, int p_max_results) {
  328. ERR_FAIL_COND_V(!p_shape_query.is_valid(), TypedArray<Vector2>());
  329. Vector<Vector2> ret;
  330. ret.resize(p_max_results * 2);
  331. int rc = 0;
  332. bool res = collide_shape(p_shape_query->get_parameters(), ret.ptrw(), p_max_results, rc);
  333. if (!res) {
  334. return TypedArray<Vector2>();
  335. }
  336. TypedArray<Vector2> r;
  337. r.resize(rc * 2);
  338. for (int i = 0; i < rc * 2; i++) {
  339. r[i] = ret[i];
  340. }
  341. return r;
  342. }
  343. Dictionary PhysicsDirectSpaceState2D::_get_rest_info(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query) {
  344. ERR_FAIL_COND_V(!p_shape_query.is_valid(), Dictionary());
  345. ShapeRestInfo sri;
  346. bool res = rest_info(p_shape_query->get_parameters(), &sri);
  347. Dictionary r;
  348. if (!res) {
  349. return r;
  350. }
  351. r["point"] = sri.point;
  352. r["normal"] = sri.normal;
  353. r["rid"] = sri.rid;
  354. r["collider_id"] = sri.collider_id;
  355. r["shape"] = sri.shape;
  356. r["linear_velocity"] = sri.linear_velocity;
  357. return r;
  358. }
  359. PhysicsDirectSpaceState2D::PhysicsDirectSpaceState2D() {
  360. }
  361. void PhysicsDirectSpaceState2D::_bind_methods() {
  362. ClassDB::bind_method(D_METHOD("intersect_point", "parameters", "max_results"), &PhysicsDirectSpaceState2D::_intersect_point, DEFVAL(32));
  363. ClassDB::bind_method(D_METHOD("intersect_ray", "parameters"), &PhysicsDirectSpaceState2D::_intersect_ray);
  364. ClassDB::bind_method(D_METHOD("intersect_shape", "parameters", "max_results"), &PhysicsDirectSpaceState2D::_intersect_shape, DEFVAL(32));
  365. ClassDB::bind_method(D_METHOD("cast_motion", "parameters"), &PhysicsDirectSpaceState2D::_cast_motion);
  366. ClassDB::bind_method(D_METHOD("collide_shape", "parameters", "max_results"), &PhysicsDirectSpaceState2D::_collide_shape, DEFVAL(32));
  367. ClassDB::bind_method(D_METHOD("get_rest_info", "parameters"), &PhysicsDirectSpaceState2D::_get_rest_info);
  368. }
  369. ///////////////////////////////
  370. TypedArray<RID> PhysicsTestMotionParameters2D::get_exclude_bodies() const {
  371. TypedArray<RID> exclude;
  372. exclude.resize(parameters.exclude_bodies.size());
  373. int body_index = 0;
  374. for (RID body : parameters.exclude_bodies) {
  375. exclude[body_index++] = body;
  376. }
  377. return exclude;
  378. }
  379. void PhysicsTestMotionParameters2D::set_exclude_bodies(const TypedArray<RID> &p_exclude) {
  380. for (int i = 0; i < p_exclude.size(); i++) {
  381. parameters.exclude_bodies.insert(p_exclude[i]);
  382. }
  383. }
  384. TypedArray<uint64_t> PhysicsTestMotionParameters2D::get_exclude_objects() const {
  385. TypedArray<uint64_t> exclude;
  386. exclude.resize(parameters.exclude_objects.size());
  387. int object_index = 0;
  388. for (ObjectID object_id : parameters.exclude_objects) {
  389. exclude[object_index++] = object_id;
  390. }
  391. return exclude;
  392. }
  393. void PhysicsTestMotionParameters2D::set_exclude_objects(const TypedArray<uint64_t> &p_exclude) {
  394. for (int i = 0; i < p_exclude.size(); ++i) {
  395. ObjectID object_id = p_exclude[i];
  396. ERR_CONTINUE(object_id.is_null());
  397. parameters.exclude_objects.insert(object_id);
  398. }
  399. }
  400. void PhysicsTestMotionParameters2D::_bind_methods() {
  401. ClassDB::bind_method(D_METHOD("get_from"), &PhysicsTestMotionParameters2D::get_from);
  402. ClassDB::bind_method(D_METHOD("set_from", "from"), &PhysicsTestMotionParameters2D::set_from);
  403. ClassDB::bind_method(D_METHOD("get_motion"), &PhysicsTestMotionParameters2D::get_motion);
  404. ClassDB::bind_method(D_METHOD("set_motion", "motion"), &PhysicsTestMotionParameters2D::set_motion);
  405. ClassDB::bind_method(D_METHOD("get_margin"), &PhysicsTestMotionParameters2D::get_margin);
  406. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &PhysicsTestMotionParameters2D::set_margin);
  407. ClassDB::bind_method(D_METHOD("is_collide_separation_ray_enabled"), &PhysicsTestMotionParameters2D::is_collide_separation_ray_enabled);
  408. ClassDB::bind_method(D_METHOD("set_collide_separation_ray_enabled", "enabled"), &PhysicsTestMotionParameters2D::set_collide_separation_ray_enabled);
  409. ClassDB::bind_method(D_METHOD("get_exclude_bodies"), &PhysicsTestMotionParameters2D::get_exclude_bodies);
  410. ClassDB::bind_method(D_METHOD("set_exclude_bodies", "exclude_list"), &PhysicsTestMotionParameters2D::set_exclude_bodies);
  411. ClassDB::bind_method(D_METHOD("get_exclude_objects"), &PhysicsTestMotionParameters2D::get_exclude_objects);
  412. ClassDB::bind_method(D_METHOD("set_exclude_objects", "exclude_list"), &PhysicsTestMotionParameters2D::set_exclude_objects);
  413. ClassDB::bind_method(D_METHOD("is_recovery_as_collision_enabled"), &PhysicsTestMotionParameters2D::is_recovery_as_collision_enabled);
  414. ClassDB::bind_method(D_METHOD("set_recovery_as_collision_enabled", "enabled"), &PhysicsTestMotionParameters2D::set_recovery_as_collision_enabled);
  415. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "from"), "set_from", "get_from");
  416. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion"), "set_motion", "get_motion");
  417. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin"), "set_margin", "get_margin");
  418. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_separation_ray"), "set_collide_separation_ray_enabled", "is_collide_separation_ray_enabled");
  419. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude_bodies", PROPERTY_HINT_ARRAY_TYPE, "RID"), "set_exclude_bodies", "get_exclude_bodies");
  420. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude_objects"), "set_exclude_objects", "get_exclude_objects");
  421. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "recovery_as_collision"), "set_recovery_as_collision_enabled", "is_recovery_as_collision_enabled");
  422. }
  423. ///////////////////////////////
  424. Vector2 PhysicsTestMotionResult2D::get_travel() const {
  425. return result.travel;
  426. }
  427. Vector2 PhysicsTestMotionResult2D::get_remainder() const {
  428. return result.remainder;
  429. }
  430. Vector2 PhysicsTestMotionResult2D::get_collision_point() const {
  431. return result.collision_point;
  432. }
  433. Vector2 PhysicsTestMotionResult2D::get_collision_normal() const {
  434. return result.collision_normal;
  435. }
  436. Vector2 PhysicsTestMotionResult2D::get_collider_velocity() const {
  437. return result.collider_velocity;
  438. }
  439. ObjectID PhysicsTestMotionResult2D::get_collider_id() const {
  440. return result.collider_id;
  441. }
  442. RID PhysicsTestMotionResult2D::get_collider_rid() const {
  443. return result.collider;
  444. }
  445. Object *PhysicsTestMotionResult2D::get_collider() const {
  446. return ObjectDB::get_instance(result.collider_id);
  447. }
  448. int PhysicsTestMotionResult2D::get_collider_shape() const {
  449. return result.collider_shape;
  450. }
  451. int PhysicsTestMotionResult2D::get_collision_local_shape() const {
  452. return result.collision_local_shape;
  453. }
  454. real_t PhysicsTestMotionResult2D::get_collision_depth() const {
  455. return result.collision_depth;
  456. }
  457. real_t PhysicsTestMotionResult2D::get_collision_safe_fraction() const {
  458. return result.collision_safe_fraction;
  459. }
  460. real_t PhysicsTestMotionResult2D::get_collision_unsafe_fraction() const {
  461. return result.collision_unsafe_fraction;
  462. }
  463. void PhysicsTestMotionResult2D::_bind_methods() {
  464. ClassDB::bind_method(D_METHOD("get_travel"), &PhysicsTestMotionResult2D::get_travel);
  465. ClassDB::bind_method(D_METHOD("get_remainder"), &PhysicsTestMotionResult2D::get_remainder);
  466. ClassDB::bind_method(D_METHOD("get_collision_point"), &PhysicsTestMotionResult2D::get_collision_point);
  467. ClassDB::bind_method(D_METHOD("get_collision_normal"), &PhysicsTestMotionResult2D::get_collision_normal);
  468. ClassDB::bind_method(D_METHOD("get_collider_velocity"), &PhysicsTestMotionResult2D::get_collider_velocity);
  469. ClassDB::bind_method(D_METHOD("get_collider_id"), &PhysicsTestMotionResult2D::get_collider_id);
  470. ClassDB::bind_method(D_METHOD("get_collider_rid"), &PhysicsTestMotionResult2D::get_collider_rid);
  471. ClassDB::bind_method(D_METHOD("get_collider"), &PhysicsTestMotionResult2D::get_collider);
  472. ClassDB::bind_method(D_METHOD("get_collider_shape"), &PhysicsTestMotionResult2D::get_collider_shape);
  473. ClassDB::bind_method(D_METHOD("get_collision_local_shape"), &PhysicsTestMotionResult2D::get_collision_local_shape);
  474. ClassDB::bind_method(D_METHOD("get_collision_depth"), &PhysicsTestMotionResult2D::get_collision_depth);
  475. ClassDB::bind_method(D_METHOD("get_collision_safe_fraction"), &PhysicsTestMotionResult2D::get_collision_safe_fraction);
  476. ClassDB::bind_method(D_METHOD("get_collision_unsafe_fraction"), &PhysicsTestMotionResult2D::get_collision_unsafe_fraction);
  477. }
  478. ///////////////////////////////////////
  479. bool PhysicsServer2D::_body_test_motion(RID p_body, const Ref<PhysicsTestMotionParameters2D> &p_parameters, const Ref<PhysicsTestMotionResult2D> &p_result) {
  480. ERR_FAIL_COND_V(!p_parameters.is_valid(), false);
  481. MotionResult *result_ptr = nullptr;
  482. if (p_result.is_valid()) {
  483. result_ptr = p_result->get_result_ptr();
  484. }
  485. return body_test_motion(p_body, p_parameters->get_parameters(), result_ptr);
  486. }
  487. void PhysicsServer2D::_bind_methods() {
  488. ClassDB::bind_method(D_METHOD("world_boundary_shape_create"), &PhysicsServer2D::world_boundary_shape_create);
  489. ClassDB::bind_method(D_METHOD("separation_ray_shape_create"), &PhysicsServer2D::separation_ray_shape_create);
  490. ClassDB::bind_method(D_METHOD("segment_shape_create"), &PhysicsServer2D::segment_shape_create);
  491. ClassDB::bind_method(D_METHOD("circle_shape_create"), &PhysicsServer2D::circle_shape_create);
  492. ClassDB::bind_method(D_METHOD("rectangle_shape_create"), &PhysicsServer2D::rectangle_shape_create);
  493. ClassDB::bind_method(D_METHOD("capsule_shape_create"), &PhysicsServer2D::capsule_shape_create);
  494. ClassDB::bind_method(D_METHOD("convex_polygon_shape_create"), &PhysicsServer2D::convex_polygon_shape_create);
  495. ClassDB::bind_method(D_METHOD("concave_polygon_shape_create"), &PhysicsServer2D::concave_polygon_shape_create);
  496. ClassDB::bind_method(D_METHOD("shape_set_data", "shape", "data"), &PhysicsServer2D::shape_set_data);
  497. ClassDB::bind_method(D_METHOD("shape_get_type", "shape"), &PhysicsServer2D::shape_get_type);
  498. ClassDB::bind_method(D_METHOD("shape_get_data", "shape"), &PhysicsServer2D::shape_get_data);
  499. ClassDB::bind_method(D_METHOD("space_create"), &PhysicsServer2D::space_create);
  500. ClassDB::bind_method(D_METHOD("space_set_active", "space", "active"), &PhysicsServer2D::space_set_active);
  501. ClassDB::bind_method(D_METHOD("space_is_active", "space"), &PhysicsServer2D::space_is_active);
  502. ClassDB::bind_method(D_METHOD("space_set_param", "space", "param", "value"), &PhysicsServer2D::space_set_param);
  503. ClassDB::bind_method(D_METHOD("space_get_param", "space", "param"), &PhysicsServer2D::space_get_param);
  504. ClassDB::bind_method(D_METHOD("space_get_direct_state", "space"), &PhysicsServer2D::space_get_direct_state);
  505. ClassDB::bind_method(D_METHOD("area_create"), &PhysicsServer2D::area_create);
  506. ClassDB::bind_method(D_METHOD("area_set_space", "area", "space"), &PhysicsServer2D::area_set_space);
  507. ClassDB::bind_method(D_METHOD("area_get_space", "area"), &PhysicsServer2D::area_get_space);
  508. ClassDB::bind_method(D_METHOD("area_add_shape", "area", "shape", "transform", "disabled"), &PhysicsServer2D::area_add_shape, DEFVAL(Transform2D()), DEFVAL(false));
  509. ClassDB::bind_method(D_METHOD("area_set_shape", "area", "shape_idx", "shape"), &PhysicsServer2D::area_set_shape);
  510. ClassDB::bind_method(D_METHOD("area_set_shape_transform", "area", "shape_idx", "transform"), &PhysicsServer2D::area_set_shape_transform);
  511. ClassDB::bind_method(D_METHOD("area_set_shape_disabled", "area", "shape_idx", "disabled"), &PhysicsServer2D::area_set_shape_disabled);
  512. ClassDB::bind_method(D_METHOD("area_get_shape_count", "area"), &PhysicsServer2D::area_get_shape_count);
  513. ClassDB::bind_method(D_METHOD("area_get_shape", "area", "shape_idx"), &PhysicsServer2D::area_get_shape);
  514. ClassDB::bind_method(D_METHOD("area_get_shape_transform", "area", "shape_idx"), &PhysicsServer2D::area_get_shape_transform);
  515. ClassDB::bind_method(D_METHOD("area_remove_shape", "area", "shape_idx"), &PhysicsServer2D::area_remove_shape);
  516. ClassDB::bind_method(D_METHOD("area_clear_shapes", "area"), &PhysicsServer2D::area_clear_shapes);
  517. ClassDB::bind_method(D_METHOD("area_set_collision_layer", "area", "layer"), &PhysicsServer2D::area_set_collision_layer);
  518. ClassDB::bind_method(D_METHOD("area_get_collision_layer", "area"), &PhysicsServer2D::area_get_collision_layer);
  519. ClassDB::bind_method(D_METHOD("area_set_collision_mask", "area", "mask"), &PhysicsServer2D::area_set_collision_mask);
  520. ClassDB::bind_method(D_METHOD("area_get_collision_mask", "area"), &PhysicsServer2D::area_get_collision_mask);
  521. ClassDB::bind_method(D_METHOD("area_set_param", "area", "param", "value"), &PhysicsServer2D::area_set_param);
  522. ClassDB::bind_method(D_METHOD("area_set_transform", "area", "transform"), &PhysicsServer2D::area_set_transform);
  523. ClassDB::bind_method(D_METHOD("area_get_param", "area", "param"), &PhysicsServer2D::area_get_param);
  524. ClassDB::bind_method(D_METHOD("area_get_transform", "area"), &PhysicsServer2D::area_get_transform);
  525. ClassDB::bind_method(D_METHOD("area_attach_object_instance_id", "area", "id"), &PhysicsServer2D::area_attach_object_instance_id);
  526. ClassDB::bind_method(D_METHOD("area_get_object_instance_id", "area"), &PhysicsServer2D::area_get_object_instance_id);
  527. ClassDB::bind_method(D_METHOD("area_attach_canvas_instance_id", "area", "id"), &PhysicsServer2D::area_attach_canvas_instance_id);
  528. ClassDB::bind_method(D_METHOD("area_get_canvas_instance_id", "area"), &PhysicsServer2D::area_get_canvas_instance_id);
  529. ClassDB::bind_method(D_METHOD("area_set_monitor_callback", "area", "callback"), &PhysicsServer2D::area_set_monitor_callback);
  530. ClassDB::bind_method(D_METHOD("area_set_area_monitor_callback", "area", "callback"), &PhysicsServer2D::area_set_area_monitor_callback);
  531. ClassDB::bind_method(D_METHOD("area_set_monitorable", "area", "monitorable"), &PhysicsServer2D::area_set_monitorable);
  532. ClassDB::bind_method(D_METHOD("body_create"), &PhysicsServer2D::body_create);
  533. ClassDB::bind_method(D_METHOD("body_set_space", "body", "space"), &PhysicsServer2D::body_set_space);
  534. ClassDB::bind_method(D_METHOD("body_get_space", "body"), &PhysicsServer2D::body_get_space);
  535. ClassDB::bind_method(D_METHOD("body_set_mode", "body", "mode"), &PhysicsServer2D::body_set_mode);
  536. ClassDB::bind_method(D_METHOD("body_get_mode", "body"), &PhysicsServer2D::body_get_mode);
  537. ClassDB::bind_method(D_METHOD("body_add_shape", "body", "shape", "transform", "disabled"), &PhysicsServer2D::body_add_shape, DEFVAL(Transform2D()), DEFVAL(false));
  538. ClassDB::bind_method(D_METHOD("body_set_shape", "body", "shape_idx", "shape"), &PhysicsServer2D::body_set_shape);
  539. ClassDB::bind_method(D_METHOD("body_set_shape_transform", "body", "shape_idx", "transform"), &PhysicsServer2D::body_set_shape_transform);
  540. ClassDB::bind_method(D_METHOD("body_get_shape_count", "body"), &PhysicsServer2D::body_get_shape_count);
  541. ClassDB::bind_method(D_METHOD("body_get_shape", "body", "shape_idx"), &PhysicsServer2D::body_get_shape);
  542. ClassDB::bind_method(D_METHOD("body_get_shape_transform", "body", "shape_idx"), &PhysicsServer2D::body_get_shape_transform);
  543. ClassDB::bind_method(D_METHOD("body_remove_shape", "body", "shape_idx"), &PhysicsServer2D::body_remove_shape);
  544. ClassDB::bind_method(D_METHOD("body_clear_shapes", "body"), &PhysicsServer2D::body_clear_shapes);
  545. ClassDB::bind_method(D_METHOD("body_set_shape_disabled", "body", "shape_idx", "disabled"), &PhysicsServer2D::body_set_shape_disabled);
  546. ClassDB::bind_method(D_METHOD("body_set_shape_as_one_way_collision", "body", "shape_idx", "enable", "margin"), &PhysicsServer2D::body_set_shape_as_one_way_collision);
  547. ClassDB::bind_method(D_METHOD("body_attach_object_instance_id", "body", "id"), &PhysicsServer2D::body_attach_object_instance_id);
  548. ClassDB::bind_method(D_METHOD("body_get_object_instance_id", "body"), &PhysicsServer2D::body_get_object_instance_id);
  549. ClassDB::bind_method(D_METHOD("body_attach_canvas_instance_id", "body", "id"), &PhysicsServer2D::body_attach_canvas_instance_id);
  550. ClassDB::bind_method(D_METHOD("body_get_canvas_instance_id", "body"), &PhysicsServer2D::body_get_canvas_instance_id);
  551. ClassDB::bind_method(D_METHOD("body_set_continuous_collision_detection_mode", "body", "mode"), &PhysicsServer2D::body_set_continuous_collision_detection_mode);
  552. ClassDB::bind_method(D_METHOD("body_get_continuous_collision_detection_mode", "body"), &PhysicsServer2D::body_get_continuous_collision_detection_mode);
  553. ClassDB::bind_method(D_METHOD("body_set_collision_layer", "body", "layer"), &PhysicsServer2D::body_set_collision_layer);
  554. ClassDB::bind_method(D_METHOD("body_get_collision_layer", "body"), &PhysicsServer2D::body_get_collision_layer);
  555. ClassDB::bind_method(D_METHOD("body_set_collision_mask", "body", "mask"), &PhysicsServer2D::body_set_collision_mask);
  556. ClassDB::bind_method(D_METHOD("body_get_collision_mask", "body"), &PhysicsServer2D::body_get_collision_mask);
  557. ClassDB::bind_method(D_METHOD("body_set_collision_priority", "body", "priority"), &PhysicsServer2D::body_set_collision_priority);
  558. ClassDB::bind_method(D_METHOD("body_get_collision_priority", "body"), &PhysicsServer2D::body_get_collision_priority);
  559. ClassDB::bind_method(D_METHOD("body_set_param", "body", "param", "value"), &PhysicsServer2D::body_set_param);
  560. ClassDB::bind_method(D_METHOD("body_get_param", "body", "param"), &PhysicsServer2D::body_get_param);
  561. ClassDB::bind_method(D_METHOD("body_reset_mass_properties", "body"), &PhysicsServer2D::body_reset_mass_properties);
  562. ClassDB::bind_method(D_METHOD("body_set_state", "body", "state", "value"), &PhysicsServer2D::body_set_state);
  563. ClassDB::bind_method(D_METHOD("body_get_state", "body", "state"), &PhysicsServer2D::body_get_state);
  564. ClassDB::bind_method(D_METHOD("body_apply_central_impulse", "body", "impulse"), &PhysicsServer2D::body_apply_central_impulse);
  565. ClassDB::bind_method(D_METHOD("body_apply_torque_impulse", "body", "impulse"), &PhysicsServer2D::body_apply_torque_impulse);
  566. ClassDB::bind_method(D_METHOD("body_apply_impulse", "body", "impulse", "position"), &PhysicsServer2D::body_apply_impulse, Vector2());
  567. ClassDB::bind_method(D_METHOD("body_apply_central_force", "body", "force"), &PhysicsServer2D::body_apply_central_force);
  568. ClassDB::bind_method(D_METHOD("body_apply_force", "body", "force", "position"), &PhysicsServer2D::body_apply_force, Vector2());
  569. ClassDB::bind_method(D_METHOD("body_apply_torque", "body", "torque"), &PhysicsServer2D::body_apply_torque);
  570. ClassDB::bind_method(D_METHOD("body_add_constant_central_force", "body", "force"), &PhysicsServer2D::body_add_constant_central_force);
  571. ClassDB::bind_method(D_METHOD("body_add_constant_force", "body", "force", "position"), &PhysicsServer2D::body_add_constant_force, Vector2());
  572. ClassDB::bind_method(D_METHOD("body_add_constant_torque", "body", "torque"), &PhysicsServer2D::body_add_constant_torque);
  573. ClassDB::bind_method(D_METHOD("body_set_constant_force", "body", "force"), &PhysicsServer2D::body_set_constant_force);
  574. ClassDB::bind_method(D_METHOD("body_get_constant_force", "body"), &PhysicsServer2D::body_get_constant_force);
  575. ClassDB::bind_method(D_METHOD("body_set_constant_torque", "body", "torque"), &PhysicsServer2D::body_set_constant_torque);
  576. ClassDB::bind_method(D_METHOD("body_get_constant_torque", "body"), &PhysicsServer2D::body_get_constant_torque);
  577. ClassDB::bind_method(D_METHOD("body_set_axis_velocity", "body", "axis_velocity"), &PhysicsServer2D::body_set_axis_velocity);
  578. ClassDB::bind_method(D_METHOD("body_add_collision_exception", "body", "excepted_body"), &PhysicsServer2D::body_add_collision_exception);
  579. ClassDB::bind_method(D_METHOD("body_remove_collision_exception", "body", "excepted_body"), &PhysicsServer2D::body_remove_collision_exception);
  580. ClassDB::bind_method(D_METHOD("body_set_max_contacts_reported", "body", "amount"), &PhysicsServer2D::body_set_max_contacts_reported);
  581. ClassDB::bind_method(D_METHOD("body_get_max_contacts_reported", "body"), &PhysicsServer2D::body_get_max_contacts_reported);
  582. ClassDB::bind_method(D_METHOD("body_set_omit_force_integration", "body", "enable"), &PhysicsServer2D::body_set_omit_force_integration);
  583. ClassDB::bind_method(D_METHOD("body_is_omitting_force_integration", "body"), &PhysicsServer2D::body_is_omitting_force_integration);
  584. ClassDB::bind_method(D_METHOD("body_set_force_integration_callback", "body", "callable", "userdata"), &PhysicsServer2D::body_set_force_integration_callback, DEFVAL(Variant()));
  585. ClassDB::bind_method(D_METHOD("body_test_motion", "body", "parameters", "result"), &PhysicsServer2D::_body_test_motion, DEFVAL(Variant()));
  586. ClassDB::bind_method(D_METHOD("body_get_direct_state", "body"), &PhysicsServer2D::body_get_direct_state);
  587. /* JOINT API */
  588. ClassDB::bind_method(D_METHOD("joint_create"), &PhysicsServer2D::joint_create);
  589. ClassDB::bind_method(D_METHOD("joint_clear", "joint"), &PhysicsServer2D::joint_clear);
  590. ClassDB::bind_method(D_METHOD("joint_set_param", "joint", "param", "value"), &PhysicsServer2D::joint_set_param);
  591. ClassDB::bind_method(D_METHOD("joint_get_param", "joint", "param"), &PhysicsServer2D::joint_get_param);
  592. ClassDB::bind_method(D_METHOD("joint_disable_collisions_between_bodies", "joint", "disable"), &PhysicsServer2D::joint_disable_collisions_between_bodies);
  593. ClassDB::bind_method(D_METHOD("joint_is_disabled_collisions_between_bodies", "joint"), &PhysicsServer2D::joint_is_disabled_collisions_between_bodies);
  594. ClassDB::bind_method(D_METHOD("joint_make_pin", "joint", "anchor", "body_a", "body_b"), &PhysicsServer2D::joint_make_pin, DEFVAL(RID()));
  595. ClassDB::bind_method(D_METHOD("joint_make_groove", "joint", "groove1_a", "groove2_a", "anchor_b", "body_a", "body_b"), &PhysicsServer2D::joint_make_groove, DEFVAL(RID()), DEFVAL(RID()));
  596. ClassDB::bind_method(D_METHOD("joint_make_damped_spring", "joint", "anchor_a", "anchor_b", "body_a", "body_b"), &PhysicsServer2D::joint_make_damped_spring, DEFVAL(RID()));
  597. ClassDB::bind_method(D_METHOD("pin_joint_set_param", "joint", "param", "value"), &PhysicsServer2D::pin_joint_set_param);
  598. ClassDB::bind_method(D_METHOD("pin_joint_get_param", "joint", "param"), &PhysicsServer2D::pin_joint_get_param);
  599. ClassDB::bind_method(D_METHOD("damped_spring_joint_set_param", "joint", "param", "value"), &PhysicsServer2D::damped_spring_joint_set_param);
  600. ClassDB::bind_method(D_METHOD("damped_spring_joint_get_param", "joint", "param"), &PhysicsServer2D::damped_spring_joint_get_param);
  601. ClassDB::bind_method(D_METHOD("joint_get_type", "joint"), &PhysicsServer2D::joint_get_type);
  602. ClassDB::bind_method(D_METHOD("free_rid", "rid"), &PhysicsServer2D::free);
  603. ClassDB::bind_method(D_METHOD("set_active", "active"), &PhysicsServer2D::set_active);
  604. ClassDB::bind_method(D_METHOD("get_process_info", "process_info"), &PhysicsServer2D::get_process_info);
  605. BIND_ENUM_CONSTANT(SPACE_PARAM_CONTACT_RECYCLE_RADIUS);
  606. BIND_ENUM_CONSTANT(SPACE_PARAM_CONTACT_MAX_SEPARATION);
  607. BIND_ENUM_CONSTANT(SPACE_PARAM_CONTACT_MAX_ALLOWED_PENETRATION);
  608. BIND_ENUM_CONSTANT(SPACE_PARAM_CONTACT_DEFAULT_BIAS);
  609. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD);
  610. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD);
  611. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_TIME_TO_SLEEP);
  612. BIND_ENUM_CONSTANT(SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS);
  613. BIND_ENUM_CONSTANT(SPACE_PARAM_SOLVER_ITERATIONS);
  614. BIND_ENUM_CONSTANT(SHAPE_WORLD_BOUNDARY);
  615. BIND_ENUM_CONSTANT(SHAPE_SEPARATION_RAY);
  616. BIND_ENUM_CONSTANT(SHAPE_SEGMENT);
  617. BIND_ENUM_CONSTANT(SHAPE_CIRCLE);
  618. BIND_ENUM_CONSTANT(SHAPE_RECTANGLE);
  619. BIND_ENUM_CONSTANT(SHAPE_CAPSULE);
  620. BIND_ENUM_CONSTANT(SHAPE_CONVEX_POLYGON);
  621. BIND_ENUM_CONSTANT(SHAPE_CONCAVE_POLYGON);
  622. BIND_ENUM_CONSTANT(SHAPE_CUSTOM);
  623. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_OVERRIDE_MODE);
  624. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY);
  625. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_VECTOR);
  626. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_IS_POINT);
  627. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_POINT_UNIT_DISTANCE);
  628. BIND_ENUM_CONSTANT(AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE);
  629. BIND_ENUM_CONSTANT(AREA_PARAM_LINEAR_DAMP);
  630. BIND_ENUM_CONSTANT(AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE);
  631. BIND_ENUM_CONSTANT(AREA_PARAM_ANGULAR_DAMP);
  632. BIND_ENUM_CONSTANT(AREA_PARAM_PRIORITY);
  633. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_DISABLED);
  634. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_COMBINE);
  635. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_COMBINE_REPLACE);
  636. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_REPLACE);
  637. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_REPLACE_COMBINE);
  638. BIND_ENUM_CONSTANT(BODY_MODE_STATIC);
  639. BIND_ENUM_CONSTANT(BODY_MODE_KINEMATIC);
  640. BIND_ENUM_CONSTANT(BODY_MODE_RIGID);
  641. BIND_ENUM_CONSTANT(BODY_MODE_RIGID_LINEAR);
  642. BIND_ENUM_CONSTANT(BODY_PARAM_BOUNCE);
  643. BIND_ENUM_CONSTANT(BODY_PARAM_FRICTION);
  644. BIND_ENUM_CONSTANT(BODY_PARAM_MASS);
  645. BIND_ENUM_CONSTANT(BODY_PARAM_INERTIA);
  646. BIND_ENUM_CONSTANT(BODY_PARAM_CENTER_OF_MASS);
  647. BIND_ENUM_CONSTANT(BODY_PARAM_GRAVITY_SCALE);
  648. BIND_ENUM_CONSTANT(BODY_PARAM_LINEAR_DAMP_MODE);
  649. BIND_ENUM_CONSTANT(BODY_PARAM_ANGULAR_DAMP_MODE);
  650. BIND_ENUM_CONSTANT(BODY_PARAM_LINEAR_DAMP);
  651. BIND_ENUM_CONSTANT(BODY_PARAM_ANGULAR_DAMP);
  652. BIND_ENUM_CONSTANT(BODY_PARAM_MAX);
  653. BIND_ENUM_CONSTANT(BODY_DAMP_MODE_COMBINE);
  654. BIND_ENUM_CONSTANT(BODY_DAMP_MODE_REPLACE);
  655. BIND_ENUM_CONSTANT(BODY_STATE_TRANSFORM);
  656. BIND_ENUM_CONSTANT(BODY_STATE_LINEAR_VELOCITY);
  657. BIND_ENUM_CONSTANT(BODY_STATE_ANGULAR_VELOCITY);
  658. BIND_ENUM_CONSTANT(BODY_STATE_SLEEPING);
  659. BIND_ENUM_CONSTANT(BODY_STATE_CAN_SLEEP);
  660. BIND_ENUM_CONSTANT(JOINT_TYPE_PIN);
  661. BIND_ENUM_CONSTANT(JOINT_TYPE_GROOVE);
  662. BIND_ENUM_CONSTANT(JOINT_TYPE_DAMPED_SPRING);
  663. BIND_ENUM_CONSTANT(JOINT_TYPE_MAX);
  664. BIND_ENUM_CONSTANT(JOINT_PARAM_BIAS);
  665. BIND_ENUM_CONSTANT(JOINT_PARAM_MAX_BIAS);
  666. BIND_ENUM_CONSTANT(JOINT_PARAM_MAX_FORCE);
  667. BIND_ENUM_CONSTANT(PIN_JOINT_SOFTNESS);
  668. BIND_ENUM_CONSTANT(DAMPED_SPRING_REST_LENGTH);
  669. BIND_ENUM_CONSTANT(DAMPED_SPRING_STIFFNESS);
  670. BIND_ENUM_CONSTANT(DAMPED_SPRING_DAMPING);
  671. BIND_ENUM_CONSTANT(CCD_MODE_DISABLED);
  672. BIND_ENUM_CONSTANT(CCD_MODE_CAST_RAY);
  673. BIND_ENUM_CONSTANT(CCD_MODE_CAST_SHAPE);
  674. BIND_ENUM_CONSTANT(AREA_BODY_ADDED);
  675. BIND_ENUM_CONSTANT(AREA_BODY_REMOVED);
  676. BIND_ENUM_CONSTANT(INFO_ACTIVE_OBJECTS);
  677. BIND_ENUM_CONSTANT(INFO_COLLISION_PAIRS);
  678. BIND_ENUM_CONSTANT(INFO_ISLAND_COUNT);
  679. }
  680. PhysicsServer2D::PhysicsServer2D() {
  681. singleton = this;
  682. // World2D physics space
  683. GLOBAL_DEF_BASIC("physics/2d/default_gravity", 980.0);
  684. GLOBAL_DEF_BASIC("physics/2d/default_gravity_vector", Vector2(0, 1));
  685. GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/2d/default_linear_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"), 0.1);
  686. GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/2d/default_angular_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"), 1.0);
  687. // PhysicsServer2D
  688. GLOBAL_DEF("physics/2d/sleep_threshold_linear", 2.0);
  689. GLOBAL_DEF("physics/2d/sleep_threshold_angular", Math::deg_to_rad(8.0));
  690. GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/2d/time_before_sleep", PROPERTY_HINT_RANGE, "0,5,0.01,or_greater"), 0.5);
  691. GLOBAL_DEF(PropertyInfo(Variant::INT, "physics/2d/solver/solver_iterations", PROPERTY_HINT_RANGE, "1,32,1,or_greater"), 16);
  692. GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/2d/solver/contact_recycle_radius", PROPERTY_HINT_RANGE, "0,10,0.01,or_greater"), 1.0);
  693. GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/2d/solver/contact_max_separation", PROPERTY_HINT_RANGE, "0,10,0.01,or_greater"), 1.5);
  694. GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/2d/solver/contact_max_allowed_penetration", PROPERTY_HINT_RANGE, "0.01,10,0.01,or_greater"), 0.3);
  695. GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/2d/solver/default_contact_bias", PROPERTY_HINT_RANGE, "0,1,0.01"), 0.8);
  696. GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/2d/solver/default_constraint_bias", PROPERTY_HINT_RANGE, "0,1,0.01"), 0.2);
  697. }
  698. PhysicsServer2D::~PhysicsServer2D() {
  699. singleton = nullptr;
  700. }
  701. PhysicsServer2DManager *PhysicsServer2DManager::singleton = nullptr;
  702. const String PhysicsServer2DManager::setting_property_name(PNAME("physics/2d/physics_engine"));
  703. void PhysicsServer2DManager::on_servers_changed() {
  704. String physics_servers("DEFAULT");
  705. for (int i = get_servers_count() - 1; 0 <= i; --i) {
  706. physics_servers += "," + get_server_name(i);
  707. }
  708. ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, setting_property_name, PROPERTY_HINT_ENUM, physics_servers));
  709. }
  710. void PhysicsServer2DManager::_bind_methods() {
  711. ClassDB::bind_method(D_METHOD("register_server", "name", "create_callback"), &PhysicsServer2DManager::register_server);
  712. ClassDB::bind_method(D_METHOD("set_default_server", "name", "priority"), &PhysicsServer2DManager::set_default_server);
  713. }
  714. PhysicsServer2DManager *PhysicsServer2DManager::get_singleton() {
  715. return singleton;
  716. }
  717. void PhysicsServer2DManager::register_server(const String &p_name, const Callable &p_create_callback) {
  718. //ERR_FAIL_COND(!p_create_callback.is_valid());
  719. ERR_FAIL_COND(find_server_id(p_name) != -1);
  720. physics_2d_servers.push_back(ClassInfo(p_name, p_create_callback));
  721. on_servers_changed();
  722. }
  723. void PhysicsServer2DManager::set_default_server(const String &p_name, int p_priority) {
  724. const int id = find_server_id(p_name);
  725. ERR_FAIL_COND(id == -1); // Not found
  726. if (default_server_priority < p_priority) {
  727. default_server_id = id;
  728. default_server_priority = p_priority;
  729. }
  730. }
  731. int PhysicsServer2DManager::find_server_id(const String &p_name) {
  732. for (int i = physics_2d_servers.size() - 1; 0 <= i; --i) {
  733. if (p_name == physics_2d_servers[i].name) {
  734. return i;
  735. }
  736. }
  737. return -1;
  738. }
  739. int PhysicsServer2DManager::get_servers_count() {
  740. return physics_2d_servers.size();
  741. }
  742. String PhysicsServer2DManager::get_server_name(int p_id) {
  743. ERR_FAIL_INDEX_V(p_id, get_servers_count(), "");
  744. return physics_2d_servers[p_id].name;
  745. }
  746. PhysicsServer2D *PhysicsServer2DManager::new_default_server() {
  747. ERR_FAIL_COND_V(default_server_id == -1, nullptr);
  748. Variant ret;
  749. Callable::CallError ce;
  750. physics_2d_servers[default_server_id].create_callback.callp(nullptr, 0, ret, ce);
  751. ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr);
  752. return Object::cast_to<PhysicsServer2D>(ret.get_validated_object());
  753. }
  754. PhysicsServer2D *PhysicsServer2DManager::new_server(const String &p_name) {
  755. int id = find_server_id(p_name);
  756. if (id == -1) {
  757. return nullptr;
  758. } else {
  759. Variant ret;
  760. Callable::CallError ce;
  761. physics_2d_servers[id].create_callback.callp(nullptr, 0, ret, ce);
  762. ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr);
  763. return Object::cast_to<PhysicsServer2D>(ret.get_validated_object());
  764. }
  765. }
  766. PhysicsServer2DManager::PhysicsServer2DManager() {
  767. singleton = this;
  768. }
  769. PhysicsServer2DManager::~PhysicsServer2DManager() {
  770. singleton = nullptr;
  771. }