physics_server_2d.cpp 46 KB

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