physics_server_2d.cpp 52 KB

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