physics_2d_server.cpp 41 KB

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