physics_server_2d.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_inverse_mass"), &PhysicsDirectBodyState2D::get_inverse_mass);
  65. ClassDB::bind_method(D_METHOD("get_inverse_inertia"), &PhysicsDirectBodyState2D::get_inverse_inertia);
  66. ClassDB::bind_method(D_METHOD("set_linear_velocity", "velocity"), &PhysicsDirectBodyState2D::set_linear_velocity);
  67. ClassDB::bind_method(D_METHOD("get_linear_velocity"), &PhysicsDirectBodyState2D::get_linear_velocity);
  68. ClassDB::bind_method(D_METHOD("set_angular_velocity", "velocity"), &PhysicsDirectBodyState2D::set_angular_velocity);
  69. ClassDB::bind_method(D_METHOD("get_angular_velocity"), &PhysicsDirectBodyState2D::get_angular_velocity);
  70. ClassDB::bind_method(D_METHOD("set_transform", "transform"), &PhysicsDirectBodyState2D::set_transform);
  71. ClassDB::bind_method(D_METHOD("get_transform"), &PhysicsDirectBodyState2D::get_transform);
  72. ClassDB::bind_method(D_METHOD("add_central_force", "force"), &PhysicsDirectBodyState2D::add_central_force);
  73. ClassDB::bind_method(D_METHOD("add_force", "force", "position"), &PhysicsDirectBodyState2D::add_force, Vector2());
  74. ClassDB::bind_method(D_METHOD("add_torque", "torque"), &PhysicsDirectBodyState2D::add_torque);
  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("set_sleep_state", "enabled"), &PhysicsDirectBodyState2D::set_sleep_state);
  79. ClassDB::bind_method(D_METHOD("is_sleeping"), &PhysicsDirectBodyState2D::is_sleeping);
  80. ClassDB::bind_method(D_METHOD("get_contact_count"), &PhysicsDirectBodyState2D::get_contact_count);
  81. ClassDB::bind_method(D_METHOD("get_contact_local_position", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_local_position);
  82. ClassDB::bind_method(D_METHOD("get_contact_local_normal", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_local_normal);
  83. ClassDB::bind_method(D_METHOD("get_contact_local_shape", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_local_shape);
  84. ClassDB::bind_method(D_METHOD("get_contact_collider", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider);
  85. ClassDB::bind_method(D_METHOD("get_contact_collider_position", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider_position);
  86. ClassDB::bind_method(D_METHOD("get_contact_collider_id", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider_id);
  87. ClassDB::bind_method(D_METHOD("get_contact_collider_object", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider_object);
  88. ClassDB::bind_method(D_METHOD("get_contact_collider_shape", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider_shape);
  89. ClassDB::bind_method(D_METHOD("get_contact_collider_shape_metadata", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider_shape_metadata);
  90. ClassDB::bind_method(D_METHOD("get_contact_collider_velocity_at_position", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider_velocity_at_position);
  91. ClassDB::bind_method(D_METHOD("get_step"), &PhysicsDirectBodyState2D::get_step);
  92. ClassDB::bind_method(D_METHOD("integrate_forces"), &PhysicsDirectBodyState2D::integrate_forces);
  93. ClassDB::bind_method(D_METHOD("get_space_state"), &PhysicsDirectBodyState2D::get_space_state);
  94. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "step"), "", "get_step");
  95. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inverse_mass"), "", "get_inverse_mass");
  96. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inverse_inertia"), "", "get_inverse_inertia");
  97. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "total_angular_damp"), "", "get_total_angular_damp");
  98. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "total_linear_damp"), "", "get_total_linear_damp");
  99. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "total_gravity"), "", "get_total_gravity");
  100. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_velocity"), "set_angular_velocity", "get_angular_velocity");
  101. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "linear_velocity"), "set_linear_velocity", "get_linear_velocity");
  102. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sleeping"), "set_sleep_state", "is_sleeping");
  103. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "transform"), "set_transform", "get_transform");
  104. }
  105. PhysicsDirectBodyState2D::PhysicsDirectBodyState2D() {}
  106. ///////////////////////////////////////////////////////
  107. void PhysicsShapeQueryParameters2D::set_shape(const RES &p_shape_ref) {
  108. ERR_FAIL_COND(p_shape_ref.is_null());
  109. shape_ref = p_shape_ref;
  110. shape = p_shape_ref->get_rid();
  111. }
  112. RES PhysicsShapeQueryParameters2D::get_shape() const {
  113. return shape_ref;
  114. }
  115. void PhysicsShapeQueryParameters2D::set_shape_rid(const RID &p_shape) {
  116. if (shape != p_shape) {
  117. shape_ref = RES();
  118. shape = p_shape;
  119. }
  120. }
  121. RID PhysicsShapeQueryParameters2D::get_shape_rid() const {
  122. return shape;
  123. }
  124. void PhysicsShapeQueryParameters2D::set_transform(const Transform2D &p_transform) {
  125. transform = p_transform;
  126. }
  127. Transform2D PhysicsShapeQueryParameters2D::get_transform() const {
  128. return transform;
  129. }
  130. void PhysicsShapeQueryParameters2D::set_motion(const Vector2 &p_motion) {
  131. motion = p_motion;
  132. }
  133. Vector2 PhysicsShapeQueryParameters2D::get_motion() const {
  134. return motion;
  135. }
  136. void PhysicsShapeQueryParameters2D::set_margin(real_t p_margin) {
  137. margin = p_margin;
  138. }
  139. real_t PhysicsShapeQueryParameters2D::get_margin() const {
  140. return margin;
  141. }
  142. void PhysicsShapeQueryParameters2D::set_collision_mask(int p_collision_mask) {
  143. collision_mask = p_collision_mask;
  144. }
  145. int PhysicsShapeQueryParameters2D::get_collision_mask() const {
  146. return collision_mask;
  147. }
  148. void PhysicsShapeQueryParameters2D::set_exclude(const Vector<RID> &p_exclude) {
  149. exclude.clear();
  150. for (int i = 0; i < p_exclude.size(); i++) {
  151. exclude.insert(p_exclude[i]);
  152. }
  153. }
  154. Vector<RID> PhysicsShapeQueryParameters2D::get_exclude() const {
  155. Vector<RID> ret;
  156. ret.resize(exclude.size());
  157. int idx = 0;
  158. for (Set<RID>::Element *E = exclude.front(); E; E = E->next()) {
  159. ret.write[idx] = E->get();
  160. }
  161. return ret;
  162. }
  163. void PhysicsShapeQueryParameters2D::set_collide_with_bodies(bool p_enable) {
  164. collide_with_bodies = p_enable;
  165. }
  166. bool PhysicsShapeQueryParameters2D::is_collide_with_bodies_enabled() const {
  167. return collide_with_bodies;
  168. }
  169. void PhysicsShapeQueryParameters2D::set_collide_with_areas(bool p_enable) {
  170. collide_with_areas = p_enable;
  171. }
  172. bool PhysicsShapeQueryParameters2D::is_collide_with_areas_enabled() const {
  173. return collide_with_areas;
  174. }
  175. void PhysicsShapeQueryParameters2D::_bind_methods() {
  176. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &PhysicsShapeQueryParameters2D::set_shape);
  177. ClassDB::bind_method(D_METHOD("get_shape"), &PhysicsShapeQueryParameters2D::get_shape);
  178. ClassDB::bind_method(D_METHOD("set_shape_rid", "shape"), &PhysicsShapeQueryParameters2D::set_shape_rid);
  179. ClassDB::bind_method(D_METHOD("get_shape_rid"), &PhysicsShapeQueryParameters2D::get_shape_rid);
  180. ClassDB::bind_method(D_METHOD("set_transform", "transform"), &PhysicsShapeQueryParameters2D::set_transform);
  181. ClassDB::bind_method(D_METHOD("get_transform"), &PhysicsShapeQueryParameters2D::get_transform);
  182. ClassDB::bind_method(D_METHOD("set_motion", "motion"), &PhysicsShapeQueryParameters2D::set_motion);
  183. ClassDB::bind_method(D_METHOD("get_motion"), &PhysicsShapeQueryParameters2D::get_motion);
  184. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &PhysicsShapeQueryParameters2D::set_margin);
  185. ClassDB::bind_method(D_METHOD("get_margin"), &PhysicsShapeQueryParameters2D::get_margin);
  186. ClassDB::bind_method(D_METHOD("set_collision_layer", "collision_layer"), &PhysicsShapeQueryParameters2D::set_collision_mask);
  187. ClassDB::bind_method(D_METHOD("get_collision_layer"), &PhysicsShapeQueryParameters2D::get_collision_mask);
  188. ClassDB::bind_method(D_METHOD("set_exclude", "exclude"), &PhysicsShapeQueryParameters2D::set_exclude);
  189. ClassDB::bind_method(D_METHOD("get_exclude"), &PhysicsShapeQueryParameters2D::get_exclude);
  190. ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &PhysicsShapeQueryParameters2D::set_collide_with_bodies);
  191. ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &PhysicsShapeQueryParameters2D::is_collide_with_bodies_enabled);
  192. ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &PhysicsShapeQueryParameters2D::set_collide_with_areas);
  193. ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &PhysicsShapeQueryParameters2D::is_collide_with_areas_enabled);
  194. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_layer", "get_collision_layer");
  195. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude", PROPERTY_HINT_NONE, itos(Variant::RID) + ":"), "set_exclude", "get_exclude");
  196. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin", PROPERTY_HINT_RANGE, "0,100,0.01"), "set_margin", "get_margin");
  197. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion"), "set_motion", "get_motion");
  198. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");
  199. ADD_PROPERTY(PropertyInfo(Variant::RID, "shape_rid"), "set_shape_rid", "get_shape_rid");
  200. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "transform"), "set_transform", "get_transform");
  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. PhysicsShapeQueryParameters2D::PhysicsShapeQueryParameters2D() {
  205. margin = 0;
  206. collision_mask = 0x7FFFFFFF;
  207. collide_with_bodies = true;
  208. collide_with_areas = false;
  209. }
  210. Dictionary PhysicsDirectSpaceState2D::_intersect_ray(const Vector2 &p_from, const Vector2 &p_to, const Vector<RID> &p_exclude, uint32_t p_layers, bool p_collide_with_bodies, bool p_collide_with_areas) {
  211. RayResult inters;
  212. Set<RID> exclude;
  213. for (int i = 0; i < p_exclude.size(); i++) {
  214. exclude.insert(p_exclude[i]);
  215. }
  216. bool res = intersect_ray(p_from, p_to, inters, exclude, p_layers, p_collide_with_bodies, p_collide_with_areas);
  217. if (!res) {
  218. return Dictionary();
  219. }
  220. Dictionary d;
  221. d["position"] = inters.position;
  222. d["normal"] = inters.normal;
  223. d["collider_id"] = inters.collider_id;
  224. d["collider"] = inters.collider;
  225. d["shape"] = inters.shape;
  226. d["rid"] = inters.rid;
  227. d["metadata"] = inters.metadata;
  228. return d;
  229. }
  230. Array PhysicsDirectSpaceState2D::_intersect_shape(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query, int p_max_results) {
  231. ERR_FAIL_COND_V(!p_shape_query.is_valid(), Array());
  232. Vector<ShapeResult> sr;
  233. sr.resize(p_max_results);
  234. int rc = intersect_shape(p_shape_query->shape, p_shape_query->transform, p_shape_query->motion, p_shape_query->margin, sr.ptrw(), sr.size(), p_shape_query->exclude, p_shape_query->collision_mask, p_shape_query->collide_with_bodies, p_shape_query->collide_with_areas);
  235. Array ret;
  236. ret.resize(rc);
  237. for (int i = 0; i < rc; i++) {
  238. Dictionary d;
  239. d["rid"] = sr[i].rid;
  240. d["collider_id"] = sr[i].collider_id;
  241. d["collider"] = sr[i].collider;
  242. d["shape"] = sr[i].shape;
  243. d["metadata"] = sr[i].metadata;
  244. ret[i] = d;
  245. }
  246. return ret;
  247. }
  248. Array PhysicsDirectSpaceState2D::_cast_motion(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query) {
  249. ERR_FAIL_COND_V(!p_shape_query.is_valid(), Array());
  250. real_t closest_safe, closest_unsafe;
  251. bool res = cast_motion(p_shape_query->shape, p_shape_query->transform, p_shape_query->motion, p_shape_query->margin, closest_safe, closest_unsafe, p_shape_query->exclude, p_shape_query->collision_mask, p_shape_query->collide_with_bodies, p_shape_query->collide_with_areas);
  252. if (!res) {
  253. return Array();
  254. }
  255. Array ret;
  256. ret.resize(2);
  257. ret[0] = closest_safe;
  258. ret[1] = closest_unsafe;
  259. return ret;
  260. }
  261. Array PhysicsDirectSpaceState2D::_intersect_point_impl(const Vector2 &p_point, int p_max_results, const Vector<RID> &p_exclude, uint32_t p_layers, bool p_collide_with_bodies, bool p_collide_with_areas, bool p_filter_by_canvas, ObjectID p_canvas_instance_id) {
  262. Set<RID> exclude;
  263. for (int i = 0; i < p_exclude.size(); i++) {
  264. exclude.insert(p_exclude[i]);
  265. }
  266. Vector<ShapeResult> ret;
  267. ret.resize(p_max_results);
  268. int rc;
  269. if (p_filter_by_canvas) {
  270. rc = intersect_point(p_point, ret.ptrw(), ret.size(), exclude, p_layers, p_collide_with_bodies, p_collide_with_areas);
  271. } else {
  272. rc = intersect_point_on_canvas(p_point, p_canvas_instance_id, ret.ptrw(), ret.size(), exclude, p_layers, p_collide_with_bodies, p_collide_with_areas);
  273. }
  274. if (rc == 0) {
  275. return Array();
  276. }
  277. Array r;
  278. r.resize(rc);
  279. for (int i = 0; i < rc; i++) {
  280. Dictionary d;
  281. d["rid"] = ret[i].rid;
  282. d["collider_id"] = ret[i].collider_id;
  283. d["collider"] = ret[i].collider;
  284. d["shape"] = ret[i].shape;
  285. d["metadata"] = ret[i].metadata;
  286. r[i] = d;
  287. }
  288. return r;
  289. }
  290. Array PhysicsDirectSpaceState2D::_intersect_point(const Vector2 &p_point, int p_max_results, const Vector<RID> &p_exclude, uint32_t p_layers, bool p_collide_with_bodies, bool p_collide_with_areas) {
  291. return _intersect_point_impl(p_point, p_max_results, p_exclude, p_layers, p_collide_with_bodies, p_collide_with_areas);
  292. }
  293. Array PhysicsDirectSpaceState2D::_intersect_point_on_canvas(const Vector2 &p_point, ObjectID p_canvas_intance_id, int p_max_results, const Vector<RID> &p_exclude, uint32_t p_layers, bool p_collide_with_bodies, bool p_collide_with_areas) {
  294. return _intersect_point_impl(p_point, p_max_results, p_exclude, p_layers, p_collide_with_bodies, p_collide_with_areas, true, p_canvas_intance_id);
  295. }
  296. Array PhysicsDirectSpaceState2D::_collide_shape(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query, int p_max_results) {
  297. ERR_FAIL_COND_V(!p_shape_query.is_valid(), Array());
  298. Vector<Vector2> ret;
  299. ret.resize(p_max_results * 2);
  300. int rc = 0;
  301. bool res = collide_shape(p_shape_query->shape, p_shape_query->transform, p_shape_query->motion, p_shape_query->margin, ret.ptrw(), p_max_results, rc, p_shape_query->exclude, p_shape_query->collision_mask, p_shape_query->collide_with_bodies, p_shape_query->collide_with_areas);
  302. if (!res) {
  303. return Array();
  304. }
  305. Array r;
  306. r.resize(rc * 2);
  307. for (int i = 0; i < rc * 2; i++) {
  308. r[i] = ret[i];
  309. }
  310. return r;
  311. }
  312. Dictionary PhysicsDirectSpaceState2D::_get_rest_info(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query) {
  313. ERR_FAIL_COND_V(!p_shape_query.is_valid(), Dictionary());
  314. ShapeRestInfo sri;
  315. bool res = rest_info(p_shape_query->shape, p_shape_query->transform, p_shape_query->motion, p_shape_query->margin, &sri, p_shape_query->exclude, p_shape_query->collision_mask, p_shape_query->collide_with_bodies, p_shape_query->collide_with_areas);
  316. Dictionary r;
  317. if (!res) {
  318. return r;
  319. }
  320. r["point"] = sri.point;
  321. r["normal"] = sri.normal;
  322. r["rid"] = sri.rid;
  323. r["collider_id"] = sri.collider_id;
  324. r["shape"] = sri.shape;
  325. r["linear_velocity"] = sri.linear_velocity;
  326. r["metadata"] = sri.metadata;
  327. return r;
  328. }
  329. PhysicsDirectSpaceState2D::PhysicsDirectSpaceState2D() {
  330. }
  331. void PhysicsDirectSpaceState2D::_bind_methods() {
  332. ClassDB::bind_method(D_METHOD("intersect_point", "point", "max_results", "exclude", "collision_layer", "collide_with_bodies", "collide_with_areas"), &PhysicsDirectSpaceState2D::_intersect_point, DEFVAL(32), DEFVAL(Array()), DEFVAL(0x7FFFFFFF), DEFVAL(true), DEFVAL(false));
  333. ClassDB::bind_method(D_METHOD("intersect_point_on_canvas", "point", "canvas_instance_id", "max_results", "exclude", "collision_layer", "collide_with_bodies", "collide_with_areas"), &PhysicsDirectSpaceState2D::_intersect_point_on_canvas, DEFVAL(32), DEFVAL(Array()), DEFVAL(0x7FFFFFFF), DEFVAL(true), DEFVAL(false));
  334. ClassDB::bind_method(D_METHOD("intersect_ray", "from", "to", "exclude", "collision_layer", "collide_with_bodies", "collide_with_areas"), &PhysicsDirectSpaceState2D::_intersect_ray, DEFVAL(Array()), DEFVAL(0x7FFFFFFF), DEFVAL(true), DEFVAL(false));
  335. ClassDB::bind_method(D_METHOD("intersect_shape", "shape", "max_results"), &PhysicsDirectSpaceState2D::_intersect_shape, DEFVAL(32));
  336. ClassDB::bind_method(D_METHOD("cast_motion", "shape"), &PhysicsDirectSpaceState2D::_cast_motion);
  337. ClassDB::bind_method(D_METHOD("collide_shape", "shape", "max_results"), &PhysicsDirectSpaceState2D::_collide_shape, DEFVAL(32));
  338. ClassDB::bind_method(D_METHOD("get_rest_info", "shape"), &PhysicsDirectSpaceState2D::_get_rest_info);
  339. }
  340. int PhysicsShapeQueryResult2D::get_result_count() const {
  341. return result.size();
  342. }
  343. RID PhysicsShapeQueryResult2D::get_result_rid(int p_idx) const {
  344. return result[p_idx].rid;
  345. }
  346. ObjectID PhysicsShapeQueryResult2D::get_result_object_id(int p_idx) const {
  347. return result[p_idx].collider_id;
  348. }
  349. Object *PhysicsShapeQueryResult2D::get_result_object(int p_idx) const {
  350. return result[p_idx].collider;
  351. }
  352. int PhysicsShapeQueryResult2D::get_result_object_shape(int p_idx) const {
  353. return result[p_idx].shape;
  354. }
  355. PhysicsShapeQueryResult2D::PhysicsShapeQueryResult2D() {
  356. }
  357. void PhysicsShapeQueryResult2D::_bind_methods() {
  358. ClassDB::bind_method(D_METHOD("get_result_count"), &PhysicsShapeQueryResult2D::get_result_count);
  359. ClassDB::bind_method(D_METHOD("get_result_rid", "idx"), &PhysicsShapeQueryResult2D::get_result_rid);
  360. ClassDB::bind_method(D_METHOD("get_result_object_id", "idx"), &PhysicsShapeQueryResult2D::get_result_object_id);
  361. ClassDB::bind_method(D_METHOD("get_result_object", "idx"), &PhysicsShapeQueryResult2D::get_result_object);
  362. ClassDB::bind_method(D_METHOD("get_result_object_shape", "idx"), &PhysicsShapeQueryResult2D::get_result_object_shape);
  363. }
  364. ///////////////////////////////
  365. Vector2 PhysicsTestMotionResult2D::get_motion() const {
  366. return result.motion;
  367. }
  368. Vector2 PhysicsTestMotionResult2D::get_motion_remainder() const {
  369. return result.remainder;
  370. }
  371. Vector2 PhysicsTestMotionResult2D::get_collision_point() const {
  372. return result.collision_point;
  373. }
  374. Vector2 PhysicsTestMotionResult2D::get_collision_normal() const {
  375. return result.collision_normal;
  376. }
  377. Vector2 PhysicsTestMotionResult2D::get_collider_velocity() const {
  378. return result.collider_velocity;
  379. }
  380. ObjectID PhysicsTestMotionResult2D::get_collider_id() const {
  381. return result.collider_id;
  382. }
  383. RID PhysicsTestMotionResult2D::get_collider_rid() const {
  384. return result.collider;
  385. }
  386. Object *PhysicsTestMotionResult2D::get_collider() const {
  387. return ObjectDB::get_instance(result.collider_id);
  388. }
  389. int PhysicsTestMotionResult2D::get_collider_shape() const {
  390. return result.collider_shape;
  391. }
  392. void PhysicsTestMotionResult2D::_bind_methods() {
  393. ClassDB::bind_method(D_METHOD("get_motion"), &PhysicsTestMotionResult2D::get_motion);
  394. ClassDB::bind_method(D_METHOD("get_motion_remainder"), &PhysicsTestMotionResult2D::get_motion_remainder);
  395. ClassDB::bind_method(D_METHOD("get_collision_point"), &PhysicsTestMotionResult2D::get_collision_point);
  396. ClassDB::bind_method(D_METHOD("get_collision_normal"), &PhysicsTestMotionResult2D::get_collision_normal);
  397. ClassDB::bind_method(D_METHOD("get_collider_velocity"), &PhysicsTestMotionResult2D::get_collider_velocity);
  398. ClassDB::bind_method(D_METHOD("get_collider_id"), &PhysicsTestMotionResult2D::get_collider_id);
  399. ClassDB::bind_method(D_METHOD("get_collider_rid"), &PhysicsTestMotionResult2D::get_collider_rid);
  400. ClassDB::bind_method(D_METHOD("get_collider"), &PhysicsTestMotionResult2D::get_collider);
  401. ClassDB::bind_method(D_METHOD("get_collider_shape"), &PhysicsTestMotionResult2D::get_collider_shape);
  402. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion"), "", "get_motion");
  403. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion_remainder"), "", "get_motion_remainder");
  404. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "collision_point"), "", "get_collision_point");
  405. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "collision_normal"), "", "get_collision_normal");
  406. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "collider_velocity"), "", "get_collider_velocity");
  407. ADD_PROPERTY(PropertyInfo(Variant::INT, "collider_id", PROPERTY_HINT_OBJECT_ID), "", "get_collider_id");
  408. ADD_PROPERTY(PropertyInfo(Variant::RID, "collider_rid"), "", "get_collider_rid");
  409. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "collider"), "", "get_collider");
  410. ADD_PROPERTY(PropertyInfo(Variant::INT, "collider_shape"), "", "get_collider_shape");
  411. }
  412. PhysicsTestMotionResult2D::PhysicsTestMotionResult2D() {
  413. colliding = false;
  414. result.collider_shape = 0;
  415. }
  416. ///////////////////////////////////////
  417. bool PhysicsServer2D::_body_test_motion(RID p_body, const Transform2D &p_from, const Vector2 &p_motion, bool p_infinite_inertia, real_t p_margin, const Ref<PhysicsTestMotionResult2D> &p_result) {
  418. MotionResult *r = nullptr;
  419. if (p_result.is_valid()) {
  420. r = p_result->get_result_ptr();
  421. }
  422. return body_test_motion(p_body, p_from, p_motion, p_infinite_inertia, p_margin, r);
  423. }
  424. void PhysicsServer2D::_bind_methods() {
  425. ClassDB::bind_method(D_METHOD("line_shape_create"), &PhysicsServer2D::line_shape_create);
  426. ClassDB::bind_method(D_METHOD("ray_shape_create"), &PhysicsServer2D::ray_shape_create);
  427. ClassDB::bind_method(D_METHOD("segment_shape_create"), &PhysicsServer2D::segment_shape_create);
  428. ClassDB::bind_method(D_METHOD("circle_shape_create"), &PhysicsServer2D::circle_shape_create);
  429. ClassDB::bind_method(D_METHOD("rectangle_shape_create"), &PhysicsServer2D::rectangle_shape_create);
  430. ClassDB::bind_method(D_METHOD("capsule_shape_create"), &PhysicsServer2D::capsule_shape_create);
  431. ClassDB::bind_method(D_METHOD("convex_polygon_shape_create"), &PhysicsServer2D::convex_polygon_shape_create);
  432. ClassDB::bind_method(D_METHOD("concave_polygon_shape_create"), &PhysicsServer2D::concave_polygon_shape_create);
  433. ClassDB::bind_method(D_METHOD("shape_set_data", "shape", "data"), &PhysicsServer2D::shape_set_data);
  434. ClassDB::bind_method(D_METHOD("shape_get_type", "shape"), &PhysicsServer2D::shape_get_type);
  435. ClassDB::bind_method(D_METHOD("shape_get_data", "shape"), &PhysicsServer2D::shape_get_data);
  436. ClassDB::bind_method(D_METHOD("space_create"), &PhysicsServer2D::space_create);
  437. ClassDB::bind_method(D_METHOD("space_set_active", "space", "active"), &PhysicsServer2D::space_set_active);
  438. ClassDB::bind_method(D_METHOD("space_is_active", "space"), &PhysicsServer2D::space_is_active);
  439. ClassDB::bind_method(D_METHOD("space_set_param", "space", "param", "value"), &PhysicsServer2D::space_set_param);
  440. ClassDB::bind_method(D_METHOD("space_get_param", "space", "param"), &PhysicsServer2D::space_get_param);
  441. ClassDB::bind_method(D_METHOD("space_get_direct_state", "space"), &PhysicsServer2D::space_get_direct_state);
  442. ClassDB::bind_method(D_METHOD("area_create"), &PhysicsServer2D::area_create);
  443. ClassDB::bind_method(D_METHOD("area_set_space", "area", "space"), &PhysicsServer2D::area_set_space);
  444. ClassDB::bind_method(D_METHOD("area_get_space", "area"), &PhysicsServer2D::area_get_space);
  445. ClassDB::bind_method(D_METHOD("area_set_space_override_mode", "area", "mode"), &PhysicsServer2D::area_set_space_override_mode);
  446. ClassDB::bind_method(D_METHOD("area_get_space_override_mode", "area"), &PhysicsServer2D::area_get_space_override_mode);
  447. ClassDB::bind_method(D_METHOD("area_add_shape", "area", "shape", "transform", "disabled"), &PhysicsServer2D::area_add_shape, DEFVAL(Transform2D()), DEFVAL(false));
  448. ClassDB::bind_method(D_METHOD("area_set_shape", "area", "shape_idx", "shape"), &PhysicsServer2D::area_set_shape);
  449. ClassDB::bind_method(D_METHOD("area_set_shape_transform", "area", "shape_idx", "transform"), &PhysicsServer2D::area_set_shape_transform);
  450. ClassDB::bind_method(D_METHOD("area_set_shape_disabled", "area", "shape_idx", "disabled"), &PhysicsServer2D::area_set_shape_disabled);
  451. ClassDB::bind_method(D_METHOD("area_get_shape_count", "area"), &PhysicsServer2D::area_get_shape_count);
  452. ClassDB::bind_method(D_METHOD("area_get_shape", "area", "shape_idx"), &PhysicsServer2D::area_get_shape);
  453. ClassDB::bind_method(D_METHOD("area_get_shape_transform", "area", "shape_idx"), &PhysicsServer2D::area_get_shape_transform);
  454. ClassDB::bind_method(D_METHOD("area_remove_shape", "area", "shape_idx"), &PhysicsServer2D::area_remove_shape);
  455. ClassDB::bind_method(D_METHOD("area_clear_shapes", "area"), &PhysicsServer2D::area_clear_shapes);
  456. ClassDB::bind_method(D_METHOD("area_set_collision_layer", "area", "layer"), &PhysicsServer2D::area_set_collision_layer);
  457. ClassDB::bind_method(D_METHOD("area_set_collision_mask", "area", "mask"), &PhysicsServer2D::area_set_collision_mask);
  458. ClassDB::bind_method(D_METHOD("area_set_param", "area", "param", "value"), &PhysicsServer2D::area_set_param);
  459. ClassDB::bind_method(D_METHOD("area_set_transform", "area", "transform"), &PhysicsServer2D::area_set_transform);
  460. ClassDB::bind_method(D_METHOD("area_get_param", "area", "param"), &PhysicsServer2D::area_get_param);
  461. ClassDB::bind_method(D_METHOD("area_get_transform", "area"), &PhysicsServer2D::area_get_transform);
  462. ClassDB::bind_method(D_METHOD("area_attach_object_instance_id", "area", "id"), &PhysicsServer2D::area_attach_object_instance_id);
  463. ClassDB::bind_method(D_METHOD("area_get_object_instance_id", "area"), &PhysicsServer2D::area_get_object_instance_id);
  464. ClassDB::bind_method(D_METHOD("area_attach_canvas_instance_id", "area", "id"), &PhysicsServer2D::area_attach_canvas_instance_id);
  465. ClassDB::bind_method(D_METHOD("area_get_canvas_instance_id", "area"), &PhysicsServer2D::area_get_canvas_instance_id);
  466. ClassDB::bind_method(D_METHOD("area_set_monitor_callback", "area", "receiver", "method"), &PhysicsServer2D::area_set_monitor_callback);
  467. ClassDB::bind_method(D_METHOD("area_set_area_monitor_callback", "area", "receiver", "method"), &PhysicsServer2D::area_set_area_monitor_callback);
  468. ClassDB::bind_method(D_METHOD("area_set_monitorable", "area", "monitorable"), &PhysicsServer2D::area_set_monitorable);
  469. ClassDB::bind_method(D_METHOD("body_create"), &PhysicsServer2D::body_create);
  470. ClassDB::bind_method(D_METHOD("body_set_space", "body", "space"), &PhysicsServer2D::body_set_space);
  471. ClassDB::bind_method(D_METHOD("body_get_space", "body"), &PhysicsServer2D::body_get_space);
  472. ClassDB::bind_method(D_METHOD("body_set_mode", "body", "mode"), &PhysicsServer2D::body_set_mode);
  473. ClassDB::bind_method(D_METHOD("body_get_mode", "body"), &PhysicsServer2D::body_get_mode);
  474. ClassDB::bind_method(D_METHOD("body_add_shape", "body", "shape", "transform", "disabled"), &PhysicsServer2D::body_add_shape, DEFVAL(Transform2D()), DEFVAL(false));
  475. ClassDB::bind_method(D_METHOD("body_set_shape", "body", "shape_idx", "shape"), &PhysicsServer2D::body_set_shape);
  476. ClassDB::bind_method(D_METHOD("body_set_shape_transform", "body", "shape_idx", "transform"), &PhysicsServer2D::body_set_shape_transform);
  477. ClassDB::bind_method(D_METHOD("body_set_shape_metadata", "body", "shape_idx", "metadata"), &PhysicsServer2D::body_set_shape_metadata);
  478. ClassDB::bind_method(D_METHOD("body_get_shape_count", "body"), &PhysicsServer2D::body_get_shape_count);
  479. ClassDB::bind_method(D_METHOD("body_get_shape", "body", "shape_idx"), &PhysicsServer2D::body_get_shape);
  480. ClassDB::bind_method(D_METHOD("body_get_shape_transform", "body", "shape_idx"), &PhysicsServer2D::body_get_shape_transform);
  481. ClassDB::bind_method(D_METHOD("body_get_shape_metadata", "body", "shape_idx"), &PhysicsServer2D::body_get_shape_metadata);
  482. ClassDB::bind_method(D_METHOD("body_remove_shape", "body", "shape_idx"), &PhysicsServer2D::body_remove_shape);
  483. ClassDB::bind_method(D_METHOD("body_clear_shapes", "body"), &PhysicsServer2D::body_clear_shapes);
  484. ClassDB::bind_method(D_METHOD("body_set_shape_disabled", "body", "shape_idx", "disabled"), &PhysicsServer2D::body_set_shape_disabled);
  485. 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);
  486. ClassDB::bind_method(D_METHOD("body_attach_object_instance_id", "body", "id"), &PhysicsServer2D::body_attach_object_instance_id);
  487. ClassDB::bind_method(D_METHOD("body_get_object_instance_id", "body"), &PhysicsServer2D::body_get_object_instance_id);
  488. ClassDB::bind_method(D_METHOD("body_attach_canvas_instance_id", "body", "id"), &PhysicsServer2D::body_attach_canvas_instance_id);
  489. ClassDB::bind_method(D_METHOD("body_get_canvas_instance_id", "body"), &PhysicsServer2D::body_get_canvas_instance_id);
  490. ClassDB::bind_method(D_METHOD("body_set_continuous_collision_detection_mode", "body", "mode"), &PhysicsServer2D::body_set_continuous_collision_detection_mode);
  491. ClassDB::bind_method(D_METHOD("body_get_continuous_collision_detection_mode", "body"), &PhysicsServer2D::body_get_continuous_collision_detection_mode);
  492. ClassDB::bind_method(D_METHOD("body_set_collision_layer", "body", "layer"), &PhysicsServer2D::body_set_collision_layer);
  493. ClassDB::bind_method(D_METHOD("body_get_collision_layer", "body"), &PhysicsServer2D::body_get_collision_layer);
  494. ClassDB::bind_method(D_METHOD("body_set_collision_mask", "body", "mask"), &PhysicsServer2D::body_set_collision_mask);
  495. ClassDB::bind_method(D_METHOD("body_get_collision_mask", "body"), &PhysicsServer2D::body_get_collision_mask);
  496. ClassDB::bind_method(D_METHOD("body_set_param", "body", "param", "value"), &PhysicsServer2D::body_set_param);
  497. ClassDB::bind_method(D_METHOD("body_get_param", "body", "param"), &PhysicsServer2D::body_get_param);
  498. ClassDB::bind_method(D_METHOD("body_set_state", "body", "state", "value"), &PhysicsServer2D::body_set_state);
  499. ClassDB::bind_method(D_METHOD("body_get_state", "body", "state"), &PhysicsServer2D::body_get_state);
  500. ClassDB::bind_method(D_METHOD("body_apply_central_impulse", "body", "impulse"), &PhysicsServer2D::body_apply_central_impulse);
  501. ClassDB::bind_method(D_METHOD("body_apply_torque_impulse", "body", "impulse"), &PhysicsServer2D::body_apply_torque_impulse);
  502. ClassDB::bind_method(D_METHOD("body_apply_impulse", "body", "impulse", "position"), &PhysicsServer2D::body_apply_impulse, Vector2());
  503. ClassDB::bind_method(D_METHOD("body_add_central_force", "body", "force"), &PhysicsServer2D::body_add_central_force);
  504. ClassDB::bind_method(D_METHOD("body_add_force", "body", "force", "position"), &PhysicsServer2D::body_add_force, Vector2());
  505. ClassDB::bind_method(D_METHOD("body_add_torque", "body", "torque"), &PhysicsServer2D::body_add_torque);
  506. ClassDB::bind_method(D_METHOD("body_set_axis_velocity", "body", "axis_velocity"), &PhysicsServer2D::body_set_axis_velocity);
  507. ClassDB::bind_method(D_METHOD("body_add_collision_exception", "body", "excepted_body"), &PhysicsServer2D::body_add_collision_exception);
  508. ClassDB::bind_method(D_METHOD("body_remove_collision_exception", "body", "excepted_body"), &PhysicsServer2D::body_remove_collision_exception);
  509. ClassDB::bind_method(D_METHOD("body_set_max_contacts_reported", "body", "amount"), &PhysicsServer2D::body_set_max_contacts_reported);
  510. ClassDB::bind_method(D_METHOD("body_get_max_contacts_reported", "body"), &PhysicsServer2D::body_get_max_contacts_reported);
  511. ClassDB::bind_method(D_METHOD("body_set_omit_force_integration", "body", "enable"), &PhysicsServer2D::body_set_omit_force_integration);
  512. ClassDB::bind_method(D_METHOD("body_is_omitting_force_integration", "body"), &PhysicsServer2D::body_is_omitting_force_integration);
  513. ClassDB::bind_method(D_METHOD("body_set_force_integration_callback", "body", "receiver", "method", "userdata"), &PhysicsServer2D::body_set_force_integration_callback, DEFVAL(Variant()));
  514. ClassDB::bind_method(D_METHOD("body_test_motion", "body", "from", "motion", "infinite_inertia", "margin", "result"), &PhysicsServer2D::_body_test_motion, DEFVAL(0.08), DEFVAL(Variant()));
  515. ClassDB::bind_method(D_METHOD("body_get_direct_state", "body"), &PhysicsServer2D::body_get_direct_state);
  516. /* JOINT API */
  517. ClassDB::bind_method(D_METHOD("joint_create"), &PhysicsServer2D::joint_create);
  518. ClassDB::bind_method(D_METHOD("joint_clear", "joint"), &PhysicsServer2D::joint_clear);
  519. ClassDB::bind_method(D_METHOD("joint_set_param", "joint", "param", "value"), &PhysicsServer2D::joint_set_param);
  520. ClassDB::bind_method(D_METHOD("joint_get_param", "joint", "param"), &PhysicsServer2D::joint_get_param);
  521. ClassDB::bind_method(D_METHOD("joint_make_pin", "joint", "anchor", "body_a", "body_b"), &PhysicsServer2D::joint_make_pin, DEFVAL(RID()));
  522. 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()));
  523. 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()));
  524. ClassDB::bind_method(D_METHOD("damped_spring_joint_set_param", "joint", "param", "value"), &PhysicsServer2D::damped_spring_joint_set_param);
  525. ClassDB::bind_method(D_METHOD("damped_spring_joint_get_param", "joint", "param"), &PhysicsServer2D::damped_spring_joint_get_param);
  526. ClassDB::bind_method(D_METHOD("joint_get_type", "joint"), &PhysicsServer2D::joint_get_type);
  527. ClassDB::bind_method(D_METHOD("free_rid", "rid"), &PhysicsServer2D::free);
  528. ClassDB::bind_method(D_METHOD("set_active", "active"), &PhysicsServer2D::set_active);
  529. ClassDB::bind_method(D_METHOD("get_process_info", "process_info"), &PhysicsServer2D::get_process_info);
  530. BIND_ENUM_CONSTANT(SPACE_PARAM_CONTACT_RECYCLE_RADIUS);
  531. BIND_ENUM_CONSTANT(SPACE_PARAM_CONTACT_MAX_SEPARATION);
  532. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION);
  533. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD);
  534. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD);
  535. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_TIME_TO_SLEEP);
  536. BIND_ENUM_CONSTANT(SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS);
  537. BIND_ENUM_CONSTANT(SPACE_PARAM_TEST_MOTION_MIN_CONTACT_DEPTH);
  538. BIND_ENUM_CONSTANT(SHAPE_LINE);
  539. BIND_ENUM_CONSTANT(SHAPE_RAY);
  540. BIND_ENUM_CONSTANT(SHAPE_SEGMENT);
  541. BIND_ENUM_CONSTANT(SHAPE_CIRCLE);
  542. BIND_ENUM_CONSTANT(SHAPE_RECTANGLE);
  543. BIND_ENUM_CONSTANT(SHAPE_CAPSULE);
  544. BIND_ENUM_CONSTANT(SHAPE_CONVEX_POLYGON);
  545. BIND_ENUM_CONSTANT(SHAPE_CONCAVE_POLYGON);
  546. BIND_ENUM_CONSTANT(SHAPE_CUSTOM);
  547. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY);
  548. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_VECTOR);
  549. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_IS_POINT);
  550. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_DISTANCE_SCALE);
  551. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_POINT_ATTENUATION);
  552. BIND_ENUM_CONSTANT(AREA_PARAM_LINEAR_DAMP);
  553. BIND_ENUM_CONSTANT(AREA_PARAM_ANGULAR_DAMP);
  554. BIND_ENUM_CONSTANT(AREA_PARAM_PRIORITY);
  555. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_DISABLED);
  556. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_COMBINE);
  557. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_COMBINE_REPLACE);
  558. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_REPLACE);
  559. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_REPLACE_COMBINE);
  560. BIND_ENUM_CONSTANT(BODY_MODE_STATIC);
  561. BIND_ENUM_CONSTANT(BODY_MODE_KINEMATIC);
  562. BIND_ENUM_CONSTANT(BODY_MODE_RIGID);
  563. BIND_ENUM_CONSTANT(BODY_MODE_CHARACTER);
  564. BIND_ENUM_CONSTANT(BODY_PARAM_BOUNCE);
  565. BIND_ENUM_CONSTANT(BODY_PARAM_FRICTION);
  566. BIND_ENUM_CONSTANT(BODY_PARAM_MASS);
  567. BIND_ENUM_CONSTANT(BODY_PARAM_INERTIA);
  568. BIND_ENUM_CONSTANT(BODY_PARAM_GRAVITY_SCALE);
  569. BIND_ENUM_CONSTANT(BODY_PARAM_LINEAR_DAMP);
  570. BIND_ENUM_CONSTANT(BODY_PARAM_ANGULAR_DAMP);
  571. BIND_ENUM_CONSTANT(BODY_PARAM_MAX);
  572. BIND_ENUM_CONSTANT(BODY_STATE_TRANSFORM);
  573. BIND_ENUM_CONSTANT(BODY_STATE_LINEAR_VELOCITY);
  574. BIND_ENUM_CONSTANT(BODY_STATE_ANGULAR_VELOCITY);
  575. BIND_ENUM_CONSTANT(BODY_STATE_SLEEPING);
  576. BIND_ENUM_CONSTANT(BODY_STATE_CAN_SLEEP);
  577. BIND_ENUM_CONSTANT(JOINT_TYPE_PIN);
  578. BIND_ENUM_CONSTANT(JOINT_TYPE_GROOVE);
  579. BIND_ENUM_CONSTANT(JOINT_TYPE_DAMPED_SPRING);
  580. BIND_ENUM_CONSTANT(JOINT_TYPE_MAX);
  581. BIND_ENUM_CONSTANT(JOINT_PARAM_BIAS);
  582. BIND_ENUM_CONSTANT(JOINT_PARAM_MAX_BIAS);
  583. BIND_ENUM_CONSTANT(JOINT_PARAM_MAX_FORCE);
  584. BIND_ENUM_CONSTANT(DAMPED_SPRING_REST_LENGTH);
  585. BIND_ENUM_CONSTANT(DAMPED_SPRING_STIFFNESS);
  586. BIND_ENUM_CONSTANT(DAMPED_SPRING_DAMPING);
  587. BIND_ENUM_CONSTANT(CCD_MODE_DISABLED);
  588. BIND_ENUM_CONSTANT(CCD_MODE_CAST_RAY);
  589. BIND_ENUM_CONSTANT(CCD_MODE_CAST_SHAPE);
  590. BIND_ENUM_CONSTANT(AREA_BODY_ADDED);
  591. BIND_ENUM_CONSTANT(AREA_BODY_REMOVED);
  592. BIND_ENUM_CONSTANT(INFO_ACTIVE_OBJECTS);
  593. BIND_ENUM_CONSTANT(INFO_COLLISION_PAIRS);
  594. BIND_ENUM_CONSTANT(INFO_ISLAND_COUNT);
  595. }
  596. PhysicsServer2D::PhysicsServer2D() {
  597. singleton = this;
  598. }
  599. PhysicsServer2D::~PhysicsServer2D() {
  600. singleton = nullptr;
  601. }
  602. Vector<PhysicsServer2DManager::ClassInfo> PhysicsServer2DManager::physics_2d_servers;
  603. int PhysicsServer2DManager::default_server_id = -1;
  604. int PhysicsServer2DManager::default_server_priority = -1;
  605. const String PhysicsServer2DManager::setting_property_name("physics/2d/physics_engine");
  606. void PhysicsServer2DManager::on_servers_changed() {
  607. String physics_servers("DEFAULT");
  608. for (int i = get_servers_count() - 1; 0 <= i; --i) {
  609. physics_servers += "," + get_server_name(i);
  610. }
  611. ProjectSettings::get_singleton()->set_custom_property_info(setting_property_name, PropertyInfo(Variant::STRING, setting_property_name, PROPERTY_HINT_ENUM, physics_servers));
  612. }
  613. void PhysicsServer2DManager::register_server(const String &p_name, CreatePhysicsServer2DCallback p_creat_callback) {
  614. ERR_FAIL_COND(!p_creat_callback);
  615. ERR_FAIL_COND(find_server_id(p_name) != -1);
  616. physics_2d_servers.push_back(ClassInfo(p_name, p_creat_callback));
  617. on_servers_changed();
  618. }
  619. void PhysicsServer2DManager::set_default_server(const String &p_name, int p_priority) {
  620. const int id = find_server_id(p_name);
  621. ERR_FAIL_COND(id == -1); // Not found
  622. if (default_server_priority < p_priority) {
  623. default_server_id = id;
  624. default_server_priority = p_priority;
  625. }
  626. }
  627. int PhysicsServer2DManager::find_server_id(const String &p_name) {
  628. for (int i = physics_2d_servers.size() - 1; 0 <= i; --i) {
  629. if (p_name == physics_2d_servers[i].name) {
  630. return i;
  631. }
  632. }
  633. return -1;
  634. }
  635. int PhysicsServer2DManager::get_servers_count() {
  636. return physics_2d_servers.size();
  637. }
  638. String PhysicsServer2DManager::get_server_name(int p_id) {
  639. ERR_FAIL_INDEX_V(p_id, get_servers_count(), "");
  640. return physics_2d_servers[p_id].name;
  641. }
  642. PhysicsServer2D *PhysicsServer2DManager::new_default_server() {
  643. ERR_FAIL_COND_V(default_server_id == -1, nullptr);
  644. return physics_2d_servers[default_server_id].create_callback();
  645. }
  646. PhysicsServer2D *PhysicsServer2DManager::new_server(const String &p_name) {
  647. int id = find_server_id(p_name);
  648. if (id == -1) {
  649. return nullptr;
  650. } else {
  651. return physics_2d_servers[id].create_callback();
  652. }
  653. }