physics_server.cpp 44 KB

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