physics_server_3d.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /*************************************************************************/
  2. /* physics_server_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "physics_server_3d.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/string/print_string.h"
  33. PhysicsServer3D *PhysicsServer3D::singleton = nullptr;
  34. void PhysicsDirectBodyState3D::integrate_forces() {
  35. real_t step = get_step();
  36. Vector3 lv = get_linear_velocity();
  37. lv += get_total_gravity() * step;
  38. Vector3 av = get_angular_velocity();
  39. real_t linear_damp = 1.0 - step * get_total_linear_damp();
  40. if (linear_damp < 0) { // reached zero in the given time
  41. linear_damp = 0;
  42. }
  43. real_t angular_damp = 1.0 - step * get_total_angular_damp();
  44. if (angular_damp < 0) { // reached zero in the given time
  45. angular_damp = 0;
  46. }
  47. lv *= linear_damp;
  48. av *= angular_damp;
  49. set_linear_velocity(lv);
  50. set_angular_velocity(av);
  51. }
  52. Object *PhysicsDirectBodyState3D::get_contact_collider_object(int p_contact_idx) const {
  53. ObjectID objid = get_contact_collider_id(p_contact_idx);
  54. Object *obj = ObjectDB::get_instance(objid);
  55. return obj;
  56. }
  57. PhysicsServer3D *PhysicsServer3D::get_singleton() {
  58. return singleton;
  59. }
  60. void PhysicsDirectBodyState3D::_bind_methods() {
  61. ClassDB::bind_method(D_METHOD("get_total_gravity"), &PhysicsDirectBodyState3D::get_total_gravity);
  62. ClassDB::bind_method(D_METHOD("get_total_linear_damp"), &PhysicsDirectBodyState3D::get_total_linear_damp);
  63. ClassDB::bind_method(D_METHOD("get_total_angular_damp"), &PhysicsDirectBodyState3D::get_total_angular_damp);
  64. ClassDB::bind_method(D_METHOD("get_center_of_mass"), &PhysicsDirectBodyState3D::get_center_of_mass);
  65. ClassDB::bind_method(D_METHOD("get_principal_inertia_axes"), &PhysicsDirectBodyState3D::get_principal_inertia_axes);
  66. ClassDB::bind_method(D_METHOD("get_inverse_mass"), &PhysicsDirectBodyState3D::get_inverse_mass);
  67. ClassDB::bind_method(D_METHOD("get_inverse_inertia"), &PhysicsDirectBodyState3D::get_inverse_inertia);
  68. ClassDB::bind_method(D_METHOD("set_linear_velocity", "velocity"), &PhysicsDirectBodyState3D::set_linear_velocity);
  69. ClassDB::bind_method(D_METHOD("get_linear_velocity"), &PhysicsDirectBodyState3D::get_linear_velocity);
  70. ClassDB::bind_method(D_METHOD("set_angular_velocity", "velocity"), &PhysicsDirectBodyState3D::set_angular_velocity);
  71. ClassDB::bind_method(D_METHOD("get_angular_velocity"), &PhysicsDirectBodyState3D::get_angular_velocity);
  72. ClassDB::bind_method(D_METHOD("set_transform", "transform"), &PhysicsDirectBodyState3D::set_transform);
  73. ClassDB::bind_method(D_METHOD("get_transform"), &PhysicsDirectBodyState3D::get_transform);
  74. ClassDB::bind_method(D_METHOD("get_velocity_at_local_position", "local_position"), &PhysicsDirectBodyState3D::get_velocity_at_local_position);
  75. ClassDB::bind_method(D_METHOD("add_central_force", "force"), &PhysicsDirectBodyState3D::add_central_force, Vector3());
  76. ClassDB::bind_method(D_METHOD("add_force", "force", "position"), &PhysicsDirectBodyState3D::add_force, Vector3());
  77. ClassDB::bind_method(D_METHOD("add_torque", "torque"), &PhysicsDirectBodyState3D::add_torque);
  78. ClassDB::bind_method(D_METHOD("apply_central_impulse", "impulse"), &PhysicsDirectBodyState3D::apply_central_impulse, Vector3());
  79. ClassDB::bind_method(D_METHOD("apply_impulse", "impulse", "position"), &PhysicsDirectBodyState3D::apply_impulse, Vector3());
  80. ClassDB::bind_method(D_METHOD("apply_torque_impulse", "impulse"), &PhysicsDirectBodyState3D::apply_torque_impulse);
  81. ClassDB::bind_method(D_METHOD("set_sleep_state", "enabled"), &PhysicsDirectBodyState3D::set_sleep_state);
  82. ClassDB::bind_method(D_METHOD("is_sleeping"), &PhysicsDirectBodyState3D::is_sleeping);
  83. ClassDB::bind_method(D_METHOD("get_contact_count"), &PhysicsDirectBodyState3D::get_contact_count);
  84. ClassDB::bind_method(D_METHOD("get_contact_local_position", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_local_position);
  85. ClassDB::bind_method(D_METHOD("get_contact_local_normal", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_local_normal);
  86. ClassDB::bind_method(D_METHOD("get_contact_impulse", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_impulse);
  87. ClassDB::bind_method(D_METHOD("get_contact_local_shape", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_local_shape);
  88. ClassDB::bind_method(D_METHOD("get_contact_collider", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_collider);
  89. ClassDB::bind_method(D_METHOD("get_contact_collider_position", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_collider_position);
  90. ClassDB::bind_method(D_METHOD("get_contact_collider_id", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_collider_id);
  91. ClassDB::bind_method(D_METHOD("get_contact_collider_object", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_collider_object);
  92. ClassDB::bind_method(D_METHOD("get_contact_collider_shape", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_collider_shape);
  93. ClassDB::bind_method(D_METHOD("get_contact_collider_velocity_at_position", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_collider_velocity_at_position);
  94. ClassDB::bind_method(D_METHOD("get_step"), &PhysicsDirectBodyState3D::get_step);
  95. ClassDB::bind_method(D_METHOD("integrate_forces"), &PhysicsDirectBodyState3D::integrate_forces);
  96. ClassDB::bind_method(D_METHOD("get_space_state"), &PhysicsDirectBodyState3D::get_space_state);
  97. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "step"), "", "get_step");
  98. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inverse_mass"), "", "get_inverse_mass");
  99. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "total_angular_damp"), "", "get_total_angular_damp");
  100. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "total_linear_damp"), "", "get_total_linear_damp");
  101. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "inverse_inertia"), "", "get_inverse_inertia");
  102. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "total_gravity"), "", "get_total_gravity");
  103. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "center_of_mass"), "", "get_center_of_mass");
  104. ADD_PROPERTY(PropertyInfo(Variant::BASIS, "principal_inertia_axes"), "", "get_principal_inertia_axes");
  105. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "angular_velocity"), "set_angular_velocity", "get_angular_velocity");
  106. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "linear_velocity"), "set_linear_velocity", "get_linear_velocity");
  107. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sleeping"), "set_sleep_state", "is_sleeping");
  108. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "transform"), "set_transform", "get_transform");
  109. }
  110. PhysicsDirectBodyState3D::PhysicsDirectBodyState3D() {}
  111. ///////////////////////////////////////////////////////
  112. void PhysicsRayQueryParameters3D::set_exclude(const Vector<RID> &p_exclude) {
  113. parameters.exclude.clear();
  114. for (int i = 0; i < p_exclude.size(); i++) {
  115. parameters.exclude.insert(p_exclude[i]);
  116. }
  117. }
  118. Vector<RID> PhysicsRayQueryParameters3D::get_exclude() const {
  119. Vector<RID> ret;
  120. ret.resize(parameters.exclude.size());
  121. int idx = 0;
  122. for (Set<RID>::Element *E = parameters.exclude.front(); E; E = E->next()) {
  123. ret.write[idx++] = E->get();
  124. }
  125. return ret;
  126. }
  127. void PhysicsRayQueryParameters3D::_bind_methods() {
  128. ClassDB::bind_method(D_METHOD("set_from", "from"), &PhysicsRayQueryParameters3D::set_from);
  129. ClassDB::bind_method(D_METHOD("get_from"), &PhysicsRayQueryParameters3D::get_from);
  130. ClassDB::bind_method(D_METHOD("set_to", "to"), &PhysicsRayQueryParameters3D::set_to);
  131. ClassDB::bind_method(D_METHOD("get_to"), &PhysicsRayQueryParameters3D::get_to);
  132. ClassDB::bind_method(D_METHOD("set_collision_mask", "collision_mask"), &PhysicsRayQueryParameters3D::set_collision_mask);
  133. ClassDB::bind_method(D_METHOD("get_collision_mask"), &PhysicsRayQueryParameters3D::get_collision_mask);
  134. ClassDB::bind_method(D_METHOD("set_exclude", "exclude"), &PhysicsRayQueryParameters3D::set_exclude);
  135. ClassDB::bind_method(D_METHOD("get_exclude"), &PhysicsRayQueryParameters3D::get_exclude);
  136. ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &PhysicsRayQueryParameters3D::set_collide_with_bodies);
  137. ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &PhysicsRayQueryParameters3D::is_collide_with_bodies_enabled);
  138. ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &PhysicsRayQueryParameters3D::set_collide_with_areas);
  139. ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &PhysicsRayQueryParameters3D::is_collide_with_areas_enabled);
  140. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "from"), "set_from", "get_from");
  141. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "to"), "set_to", "get_to");
  142. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  143. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude", PROPERTY_HINT_ARRAY_TYPE, "RID"), "set_exclude", "get_exclude");
  144. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies"), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
  145. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas"), "set_collide_with_areas", "is_collide_with_areas_enabled");
  146. }
  147. ///////////////////////////////////////////////////////
  148. void PhysicsPointQueryParameters3D::set_exclude(const Vector<RID> &p_exclude) {
  149. parameters.exclude.clear();
  150. for (int i = 0; i < p_exclude.size(); i++) {
  151. parameters.exclude.insert(p_exclude[i]);
  152. }
  153. }
  154. Vector<RID> PhysicsPointQueryParameters3D::get_exclude() const {
  155. Vector<RID> ret;
  156. ret.resize(parameters.exclude.size());
  157. int idx = 0;
  158. for (Set<RID>::Element *E = parameters.exclude.front(); E; E = E->next()) {
  159. ret.write[idx++] = E->get();
  160. }
  161. return ret;
  162. }
  163. void PhysicsPointQueryParameters3D::_bind_methods() {
  164. ClassDB::bind_method(D_METHOD("set_position", "position"), &PhysicsPointQueryParameters3D::set_position);
  165. ClassDB::bind_method(D_METHOD("get_position"), &PhysicsPointQueryParameters3D::get_position);
  166. ClassDB::bind_method(D_METHOD("set_collision_mask", "collision_mask"), &PhysicsPointQueryParameters3D::set_collision_mask);
  167. ClassDB::bind_method(D_METHOD("get_collision_mask"), &PhysicsPointQueryParameters3D::get_collision_mask);
  168. ClassDB::bind_method(D_METHOD("set_exclude", "exclude"), &PhysicsPointQueryParameters3D::set_exclude);
  169. ClassDB::bind_method(D_METHOD("get_exclude"), &PhysicsPointQueryParameters3D::get_exclude);
  170. ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &PhysicsPointQueryParameters3D::set_collide_with_bodies);
  171. ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &PhysicsPointQueryParameters3D::is_collide_with_bodies_enabled);
  172. ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &PhysicsPointQueryParameters3D::set_collide_with_areas);
  173. ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &PhysicsPointQueryParameters3D::is_collide_with_areas_enabled);
  174. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "position"), "set_position", "get_position");
  175. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  176. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude", PROPERTY_HINT_ARRAY_TYPE, "RID"), "set_exclude", "get_exclude");
  177. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies"), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
  178. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas"), "set_collide_with_areas", "is_collide_with_areas_enabled");
  179. }
  180. ///////////////////////////////////////////////////////
  181. void PhysicsShapeQueryParameters3D::set_shape(const RES &p_shape_ref) {
  182. ERR_FAIL_COND(p_shape_ref.is_null());
  183. shape_ref = p_shape_ref;
  184. parameters.shape_rid = p_shape_ref->get_rid();
  185. }
  186. void PhysicsShapeQueryParameters3D::set_shape_rid(const RID &p_shape) {
  187. if (parameters.shape_rid != p_shape) {
  188. shape_ref = RES();
  189. parameters.shape_rid = p_shape;
  190. }
  191. }
  192. void PhysicsShapeQueryParameters3D::set_exclude(const Vector<RID> &p_exclude) {
  193. parameters.exclude.clear();
  194. for (int i = 0; i < p_exclude.size(); i++) {
  195. parameters.exclude.insert(p_exclude[i]);
  196. }
  197. }
  198. Vector<RID> PhysicsShapeQueryParameters3D::get_exclude() const {
  199. Vector<RID> ret;
  200. ret.resize(parameters.exclude.size());
  201. int idx = 0;
  202. for (Set<RID>::Element *E = parameters.exclude.front(); E; E = E->next()) {
  203. ret.write[idx++] = E->get();
  204. }
  205. return ret;
  206. }
  207. void PhysicsShapeQueryParameters3D::_bind_methods() {
  208. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &PhysicsShapeQueryParameters3D::set_shape);
  209. ClassDB::bind_method(D_METHOD("get_shape"), &PhysicsShapeQueryParameters3D::get_shape);
  210. ClassDB::bind_method(D_METHOD("set_shape_rid", "shape"), &PhysicsShapeQueryParameters3D::set_shape_rid);
  211. ClassDB::bind_method(D_METHOD("get_shape_rid"), &PhysicsShapeQueryParameters3D::get_shape_rid);
  212. ClassDB::bind_method(D_METHOD("set_transform", "transform"), &PhysicsShapeQueryParameters3D::set_transform);
  213. ClassDB::bind_method(D_METHOD("get_transform"), &PhysicsShapeQueryParameters3D::get_transform);
  214. ClassDB::bind_method(D_METHOD("set_motion", "motion"), &PhysicsShapeQueryParameters3D::set_motion);
  215. ClassDB::bind_method(D_METHOD("get_motion"), &PhysicsShapeQueryParameters3D::get_motion);
  216. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &PhysicsShapeQueryParameters3D::set_margin);
  217. ClassDB::bind_method(D_METHOD("get_margin"), &PhysicsShapeQueryParameters3D::get_margin);
  218. ClassDB::bind_method(D_METHOD("set_collision_mask", "collision_mask"), &PhysicsShapeQueryParameters3D::set_collision_mask);
  219. ClassDB::bind_method(D_METHOD("get_collision_mask"), &PhysicsShapeQueryParameters3D::get_collision_mask);
  220. ClassDB::bind_method(D_METHOD("set_exclude", "exclude"), &PhysicsShapeQueryParameters3D::set_exclude);
  221. ClassDB::bind_method(D_METHOD("get_exclude"), &PhysicsShapeQueryParameters3D::get_exclude);
  222. ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &PhysicsShapeQueryParameters3D::set_collide_with_bodies);
  223. ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &PhysicsShapeQueryParameters3D::is_collide_with_bodies_enabled);
  224. ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &PhysicsShapeQueryParameters3D::set_collide_with_areas);
  225. ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &PhysicsShapeQueryParameters3D::is_collide_with_areas_enabled);
  226. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  227. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude", PROPERTY_HINT_ARRAY_TYPE, "RID"), "set_exclude", "get_exclude");
  228. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin", PROPERTY_HINT_RANGE, "0,100,0.01"), "set_margin", "get_margin");
  229. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion"), "set_motion", "get_motion");
  230. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape3D"), "set_shape", "get_shape");
  231. ADD_PROPERTY(PropertyInfo(Variant::RID, "shape_rid"), "set_shape_rid", "get_shape_rid");
  232. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "transform"), "set_transform", "get_transform");
  233. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies"), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
  234. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas"), "set_collide_with_areas", "is_collide_with_areas_enabled");
  235. }
  236. /////////////////////////////////////
  237. Dictionary PhysicsDirectSpaceState3D::_intersect_ray(const Ref<PhysicsRayQueryParameters3D> &p_ray_query) {
  238. ERR_FAIL_COND_V(!p_ray_query.is_valid(), Dictionary());
  239. RayResult result;
  240. bool res = intersect_ray(p_ray_query->get_parameters(), result);
  241. if (!res) {
  242. return Dictionary();
  243. }
  244. Dictionary d;
  245. d["position"] = result.position;
  246. d["normal"] = result.normal;
  247. d["collider_id"] = result.collider_id;
  248. d["collider"] = result.collider;
  249. d["shape"] = result.shape;
  250. d["rid"] = result.rid;
  251. return d;
  252. }
  253. Array PhysicsDirectSpaceState3D::_intersect_point(const Ref<PhysicsPointQueryParameters3D> &p_point_query, int p_max_results) {
  254. Vector<ShapeResult> ret;
  255. ret.resize(p_max_results);
  256. int rc = intersect_point(p_point_query->get_parameters(), ret.ptrw(), ret.size());
  257. if (rc == 0) {
  258. return Array();
  259. }
  260. Array r;
  261. r.resize(rc);
  262. for (int i = 0; i < rc; i++) {
  263. Dictionary d;
  264. d["rid"] = ret[i].rid;
  265. d["collider_id"] = ret[i].collider_id;
  266. d["collider"] = ret[i].collider;
  267. d["shape"] = ret[i].shape;
  268. r[i] = d;
  269. }
  270. return r;
  271. }
  272. Array PhysicsDirectSpaceState3D::_intersect_shape(const Ref<PhysicsShapeQueryParameters3D> &p_shape_query, int p_max_results) {
  273. ERR_FAIL_COND_V(!p_shape_query.is_valid(), Array());
  274. Vector<ShapeResult> sr;
  275. sr.resize(p_max_results);
  276. int rc = intersect_shape(p_shape_query->get_parameters(), sr.ptrw(), sr.size());
  277. Array ret;
  278. ret.resize(rc);
  279. for (int i = 0; i < rc; i++) {
  280. Dictionary d;
  281. d["rid"] = sr[i].rid;
  282. d["collider_id"] = sr[i].collider_id;
  283. d["collider"] = sr[i].collider;
  284. d["shape"] = sr[i].shape;
  285. ret[i] = d;
  286. }
  287. return ret;
  288. }
  289. Array PhysicsDirectSpaceState3D::_cast_motion(const Ref<PhysicsShapeQueryParameters3D> &p_shape_query) {
  290. ERR_FAIL_COND_V(!p_shape_query.is_valid(), Array());
  291. real_t closest_safe = 1.0f, closest_unsafe = 1.0f;
  292. bool res = cast_motion(p_shape_query->get_parameters(), closest_safe, closest_unsafe);
  293. if (!res) {
  294. return Array();
  295. }
  296. Array ret;
  297. ret.resize(2);
  298. ret[0] = closest_safe;
  299. ret[1] = closest_unsafe;
  300. return ret;
  301. }
  302. Array PhysicsDirectSpaceState3D::_collide_shape(const Ref<PhysicsShapeQueryParameters3D> &p_shape_query, int p_max_results) {
  303. ERR_FAIL_COND_V(!p_shape_query.is_valid(), Array());
  304. Vector<Vector3> ret;
  305. ret.resize(p_max_results * 2);
  306. int rc = 0;
  307. bool res = collide_shape(p_shape_query->get_parameters(), ret.ptrw(), p_max_results, rc);
  308. if (!res) {
  309. return Array();
  310. }
  311. Array r;
  312. r.resize(rc * 2);
  313. for (int i = 0; i < rc * 2; i++) {
  314. r[i] = ret[i];
  315. }
  316. return r;
  317. }
  318. Dictionary PhysicsDirectSpaceState3D::_get_rest_info(const Ref<PhysicsShapeQueryParameters3D> &p_shape_query) {
  319. ERR_FAIL_COND_V(!p_shape_query.is_valid(), Dictionary());
  320. ShapeRestInfo sri;
  321. bool res = rest_info(p_shape_query->get_parameters(), &sri);
  322. Dictionary r;
  323. if (!res) {
  324. return r;
  325. }
  326. r["point"] = sri.point;
  327. r["normal"] = sri.normal;
  328. r["rid"] = sri.rid;
  329. r["collider_id"] = sri.collider_id;
  330. r["shape"] = sri.shape;
  331. r["linear_velocity"] = sri.linear_velocity;
  332. return r;
  333. }
  334. PhysicsDirectSpaceState3D::PhysicsDirectSpaceState3D() {
  335. }
  336. void PhysicsDirectSpaceState3D::_bind_methods() {
  337. ClassDB::bind_method(D_METHOD("intersect_point", "parameters", "max_results"), &PhysicsDirectSpaceState3D::_intersect_point, DEFVAL(32));
  338. ClassDB::bind_method(D_METHOD("intersect_ray", "parameters"), &PhysicsDirectSpaceState3D::_intersect_ray);
  339. ClassDB::bind_method(D_METHOD("intersect_shape", "parameters", "max_results"), &PhysicsDirectSpaceState3D::_intersect_shape, DEFVAL(32));
  340. ClassDB::bind_method(D_METHOD("cast_motion", "parameters"), &PhysicsDirectSpaceState3D::_cast_motion);
  341. ClassDB::bind_method(D_METHOD("collide_shape", "parameters", "max_results"), &PhysicsDirectSpaceState3D::_collide_shape, DEFVAL(32));
  342. ClassDB::bind_method(D_METHOD("get_rest_info", "parameters"), &PhysicsDirectSpaceState3D::_get_rest_info);
  343. }
  344. ///////////////////////////////
  345. Vector<RID> PhysicsTestMotionParameters3D::get_exclude_bodies() const {
  346. Vector<RID> exclude;
  347. exclude.resize(parameters.exclude_bodies.size());
  348. int body_index = 0;
  349. for (RID body : parameters.exclude_bodies) {
  350. exclude.write[body_index++] = body;
  351. }
  352. return exclude;
  353. }
  354. void PhysicsTestMotionParameters3D::set_exclude_bodies(const Vector<RID> &p_exclude) {
  355. for (RID body : p_exclude) {
  356. parameters.exclude_bodies.insert(body);
  357. }
  358. }
  359. Array PhysicsTestMotionParameters3D::get_exclude_objects() const {
  360. Array exclude;
  361. exclude.resize(parameters.exclude_objects.size());
  362. int object_index = 0;
  363. for (ObjectID object_id : parameters.exclude_objects) {
  364. exclude[object_index++] = object_id;
  365. }
  366. return exclude;
  367. }
  368. void PhysicsTestMotionParameters3D::set_exclude_objects(const Array &p_exclude) {
  369. for (int i = 0; i < p_exclude.size(); ++i) {
  370. ObjectID object_id = p_exclude[i];
  371. ERR_CONTINUE(object_id.is_null());
  372. parameters.exclude_objects.insert(object_id);
  373. }
  374. }
  375. void PhysicsTestMotionParameters3D::_bind_methods() {
  376. ClassDB::bind_method(D_METHOD("get_from"), &PhysicsTestMotionParameters3D::get_from);
  377. ClassDB::bind_method(D_METHOD("set_from", "from"), &PhysicsTestMotionParameters3D::set_from);
  378. ClassDB::bind_method(D_METHOD("get_motion"), &PhysicsTestMotionParameters3D::get_motion);
  379. ClassDB::bind_method(D_METHOD("set_motion", "motion"), &PhysicsTestMotionParameters3D::set_motion);
  380. ClassDB::bind_method(D_METHOD("get_margin"), &PhysicsTestMotionParameters3D::get_margin);
  381. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &PhysicsTestMotionParameters3D::set_margin);
  382. ClassDB::bind_method(D_METHOD("get_max_collisions"), &PhysicsTestMotionParameters3D::get_max_collisions);
  383. ClassDB::bind_method(D_METHOD("set_max_collisions", "max_collisions"), &PhysicsTestMotionParameters3D::set_max_collisions);
  384. ClassDB::bind_method(D_METHOD("is_collide_separation_ray_enabled"), &PhysicsTestMotionParameters3D::is_collide_separation_ray_enabled);
  385. ClassDB::bind_method(D_METHOD("set_collide_separation_ray_enabled", "enabled"), &PhysicsTestMotionParameters3D::set_collide_separation_ray_enabled);
  386. ClassDB::bind_method(D_METHOD("get_exclude_bodies"), &PhysicsTestMotionParameters3D::get_exclude_bodies);
  387. ClassDB::bind_method(D_METHOD("set_exclude_bodies", "exclude_list"), &PhysicsTestMotionParameters3D::set_exclude_bodies);
  388. ClassDB::bind_method(D_METHOD("get_exclude_objects"), &PhysicsTestMotionParameters3D::get_exclude_objects);
  389. ClassDB::bind_method(D_METHOD("set_exclude_objects", "exclude_list"), &PhysicsTestMotionParameters3D::set_exclude_objects);
  390. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "from"), "set_from", "get_from");
  391. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "motion"), "set_motion", "get_motion");
  392. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin"), "set_margin", "get_margin");
  393. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_collisions"), "set_max_collisions", "get_max_collisions");
  394. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_separation_ray"), "set_collide_separation_ray_enabled", "is_collide_separation_ray_enabled");
  395. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude_bodies", PROPERTY_HINT_ARRAY_TYPE, "RID"), "set_exclude_bodies", "get_exclude_bodies");
  396. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude_objects"), "set_exclude_objects", "get_exclude_objects");
  397. }
  398. ///////////////////////////////
  399. Vector3 PhysicsTestMotionResult3D::get_travel() const {
  400. return result.travel;
  401. }
  402. Vector3 PhysicsTestMotionResult3D::get_remainder() const {
  403. return result.remainder;
  404. }
  405. real_t PhysicsTestMotionResult3D::get_collision_safe_fraction() const {
  406. return result.collision_safe_fraction;
  407. }
  408. real_t PhysicsTestMotionResult3D::get_collision_unsafe_fraction() const {
  409. return result.collision_unsafe_fraction;
  410. }
  411. int PhysicsTestMotionResult3D::get_collision_count() const {
  412. return result.collision_count;
  413. }
  414. Vector3 PhysicsTestMotionResult3D::get_collision_point(int p_collision_index) const {
  415. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, Vector3());
  416. return result.collisions[p_collision_index].position;
  417. }
  418. Vector3 PhysicsTestMotionResult3D::get_collision_normal(int p_collision_index) const {
  419. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, Vector3());
  420. return result.collisions[p_collision_index].normal;
  421. }
  422. Vector3 PhysicsTestMotionResult3D::get_collider_velocity(int p_collision_index) const {
  423. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, Vector3());
  424. return result.collisions[p_collision_index].collider_velocity;
  425. }
  426. ObjectID PhysicsTestMotionResult3D::get_collider_id(int p_collision_index) const {
  427. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, ObjectID());
  428. return result.collisions[p_collision_index].collider_id;
  429. }
  430. RID PhysicsTestMotionResult3D::get_collider_rid(int p_collision_index) const {
  431. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, RID());
  432. return result.collisions[p_collision_index].collider;
  433. }
  434. Object *PhysicsTestMotionResult3D::get_collider(int p_collision_index) const {
  435. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, nullptr);
  436. return ObjectDB::get_instance(result.collisions[p_collision_index].collider_id);
  437. }
  438. int PhysicsTestMotionResult3D::get_collider_shape(int p_collision_index) const {
  439. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, 0);
  440. return result.collisions[p_collision_index].collider_shape;
  441. }
  442. int PhysicsTestMotionResult3D::get_collision_local_shape(int p_collision_index) const {
  443. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, 0);
  444. return result.collisions[p_collision_index].local_shape;
  445. }
  446. real_t PhysicsTestMotionResult3D::get_collision_depth(int p_collision_index) const {
  447. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, 0.0);
  448. return result.collisions[p_collision_index].depth;
  449. }
  450. void PhysicsTestMotionResult3D::_bind_methods() {
  451. ClassDB::bind_method(D_METHOD("get_travel"), &PhysicsTestMotionResult3D::get_travel);
  452. ClassDB::bind_method(D_METHOD("get_remainder"), &PhysicsTestMotionResult3D::get_remainder);
  453. ClassDB::bind_method(D_METHOD("get_collision_safe_fraction"), &PhysicsTestMotionResult3D::get_collision_safe_fraction);
  454. ClassDB::bind_method(D_METHOD("get_collision_unsafe_fraction"), &PhysicsTestMotionResult3D::get_collision_unsafe_fraction);
  455. ClassDB::bind_method(D_METHOD("get_collision_count"), &PhysicsTestMotionResult3D::get_collision_count);
  456. ClassDB::bind_method(D_METHOD("get_collision_point", "collision_index"), &PhysicsTestMotionResult3D::get_collision_point, DEFVAL(0));
  457. ClassDB::bind_method(D_METHOD("get_collision_normal", "collision_index"), &PhysicsTestMotionResult3D::get_collision_normal, DEFVAL(0));
  458. ClassDB::bind_method(D_METHOD("get_collider_velocity", "collision_index"), &PhysicsTestMotionResult3D::get_collider_velocity, DEFVAL(0));
  459. ClassDB::bind_method(D_METHOD("get_collider_id", "collision_index"), &PhysicsTestMotionResult3D::get_collider_id, DEFVAL(0));
  460. ClassDB::bind_method(D_METHOD("get_collider_rid", "collision_index"), &PhysicsTestMotionResult3D::get_collider_rid, DEFVAL(0));
  461. ClassDB::bind_method(D_METHOD("get_collider", "collision_index"), &PhysicsTestMotionResult3D::get_collider, DEFVAL(0));
  462. ClassDB::bind_method(D_METHOD("get_collider_shape", "collision_index"), &PhysicsTestMotionResult3D::get_collider_shape, DEFVAL(0));
  463. ClassDB::bind_method(D_METHOD("get_collision_local_shape", "collision_index"), &PhysicsTestMotionResult3D::get_collision_local_shape, DEFVAL(0));
  464. ClassDB::bind_method(D_METHOD("get_collision_depth", "collision_index"), &PhysicsTestMotionResult3D::get_collision_depth, DEFVAL(0));
  465. }
  466. ///////////////////////////////////////
  467. bool PhysicsServer3D::_body_test_motion(RID p_body, const Ref<PhysicsTestMotionParameters3D> &p_parameters, const Ref<PhysicsTestMotionResult3D> &p_result) {
  468. ERR_FAIL_COND_V(!p_parameters.is_valid(), false);
  469. MotionResult *result_ptr = nullptr;
  470. if (p_result.is_valid()) {
  471. result_ptr = p_result->get_result_ptr();
  472. }
  473. return body_test_motion(p_body, p_parameters->get_parameters(), result_ptr);
  474. }
  475. RID PhysicsServer3D::shape_create(ShapeType p_shape) {
  476. switch (p_shape) {
  477. case SHAPE_WORLD_BOUNDARY:
  478. return world_boundary_shape_create();
  479. case SHAPE_SEPARATION_RAY:
  480. return separation_ray_shape_create();
  481. case SHAPE_SPHERE:
  482. return sphere_shape_create();
  483. case SHAPE_BOX:
  484. return box_shape_create();
  485. case SHAPE_CAPSULE:
  486. return capsule_shape_create();
  487. case SHAPE_CYLINDER:
  488. return cylinder_shape_create();
  489. case SHAPE_CONVEX_POLYGON:
  490. return convex_polygon_shape_create();
  491. case SHAPE_CONCAVE_POLYGON:
  492. return concave_polygon_shape_create();
  493. case SHAPE_HEIGHTMAP:
  494. return heightmap_shape_create();
  495. case SHAPE_CUSTOM:
  496. return custom_shape_create();
  497. default:
  498. return RID();
  499. }
  500. }
  501. void PhysicsServer3D::_bind_methods() {
  502. #ifndef _3D_DISABLED
  503. ClassDB::bind_method(D_METHOD("world_boundary_shape_create"), &PhysicsServer3D::world_boundary_shape_create);
  504. ClassDB::bind_method(D_METHOD("separation_ray_shape_create"), &PhysicsServer3D::separation_ray_shape_create);
  505. ClassDB::bind_method(D_METHOD("sphere_shape_create"), &PhysicsServer3D::sphere_shape_create);
  506. ClassDB::bind_method(D_METHOD("box_shape_create"), &PhysicsServer3D::box_shape_create);
  507. ClassDB::bind_method(D_METHOD("capsule_shape_create"), &PhysicsServer3D::capsule_shape_create);
  508. ClassDB::bind_method(D_METHOD("cylinder_shape_create"), &PhysicsServer3D::cylinder_shape_create);
  509. ClassDB::bind_method(D_METHOD("convex_polygon_shape_create"), &PhysicsServer3D::convex_polygon_shape_create);
  510. ClassDB::bind_method(D_METHOD("concave_polygon_shape_create"), &PhysicsServer3D::concave_polygon_shape_create);
  511. ClassDB::bind_method(D_METHOD("heightmap_shape_create"), &PhysicsServer3D::heightmap_shape_create);
  512. ClassDB::bind_method(D_METHOD("custom_shape_create"), &PhysicsServer3D::custom_shape_create);
  513. ClassDB::bind_method(D_METHOD("shape_set_data", "shape", "data"), &PhysicsServer3D::shape_set_data);
  514. ClassDB::bind_method(D_METHOD("shape_get_type", "shape"), &PhysicsServer3D::shape_get_type);
  515. ClassDB::bind_method(D_METHOD("shape_get_data", "shape"), &PhysicsServer3D::shape_get_data);
  516. ClassDB::bind_method(D_METHOD("space_create"), &PhysicsServer3D::space_create);
  517. ClassDB::bind_method(D_METHOD("space_set_active", "space", "active"), &PhysicsServer3D::space_set_active);
  518. ClassDB::bind_method(D_METHOD("space_is_active", "space"), &PhysicsServer3D::space_is_active);
  519. ClassDB::bind_method(D_METHOD("space_set_param", "space", "param", "value"), &PhysicsServer3D::space_set_param);
  520. ClassDB::bind_method(D_METHOD("space_get_param", "space", "param"), &PhysicsServer3D::space_get_param);
  521. ClassDB::bind_method(D_METHOD("space_get_direct_state", "space"), &PhysicsServer3D::space_get_direct_state);
  522. ClassDB::bind_method(D_METHOD("area_create"), &PhysicsServer3D::area_create);
  523. ClassDB::bind_method(D_METHOD("area_set_space", "area", "space"), &PhysicsServer3D::area_set_space);
  524. ClassDB::bind_method(D_METHOD("area_get_space", "area"), &PhysicsServer3D::area_get_space);
  525. ClassDB::bind_method(D_METHOD("area_set_space_override_mode", "area", "mode"), &PhysicsServer3D::area_set_space_override_mode);
  526. ClassDB::bind_method(D_METHOD("area_get_space_override_mode", "area"), &PhysicsServer3D::area_get_space_override_mode);
  527. ClassDB::bind_method(D_METHOD("area_add_shape", "area", "shape", "transform", "disabled"), &PhysicsServer3D::area_add_shape, DEFVAL(Transform3D()), DEFVAL(false));
  528. ClassDB::bind_method(D_METHOD("area_set_shape", "area", "shape_idx", "shape"), &PhysicsServer3D::area_set_shape);
  529. ClassDB::bind_method(D_METHOD("area_set_shape_transform", "area", "shape_idx", "transform"), &PhysicsServer3D::area_set_shape_transform);
  530. ClassDB::bind_method(D_METHOD("area_set_shape_disabled", "area", "shape_idx", "disabled"), &PhysicsServer3D::area_set_shape_disabled);
  531. ClassDB::bind_method(D_METHOD("area_get_shape_count", "area"), &PhysicsServer3D::area_get_shape_count);
  532. ClassDB::bind_method(D_METHOD("area_get_shape", "area", "shape_idx"), &PhysicsServer3D::area_get_shape);
  533. ClassDB::bind_method(D_METHOD("area_get_shape_transform", "area", "shape_idx"), &PhysicsServer3D::area_get_shape_transform);
  534. ClassDB::bind_method(D_METHOD("area_remove_shape", "area", "shape_idx"), &PhysicsServer3D::area_remove_shape);
  535. ClassDB::bind_method(D_METHOD("area_clear_shapes", "area"), &PhysicsServer3D::area_clear_shapes);
  536. ClassDB::bind_method(D_METHOD("area_set_collision_layer", "area", "layer"), &PhysicsServer3D::area_set_collision_layer);
  537. ClassDB::bind_method(D_METHOD("area_set_collision_mask", "area", "mask"), &PhysicsServer3D::area_set_collision_mask);
  538. ClassDB::bind_method(D_METHOD("area_set_param", "area", "param", "value"), &PhysicsServer3D::area_set_param);
  539. ClassDB::bind_method(D_METHOD("area_set_transform", "area", "transform"), &PhysicsServer3D::area_set_transform);
  540. ClassDB::bind_method(D_METHOD("area_get_param", "area", "param"), &PhysicsServer3D::area_get_param);
  541. ClassDB::bind_method(D_METHOD("area_get_transform", "area"), &PhysicsServer3D::area_get_transform);
  542. ClassDB::bind_method(D_METHOD("area_attach_object_instance_id", "area", "id"), &PhysicsServer3D::area_attach_object_instance_id);
  543. ClassDB::bind_method(D_METHOD("area_get_object_instance_id", "area"), &PhysicsServer3D::area_get_object_instance_id);
  544. ClassDB::bind_method(D_METHOD("area_set_monitor_callback", "area", "callback"), &PhysicsServer3D::area_set_monitor_callback);
  545. ClassDB::bind_method(D_METHOD("area_set_area_monitor_callback", "area", "callback"), &PhysicsServer3D::area_set_area_monitor_callback);
  546. ClassDB::bind_method(D_METHOD("area_set_monitorable", "area", "monitorable"), &PhysicsServer3D::area_set_monitorable);
  547. ClassDB::bind_method(D_METHOD("area_set_ray_pickable", "area", "enable"), &PhysicsServer3D::area_set_ray_pickable);
  548. ClassDB::bind_method(D_METHOD("body_create"), &PhysicsServer3D::body_create);
  549. ClassDB::bind_method(D_METHOD("body_set_space", "body", "space"), &PhysicsServer3D::body_set_space);
  550. ClassDB::bind_method(D_METHOD("body_get_space", "body"), &PhysicsServer3D::body_get_space);
  551. ClassDB::bind_method(D_METHOD("body_set_mode", "body", "mode"), &PhysicsServer3D::body_set_mode);
  552. ClassDB::bind_method(D_METHOD("body_get_mode", "body"), &PhysicsServer3D::body_get_mode);
  553. ClassDB::bind_method(D_METHOD("body_set_collision_layer", "body", "layer"), &PhysicsServer3D::body_set_collision_layer);
  554. ClassDB::bind_method(D_METHOD("body_get_collision_layer", "body"), &PhysicsServer3D::body_get_collision_layer);
  555. ClassDB::bind_method(D_METHOD("body_set_collision_mask", "body", "mask"), &PhysicsServer3D::body_set_collision_mask);
  556. ClassDB::bind_method(D_METHOD("body_get_collision_mask", "body"), &PhysicsServer3D::body_get_collision_mask);
  557. ClassDB::bind_method(D_METHOD("body_add_shape", "body", "shape", "transform", "disabled"), &PhysicsServer3D::body_add_shape, DEFVAL(Transform3D()), DEFVAL(false));
  558. ClassDB::bind_method(D_METHOD("body_set_shape", "body", "shape_idx", "shape"), &PhysicsServer3D::body_set_shape);
  559. ClassDB::bind_method(D_METHOD("body_set_shape_transform", "body", "shape_idx", "transform"), &PhysicsServer3D::body_set_shape_transform);
  560. ClassDB::bind_method(D_METHOD("body_set_shape_disabled", "body", "shape_idx", "disabled"), &PhysicsServer3D::body_set_shape_disabled);
  561. ClassDB::bind_method(D_METHOD("body_get_shape_count", "body"), &PhysicsServer3D::body_get_shape_count);
  562. ClassDB::bind_method(D_METHOD("body_get_shape", "body", "shape_idx"), &PhysicsServer3D::body_get_shape);
  563. ClassDB::bind_method(D_METHOD("body_get_shape_transform", "body", "shape_idx"), &PhysicsServer3D::body_get_shape_transform);
  564. ClassDB::bind_method(D_METHOD("body_remove_shape", "body", "shape_idx"), &PhysicsServer3D::body_remove_shape);
  565. ClassDB::bind_method(D_METHOD("body_clear_shapes", "body"), &PhysicsServer3D::body_clear_shapes);
  566. ClassDB::bind_method(D_METHOD("body_attach_object_instance_id", "body", "id"), &PhysicsServer3D::body_attach_object_instance_id);
  567. ClassDB::bind_method(D_METHOD("body_get_object_instance_id", "body"), &PhysicsServer3D::body_get_object_instance_id);
  568. ClassDB::bind_method(D_METHOD("body_set_enable_continuous_collision_detection", "body", "enable"), &PhysicsServer3D::body_set_enable_continuous_collision_detection);
  569. ClassDB::bind_method(D_METHOD("body_is_continuous_collision_detection_enabled", "body"), &PhysicsServer3D::body_is_continuous_collision_detection_enabled);
  570. ClassDB::bind_method(D_METHOD("body_set_param", "body", "param", "value"), &PhysicsServer3D::body_set_param);
  571. ClassDB::bind_method(D_METHOD("body_get_param", "body", "param"), &PhysicsServer3D::body_get_param);
  572. ClassDB::bind_method(D_METHOD("body_reset_mass_properties", "body"), &PhysicsServer3D::body_reset_mass_properties);
  573. ClassDB::bind_method(D_METHOD("body_set_state", "body", "state", "value"), &PhysicsServer3D::body_set_state);
  574. ClassDB::bind_method(D_METHOD("body_get_state", "body", "state"), &PhysicsServer3D::body_get_state);
  575. ClassDB::bind_method(D_METHOD("body_add_central_force", "body", "force"), &PhysicsServer3D::body_add_central_force);
  576. ClassDB::bind_method(D_METHOD("body_add_force", "body", "force", "position"), &PhysicsServer3D::body_add_force, Vector3());
  577. ClassDB::bind_method(D_METHOD("body_add_torque", "body", "torque"), &PhysicsServer3D::body_add_torque);
  578. ClassDB::bind_method(D_METHOD("body_apply_central_impulse", "body", "impulse"), &PhysicsServer3D::body_apply_central_impulse);
  579. ClassDB::bind_method(D_METHOD("body_apply_impulse", "body", "impulse", "position"), &PhysicsServer3D::body_apply_impulse, Vector3());
  580. ClassDB::bind_method(D_METHOD("body_apply_torque_impulse", "body", "impulse"), &PhysicsServer3D::body_apply_torque_impulse);
  581. ClassDB::bind_method(D_METHOD("body_set_axis_velocity", "body", "axis_velocity"), &PhysicsServer3D::body_set_axis_velocity);
  582. ClassDB::bind_method(D_METHOD("body_set_axis_lock", "body", "axis", "lock"), &PhysicsServer3D::body_set_axis_lock);
  583. ClassDB::bind_method(D_METHOD("body_is_axis_locked", "body", "axis"), &PhysicsServer3D::body_is_axis_locked);
  584. ClassDB::bind_method(D_METHOD("body_add_collision_exception", "body", "excepted_body"), &PhysicsServer3D::body_add_collision_exception);
  585. ClassDB::bind_method(D_METHOD("body_remove_collision_exception", "body", "excepted_body"), &PhysicsServer3D::body_remove_collision_exception);
  586. ClassDB::bind_method(D_METHOD("body_set_max_contacts_reported", "body", "amount"), &PhysicsServer3D::body_set_max_contacts_reported);
  587. ClassDB::bind_method(D_METHOD("body_get_max_contacts_reported", "body"), &PhysicsServer3D::body_get_max_contacts_reported);
  588. ClassDB::bind_method(D_METHOD("body_set_omit_force_integration", "body", "enable"), &PhysicsServer3D::body_set_omit_force_integration);
  589. ClassDB::bind_method(D_METHOD("body_is_omitting_force_integration", "body"), &PhysicsServer3D::body_is_omitting_force_integration);
  590. ClassDB::bind_method(D_METHOD("body_set_force_integration_callback", "body", "callable", "userdata"), &PhysicsServer3D::body_set_force_integration_callback, DEFVAL(Variant()));
  591. ClassDB::bind_method(D_METHOD("body_set_ray_pickable", "body", "enable"), &PhysicsServer3D::body_set_ray_pickable);
  592. ClassDB::bind_method(D_METHOD("body_test_motion", "body", "parameters", "result"), &PhysicsServer3D::_body_test_motion, DEFVAL(Variant()));
  593. ClassDB::bind_method(D_METHOD("body_get_direct_state", "body"), &PhysicsServer3D::body_get_direct_state);
  594. /* SOFT BODY API */
  595. ClassDB::bind_method(D_METHOD("soft_body_get_bounds", "body"), &PhysicsServer3D::soft_body_get_bounds);
  596. /* JOINT API */
  597. ClassDB::bind_method(D_METHOD("joint_create"), &PhysicsServer3D::joint_create);
  598. ClassDB::bind_method(D_METHOD("joint_clear", "joint"), &PhysicsServer3D::joint_clear);
  599. BIND_ENUM_CONSTANT(JOINT_TYPE_PIN);
  600. BIND_ENUM_CONSTANT(JOINT_TYPE_HINGE);
  601. BIND_ENUM_CONSTANT(JOINT_TYPE_SLIDER);
  602. BIND_ENUM_CONSTANT(JOINT_TYPE_CONE_TWIST);
  603. BIND_ENUM_CONSTANT(JOINT_TYPE_6DOF);
  604. BIND_ENUM_CONSTANT(JOINT_TYPE_MAX);
  605. ClassDB::bind_method(D_METHOD("joint_make_pin", "joint", "body_A", "local_A", "body_B", "local_B"), &PhysicsServer3D::joint_make_pin);
  606. ClassDB::bind_method(D_METHOD("pin_joint_set_param", "joint", "param", "value"), &PhysicsServer3D::pin_joint_set_param);
  607. ClassDB::bind_method(D_METHOD("pin_joint_get_param", "joint", "param"), &PhysicsServer3D::pin_joint_get_param);
  608. ClassDB::bind_method(D_METHOD("pin_joint_set_local_a", "joint", "local_A"), &PhysicsServer3D::pin_joint_set_local_a);
  609. ClassDB::bind_method(D_METHOD("pin_joint_get_local_a", "joint"), &PhysicsServer3D::pin_joint_get_local_a);
  610. ClassDB::bind_method(D_METHOD("pin_joint_set_local_b", "joint", "local_B"), &PhysicsServer3D::pin_joint_set_local_b);
  611. ClassDB::bind_method(D_METHOD("pin_joint_get_local_b", "joint"), &PhysicsServer3D::pin_joint_get_local_b);
  612. BIND_ENUM_CONSTANT(PIN_JOINT_BIAS);
  613. BIND_ENUM_CONSTANT(PIN_JOINT_DAMPING);
  614. BIND_ENUM_CONSTANT(PIN_JOINT_IMPULSE_CLAMP);
  615. BIND_ENUM_CONSTANT(HINGE_JOINT_BIAS);
  616. BIND_ENUM_CONSTANT(HINGE_JOINT_LIMIT_UPPER);
  617. BIND_ENUM_CONSTANT(HINGE_JOINT_LIMIT_LOWER);
  618. BIND_ENUM_CONSTANT(HINGE_JOINT_LIMIT_BIAS);
  619. BIND_ENUM_CONSTANT(HINGE_JOINT_LIMIT_SOFTNESS);
  620. BIND_ENUM_CONSTANT(HINGE_JOINT_LIMIT_RELAXATION);
  621. BIND_ENUM_CONSTANT(HINGE_JOINT_MOTOR_TARGET_VELOCITY);
  622. BIND_ENUM_CONSTANT(HINGE_JOINT_MOTOR_MAX_IMPULSE);
  623. BIND_ENUM_CONSTANT(HINGE_JOINT_FLAG_USE_LIMIT);
  624. BIND_ENUM_CONSTANT(HINGE_JOINT_FLAG_ENABLE_MOTOR);
  625. ClassDB::bind_method(D_METHOD("joint_make_hinge", "joint", "body_A", "hinge_A", "body_B", "hinge_B"), &PhysicsServer3D::joint_make_hinge);
  626. ClassDB::bind_method(D_METHOD("hinge_joint_set_param", "joint", "param", "value"), &PhysicsServer3D::hinge_joint_set_param);
  627. ClassDB::bind_method(D_METHOD("hinge_joint_get_param", "joint", "param"), &PhysicsServer3D::hinge_joint_get_param);
  628. ClassDB::bind_method(D_METHOD("hinge_joint_set_flag", "joint", "flag", "enabled"), &PhysicsServer3D::hinge_joint_set_flag);
  629. ClassDB::bind_method(D_METHOD("hinge_joint_get_flag", "joint", "flag"), &PhysicsServer3D::hinge_joint_get_flag);
  630. ClassDB::bind_method(D_METHOD("joint_make_slider", "joint", "body_A", "local_ref_A", "body_B", "local_ref_B"), &PhysicsServer3D::joint_make_slider);
  631. ClassDB::bind_method(D_METHOD("slider_joint_set_param", "joint", "param", "value"), &PhysicsServer3D::slider_joint_set_param);
  632. ClassDB::bind_method(D_METHOD("slider_joint_get_param", "joint", "param"), &PhysicsServer3D::slider_joint_get_param);
  633. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_LIMIT_UPPER);
  634. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_LIMIT_LOWER);
  635. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS);
  636. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION);
  637. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_LIMIT_DAMPING);
  638. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_MOTION_SOFTNESS);
  639. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_MOTION_RESTITUTION);
  640. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_MOTION_DAMPING);
  641. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_ORTHOGONAL_SOFTNESS);
  642. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_ORTHOGONAL_RESTITUTION);
  643. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_ORTHOGONAL_DAMPING);
  644. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_LIMIT_UPPER);
  645. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_LIMIT_LOWER);
  646. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS);
  647. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_LIMIT_RESTITUTION);
  648. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_LIMIT_DAMPING);
  649. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_MOTION_SOFTNESS);
  650. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_MOTION_RESTITUTION);
  651. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_MOTION_DAMPING);
  652. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_ORTHOGONAL_SOFTNESS);
  653. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_ORTHOGONAL_RESTITUTION);
  654. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_ORTHOGONAL_DAMPING);
  655. BIND_ENUM_CONSTANT(SLIDER_JOINT_MAX);
  656. ClassDB::bind_method(D_METHOD("joint_make_cone_twist", "joint", "body_A", "local_ref_A", "body_B", "local_ref_B"), &PhysicsServer3D::joint_make_cone_twist);
  657. ClassDB::bind_method(D_METHOD("cone_twist_joint_set_param", "joint", "param", "value"), &PhysicsServer3D::cone_twist_joint_set_param);
  658. ClassDB::bind_method(D_METHOD("cone_twist_joint_get_param", "joint", "param"), &PhysicsServer3D::cone_twist_joint_get_param);
  659. BIND_ENUM_CONSTANT(CONE_TWIST_JOINT_SWING_SPAN);
  660. BIND_ENUM_CONSTANT(CONE_TWIST_JOINT_TWIST_SPAN);
  661. BIND_ENUM_CONSTANT(CONE_TWIST_JOINT_BIAS);
  662. BIND_ENUM_CONSTANT(CONE_TWIST_JOINT_SOFTNESS);
  663. BIND_ENUM_CONSTANT(CONE_TWIST_JOINT_RELAXATION);
  664. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_LOWER_LIMIT);
  665. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_UPPER_LIMIT);
  666. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS);
  667. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_RESTITUTION);
  668. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_DAMPING);
  669. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_MOTOR_TARGET_VELOCITY);
  670. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_MOTOR_FORCE_LIMIT);
  671. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_LOWER_LIMIT);
  672. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_UPPER_LIMIT);
  673. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS);
  674. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_DAMPING);
  675. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_RESTITUTION);
  676. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_FORCE_LIMIT);
  677. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_ERP);
  678. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_MOTOR_TARGET_VELOCITY);
  679. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_MOTOR_FORCE_LIMIT);
  680. BIND_ENUM_CONSTANT(G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT);
  681. BIND_ENUM_CONSTANT(G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT);
  682. BIND_ENUM_CONSTANT(G6DOF_JOINT_FLAG_ENABLE_MOTOR);
  683. BIND_ENUM_CONSTANT(G6DOF_JOINT_FLAG_ENABLE_LINEAR_MOTOR);
  684. ClassDB::bind_method(D_METHOD("joint_get_type", "joint"), &PhysicsServer3D::joint_get_type);
  685. ClassDB::bind_method(D_METHOD("joint_set_solver_priority", "joint", "priority"), &PhysicsServer3D::joint_set_solver_priority);
  686. ClassDB::bind_method(D_METHOD("joint_get_solver_priority", "joint"), &PhysicsServer3D::joint_get_solver_priority);
  687. ClassDB::bind_method(D_METHOD("joint_make_generic_6dof", "joint", "body_A", "local_ref_A", "body_B", "local_ref_B"), &PhysicsServer3D::joint_make_generic_6dof);
  688. ClassDB::bind_method(D_METHOD("generic_6dof_joint_set_param", "joint", "axis", "param", "value"), &PhysicsServer3D::generic_6dof_joint_set_param);
  689. ClassDB::bind_method(D_METHOD("generic_6dof_joint_get_param", "joint", "axis", "param"), &PhysicsServer3D::generic_6dof_joint_get_param);
  690. ClassDB::bind_method(D_METHOD("generic_6dof_joint_set_flag", "joint", "axis", "flag", "enable"), &PhysicsServer3D::generic_6dof_joint_set_flag);
  691. ClassDB::bind_method(D_METHOD("generic_6dof_joint_get_flag", "joint", "axis", "flag"), &PhysicsServer3D::generic_6dof_joint_get_flag);
  692. ClassDB::bind_method(D_METHOD("free_rid", "rid"), &PhysicsServer3D::free);
  693. ClassDB::bind_method(D_METHOD("set_active", "active"), &PhysicsServer3D::set_active);
  694. ClassDB::bind_method(D_METHOD("set_collision_iterations", "iterations"), &PhysicsServer3D::set_collision_iterations);
  695. ClassDB::bind_method(D_METHOD("get_process_info", "process_info"), &PhysicsServer3D::get_process_info);
  696. BIND_ENUM_CONSTANT(SHAPE_WORLD_BOUNDARY);
  697. BIND_ENUM_CONSTANT(SHAPE_SEPARATION_RAY);
  698. BIND_ENUM_CONSTANT(SHAPE_SPHERE);
  699. BIND_ENUM_CONSTANT(SHAPE_BOX);
  700. BIND_ENUM_CONSTANT(SHAPE_CAPSULE);
  701. BIND_ENUM_CONSTANT(SHAPE_CYLINDER);
  702. BIND_ENUM_CONSTANT(SHAPE_CONVEX_POLYGON);
  703. BIND_ENUM_CONSTANT(SHAPE_CONCAVE_POLYGON);
  704. BIND_ENUM_CONSTANT(SHAPE_HEIGHTMAP);
  705. BIND_ENUM_CONSTANT(SHAPE_SOFT_BODY);
  706. BIND_ENUM_CONSTANT(SHAPE_CUSTOM);
  707. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY);
  708. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_VECTOR);
  709. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_IS_POINT);
  710. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_DISTANCE_SCALE);
  711. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_POINT_ATTENUATION);
  712. BIND_ENUM_CONSTANT(AREA_PARAM_LINEAR_DAMP);
  713. BIND_ENUM_CONSTANT(AREA_PARAM_ANGULAR_DAMP);
  714. BIND_ENUM_CONSTANT(AREA_PARAM_PRIORITY);
  715. BIND_ENUM_CONSTANT(AREA_PARAM_WIND_FORCE_MAGNITUDE);
  716. BIND_ENUM_CONSTANT(AREA_PARAM_WIND_SOURCE);
  717. BIND_ENUM_CONSTANT(AREA_PARAM_WIND_DIRECTION);
  718. BIND_ENUM_CONSTANT(AREA_PARAM_WIND_ATTENUATION_FACTOR);
  719. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_DISABLED);
  720. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_COMBINE);
  721. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_COMBINE_REPLACE);
  722. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_REPLACE);
  723. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_REPLACE_COMBINE);
  724. BIND_ENUM_CONSTANT(BODY_MODE_STATIC);
  725. BIND_ENUM_CONSTANT(BODY_MODE_KINEMATIC);
  726. BIND_ENUM_CONSTANT(BODY_MODE_DYNAMIC);
  727. BIND_ENUM_CONSTANT(BODY_MODE_DYNAMIC_LINEAR);
  728. BIND_ENUM_CONSTANT(BODY_PARAM_BOUNCE);
  729. BIND_ENUM_CONSTANT(BODY_PARAM_FRICTION);
  730. BIND_ENUM_CONSTANT(BODY_PARAM_MASS);
  731. BIND_ENUM_CONSTANT(BODY_PARAM_INERTIA);
  732. BIND_ENUM_CONSTANT(BODY_PARAM_CENTER_OF_MASS);
  733. BIND_ENUM_CONSTANT(BODY_PARAM_GRAVITY_SCALE);
  734. BIND_ENUM_CONSTANT(BODY_PARAM_LINEAR_DAMP_MODE);
  735. BIND_ENUM_CONSTANT(BODY_PARAM_ANGULAR_DAMP_MODE);
  736. BIND_ENUM_CONSTANT(BODY_PARAM_LINEAR_DAMP);
  737. BIND_ENUM_CONSTANT(BODY_PARAM_ANGULAR_DAMP);
  738. BIND_ENUM_CONSTANT(BODY_PARAM_MAX);
  739. BIND_ENUM_CONSTANT(BODY_DAMP_MODE_COMBINE);
  740. BIND_ENUM_CONSTANT(BODY_DAMP_MODE_REPLACE);
  741. BIND_ENUM_CONSTANT(BODY_STATE_TRANSFORM);
  742. BIND_ENUM_CONSTANT(BODY_STATE_LINEAR_VELOCITY);
  743. BIND_ENUM_CONSTANT(BODY_STATE_ANGULAR_VELOCITY);
  744. BIND_ENUM_CONSTANT(BODY_STATE_SLEEPING);
  745. BIND_ENUM_CONSTANT(BODY_STATE_CAN_SLEEP);
  746. BIND_ENUM_CONSTANT(AREA_BODY_ADDED);
  747. BIND_ENUM_CONSTANT(AREA_BODY_REMOVED);
  748. BIND_ENUM_CONSTANT(INFO_ACTIVE_OBJECTS);
  749. BIND_ENUM_CONSTANT(INFO_COLLISION_PAIRS);
  750. BIND_ENUM_CONSTANT(INFO_ISLAND_COUNT);
  751. BIND_ENUM_CONSTANT(SPACE_PARAM_CONTACT_RECYCLE_RADIUS);
  752. BIND_ENUM_CONSTANT(SPACE_PARAM_CONTACT_MAX_SEPARATION);
  753. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION);
  754. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD);
  755. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD);
  756. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_TIME_TO_SLEEP);
  757. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO);
  758. BIND_ENUM_CONSTANT(SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS);
  759. BIND_ENUM_CONSTANT(BODY_AXIS_LINEAR_X);
  760. BIND_ENUM_CONSTANT(BODY_AXIS_LINEAR_Y);
  761. BIND_ENUM_CONSTANT(BODY_AXIS_LINEAR_Z);
  762. BIND_ENUM_CONSTANT(BODY_AXIS_ANGULAR_X);
  763. BIND_ENUM_CONSTANT(BODY_AXIS_ANGULAR_Y);
  764. BIND_ENUM_CONSTANT(BODY_AXIS_ANGULAR_Z);
  765. #endif
  766. }
  767. PhysicsServer3D::PhysicsServer3D() {
  768. singleton = this;
  769. }
  770. PhysicsServer3D::~PhysicsServer3D() {
  771. singleton = nullptr;
  772. }
  773. Vector<PhysicsServer3DManager::ClassInfo> PhysicsServer3DManager::physics_servers;
  774. int PhysicsServer3DManager::default_server_id = -1;
  775. int PhysicsServer3DManager::default_server_priority = -1;
  776. const String PhysicsServer3DManager::setting_property_name("physics/3d/physics_engine");
  777. void PhysicsServer3DManager::on_servers_changed() {
  778. String physics_servers2("DEFAULT");
  779. for (int i = get_servers_count() - 1; 0 <= i; --i) {
  780. physics_servers2 += "," + get_server_name(i);
  781. }
  782. ProjectSettings::get_singleton()->set_custom_property_info(setting_property_name, PropertyInfo(Variant::STRING, setting_property_name, PROPERTY_HINT_ENUM, physics_servers2));
  783. }
  784. void PhysicsServer3DManager::register_server(const String &p_name, CreatePhysicsServer3DCallback p_creat_callback) {
  785. ERR_FAIL_COND(!p_creat_callback);
  786. ERR_FAIL_COND(find_server_id(p_name) != -1);
  787. physics_servers.push_back(ClassInfo(p_name, p_creat_callback));
  788. on_servers_changed();
  789. }
  790. void PhysicsServer3DManager::set_default_server(const String &p_name, int p_priority) {
  791. const int id = find_server_id(p_name);
  792. ERR_FAIL_COND(id == -1); // Not found
  793. if (default_server_priority < p_priority) {
  794. default_server_id = id;
  795. default_server_priority = p_priority;
  796. }
  797. }
  798. int PhysicsServer3DManager::find_server_id(const String &p_name) {
  799. for (int i = physics_servers.size() - 1; 0 <= i; --i) {
  800. if (p_name == physics_servers[i].name) {
  801. return i;
  802. }
  803. }
  804. return -1;
  805. }
  806. int PhysicsServer3DManager::get_servers_count() {
  807. return physics_servers.size();
  808. }
  809. String PhysicsServer3DManager::get_server_name(int p_id) {
  810. ERR_FAIL_INDEX_V(p_id, get_servers_count(), "");
  811. return physics_servers[p_id].name;
  812. }
  813. PhysicsServer3D *PhysicsServer3DManager::new_default_server() {
  814. ERR_FAIL_COND_V(default_server_id == -1, nullptr);
  815. return physics_servers[default_server_id].create_callback();
  816. }
  817. PhysicsServer3D *PhysicsServer3DManager::new_server(const String &p_name) {
  818. int id = find_server_id(p_name);
  819. if (id == -1) {
  820. return nullptr;
  821. } else {
  822. return physics_servers[id].create_callback();
  823. }
  824. }