physics_server_3d.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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("add_central_force", "force"), &PhysicsDirectBodyState3D::add_central_force, Vector3());
  75. ClassDB::bind_method(D_METHOD("add_force", "force", "position"), &PhysicsDirectBodyState3D::add_force, Vector3());
  76. ClassDB::bind_method(D_METHOD("add_torque", "torque"), &PhysicsDirectBodyState3D::add_torque);
  77. ClassDB::bind_method(D_METHOD("apply_central_impulse", "impulse"), &PhysicsDirectBodyState3D::apply_central_impulse, Vector3());
  78. ClassDB::bind_method(D_METHOD("apply_impulse", "impulse", "position"), &PhysicsDirectBodyState3D::apply_impulse, Vector3());
  79. ClassDB::bind_method(D_METHOD("apply_torque_impulse", "impulse"), &PhysicsDirectBodyState3D::apply_torque_impulse);
  80. ClassDB::bind_method(D_METHOD("set_sleep_state", "enabled"), &PhysicsDirectBodyState3D::set_sleep_state);
  81. ClassDB::bind_method(D_METHOD("is_sleeping"), &PhysicsDirectBodyState3D::is_sleeping);
  82. ClassDB::bind_method(D_METHOD("get_contact_count"), &PhysicsDirectBodyState3D::get_contact_count);
  83. ClassDB::bind_method(D_METHOD("get_contact_local_position", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_local_position);
  84. ClassDB::bind_method(D_METHOD("get_contact_local_normal", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_local_normal);
  85. ClassDB::bind_method(D_METHOD("get_contact_impulse", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_impulse);
  86. ClassDB::bind_method(D_METHOD("get_contact_local_shape", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_local_shape);
  87. ClassDB::bind_method(D_METHOD("get_contact_collider", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_collider);
  88. ClassDB::bind_method(D_METHOD("get_contact_collider_position", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_collider_position);
  89. ClassDB::bind_method(D_METHOD("get_contact_collider_id", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_collider_id);
  90. ClassDB::bind_method(D_METHOD("get_contact_collider_object", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_collider_object);
  91. ClassDB::bind_method(D_METHOD("get_contact_collider_shape", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_collider_shape);
  92. ClassDB::bind_method(D_METHOD("get_contact_collider_velocity_at_position", "contact_idx"), &PhysicsDirectBodyState3D::get_contact_collider_velocity_at_position);
  93. ClassDB::bind_method(D_METHOD("get_step"), &PhysicsDirectBodyState3D::get_step);
  94. ClassDB::bind_method(D_METHOD("integrate_forces"), &PhysicsDirectBodyState3D::integrate_forces);
  95. ClassDB::bind_method(D_METHOD("get_space_state"), &PhysicsDirectBodyState3D::get_space_state);
  96. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "step"), "", "get_step");
  97. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inverse_mass"), "", "get_inverse_mass");
  98. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "total_angular_damp"), "", "get_total_angular_damp");
  99. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "total_linear_damp"), "", "get_total_linear_damp");
  100. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "inverse_inertia"), "", "get_inverse_inertia");
  101. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "total_gravity"), "", "get_total_gravity");
  102. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "center_of_mass"), "", "get_center_of_mass");
  103. ADD_PROPERTY(PropertyInfo(Variant::BASIS, "principal_inertia_axes"), "", "get_principal_inertia_axes");
  104. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "angular_velocity"), "set_angular_velocity", "get_angular_velocity");
  105. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "linear_velocity"), "set_linear_velocity", "get_linear_velocity");
  106. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sleeping"), "set_sleep_state", "is_sleeping");
  107. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "transform"), "set_transform", "get_transform");
  108. }
  109. PhysicsDirectBodyState3D::PhysicsDirectBodyState3D() {}
  110. ///////////////////////////////////////////////////////
  111. void PhysicsShapeQueryParameters3D::set_shape(const RES &p_shape_ref) {
  112. ERR_FAIL_COND(p_shape_ref.is_null());
  113. shape_ref = p_shape_ref;
  114. shape = p_shape_ref->get_rid();
  115. }
  116. RES PhysicsShapeQueryParameters3D::get_shape() const {
  117. return shape_ref;
  118. }
  119. void PhysicsShapeQueryParameters3D::set_shape_rid(const RID &p_shape) {
  120. if (shape != p_shape) {
  121. shape_ref = RES();
  122. shape = p_shape;
  123. }
  124. }
  125. RID PhysicsShapeQueryParameters3D::get_shape_rid() const {
  126. return shape;
  127. }
  128. void PhysicsShapeQueryParameters3D::set_transform(const Transform &p_transform) {
  129. transform = p_transform;
  130. }
  131. Transform PhysicsShapeQueryParameters3D::get_transform() const {
  132. return transform;
  133. }
  134. void PhysicsShapeQueryParameters3D::set_margin(real_t p_margin) {
  135. margin = p_margin;
  136. }
  137. real_t PhysicsShapeQueryParameters3D::get_margin() const {
  138. return margin;
  139. }
  140. void PhysicsShapeQueryParameters3D::set_collision_mask(int p_collision_mask) {
  141. collision_mask = p_collision_mask;
  142. }
  143. int PhysicsShapeQueryParameters3D::get_collision_mask() const {
  144. return collision_mask;
  145. }
  146. void PhysicsShapeQueryParameters3D::set_exclude(const Vector<RID> &p_exclude) {
  147. exclude.clear();
  148. for (int i = 0; i < p_exclude.size(); i++) {
  149. exclude.insert(p_exclude[i]);
  150. }
  151. }
  152. Vector<RID> PhysicsShapeQueryParameters3D::get_exclude() const {
  153. Vector<RID> ret;
  154. ret.resize(exclude.size());
  155. int idx = 0;
  156. for (Set<RID>::Element *E = exclude.front(); E; E = E->next()) {
  157. ret.write[idx] = E->get();
  158. }
  159. return ret;
  160. }
  161. void PhysicsShapeQueryParameters3D::set_collide_with_bodies(bool p_enable) {
  162. collide_with_bodies = p_enable;
  163. }
  164. bool PhysicsShapeQueryParameters3D::is_collide_with_bodies_enabled() const {
  165. return collide_with_bodies;
  166. }
  167. void PhysicsShapeQueryParameters3D::set_collide_with_areas(bool p_enable) {
  168. collide_with_areas = p_enable;
  169. }
  170. bool PhysicsShapeQueryParameters3D::is_collide_with_areas_enabled() const {
  171. return collide_with_areas;
  172. }
  173. void PhysicsShapeQueryParameters3D::_bind_methods() {
  174. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &PhysicsShapeQueryParameters3D::set_shape);
  175. ClassDB::bind_method(D_METHOD("get_shape"), &PhysicsShapeQueryParameters3D::get_shape);
  176. ClassDB::bind_method(D_METHOD("set_shape_rid", "shape"), &PhysicsShapeQueryParameters3D::set_shape_rid);
  177. ClassDB::bind_method(D_METHOD("get_shape_rid"), &PhysicsShapeQueryParameters3D::get_shape_rid);
  178. ClassDB::bind_method(D_METHOD("set_transform", "transform"), &PhysicsShapeQueryParameters3D::set_transform);
  179. ClassDB::bind_method(D_METHOD("get_transform"), &PhysicsShapeQueryParameters3D::get_transform);
  180. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &PhysicsShapeQueryParameters3D::set_margin);
  181. ClassDB::bind_method(D_METHOD("get_margin"), &PhysicsShapeQueryParameters3D::get_margin);
  182. ClassDB::bind_method(D_METHOD("set_collision_mask", "collision_mask"), &PhysicsShapeQueryParameters3D::set_collision_mask);
  183. ClassDB::bind_method(D_METHOD("get_collision_mask"), &PhysicsShapeQueryParameters3D::get_collision_mask);
  184. ClassDB::bind_method(D_METHOD("set_exclude", "exclude"), &PhysicsShapeQueryParameters3D::set_exclude);
  185. ClassDB::bind_method(D_METHOD("get_exclude"), &PhysicsShapeQueryParameters3D::get_exclude);
  186. ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &PhysicsShapeQueryParameters3D::set_collide_with_bodies);
  187. ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &PhysicsShapeQueryParameters3D::is_collide_with_bodies_enabled);
  188. ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &PhysicsShapeQueryParameters3D::set_collide_with_areas);
  189. ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &PhysicsShapeQueryParameters3D::is_collide_with_areas_enabled);
  190. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  191. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude", PROPERTY_HINT_NONE, itos(Variant::RID) + ":"), "set_exclude", "get_exclude");
  192. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin", PROPERTY_HINT_RANGE, "0,100,0.01"), "set_margin", "get_margin");
  193. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape3D"), "set_shape", "get_shape");
  194. ADD_PROPERTY(PropertyInfo(Variant::RID, "shape_rid"), "set_shape_rid", "get_shape_rid");
  195. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "transform"), "set_transform", "get_transform");
  196. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies"), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
  197. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas"), "set_collide_with_areas", "is_collide_with_areas_enabled");
  198. }
  199. PhysicsShapeQueryParameters3D::PhysicsShapeQueryParameters3D() {
  200. margin = 0;
  201. collision_mask = 0x7FFFFFFF;
  202. collide_with_bodies = true;
  203. collide_with_areas = false;
  204. }
  205. /////////////////////////////////////
  206. Dictionary PhysicsDirectSpaceState3D::_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) {
  207. RayResult inters;
  208. Set<RID> exclude;
  209. for (int i = 0; i < p_exclude.size(); i++) {
  210. exclude.insert(p_exclude[i]);
  211. }
  212. bool res = intersect_ray(p_from, p_to, inters, exclude, p_collision_mask, p_collide_with_bodies, p_collide_with_areas);
  213. if (!res) {
  214. return Dictionary();
  215. }
  216. Dictionary d;
  217. d["position"] = inters.position;
  218. d["normal"] = inters.normal;
  219. d["collider_id"] = inters.collider_id;
  220. d["collider"] = inters.collider;
  221. d["shape"] = inters.shape;
  222. d["rid"] = inters.rid;
  223. return d;
  224. }
  225. Array PhysicsDirectSpaceState3D::_intersect_shape(const Ref<PhysicsShapeQueryParameters3D> &p_shape_query, int p_max_results) {
  226. ERR_FAIL_COND_V(!p_shape_query.is_valid(), Array());
  227. Vector<ShapeResult> sr;
  228. sr.resize(p_max_results);
  229. 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);
  230. Array ret;
  231. ret.resize(rc);
  232. for (int i = 0; i < rc; i++) {
  233. Dictionary d;
  234. d["rid"] = sr[i].rid;
  235. d["collider_id"] = sr[i].collider_id;
  236. d["collider"] = sr[i].collider;
  237. d["shape"] = sr[i].shape;
  238. ret[i] = d;
  239. }
  240. return ret;
  241. }
  242. Array PhysicsDirectSpaceState3D::_cast_motion(const Ref<PhysicsShapeQueryParameters3D> &p_shape_query, const Vector3 &p_motion) {
  243. ERR_FAIL_COND_V(!p_shape_query.is_valid(), Array());
  244. real_t closest_safe = 1.0f, closest_unsafe = 1.0f;
  245. 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);
  246. if (!res) {
  247. return Array();
  248. }
  249. Array ret;
  250. ret.resize(2);
  251. ret[0] = closest_safe;
  252. ret[1] = closest_unsafe;
  253. return ret;
  254. }
  255. Array PhysicsDirectSpaceState3D::_collide_shape(const Ref<PhysicsShapeQueryParameters3D> &p_shape_query, int p_max_results) {
  256. ERR_FAIL_COND_V(!p_shape_query.is_valid(), Array());
  257. Vector<Vector3> ret;
  258. ret.resize(p_max_results * 2);
  259. int rc = 0;
  260. 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);
  261. if (!res) {
  262. return Array();
  263. }
  264. Array r;
  265. r.resize(rc * 2);
  266. for (int i = 0; i < rc * 2; i++) {
  267. r[i] = ret[i];
  268. }
  269. return r;
  270. }
  271. Dictionary PhysicsDirectSpaceState3D::_get_rest_info(const Ref<PhysicsShapeQueryParameters3D> &p_shape_query) {
  272. ERR_FAIL_COND_V(!p_shape_query.is_valid(), Dictionary());
  273. ShapeRestInfo sri;
  274. 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);
  275. Dictionary r;
  276. if (!res) {
  277. return r;
  278. }
  279. r["point"] = sri.point;
  280. r["normal"] = sri.normal;
  281. r["rid"] = sri.rid;
  282. r["collider_id"] = sri.collider_id;
  283. r["shape"] = sri.shape;
  284. r["linear_velocity"] = sri.linear_velocity;
  285. return r;
  286. }
  287. PhysicsDirectSpaceState3D::PhysicsDirectSpaceState3D() {
  288. }
  289. void PhysicsDirectSpaceState3D::_bind_methods() {
  290. ClassDB::bind_method(D_METHOD("intersect_ray", "from", "to", "exclude", "collision_mask", "collide_with_bodies", "collide_with_areas"), &PhysicsDirectSpaceState3D::_intersect_ray, DEFVAL(Array()), DEFVAL(0x7FFFFFFF), DEFVAL(true), DEFVAL(false));
  291. ClassDB::bind_method(D_METHOD("intersect_shape", "shape", "max_results"), &PhysicsDirectSpaceState3D::_intersect_shape, DEFVAL(32));
  292. ClassDB::bind_method(D_METHOD("cast_motion", "shape", "motion"), &PhysicsDirectSpaceState3D::_cast_motion);
  293. ClassDB::bind_method(D_METHOD("collide_shape", "shape", "max_results"), &PhysicsDirectSpaceState3D::_collide_shape, DEFVAL(32));
  294. ClassDB::bind_method(D_METHOD("get_rest_info", "shape"), &PhysicsDirectSpaceState3D::_get_rest_info);
  295. }
  296. int PhysicsShapeQueryResult3D::get_result_count() const {
  297. return result.size();
  298. }
  299. RID PhysicsShapeQueryResult3D::get_result_rid(int p_idx) const {
  300. return result[p_idx].rid;
  301. }
  302. ObjectID PhysicsShapeQueryResult3D::get_result_object_id(int p_idx) const {
  303. return result[p_idx].collider_id;
  304. }
  305. Object *PhysicsShapeQueryResult3D::get_result_object(int p_idx) const {
  306. return result[p_idx].collider;
  307. }
  308. int PhysicsShapeQueryResult3D::get_result_object_shape(int p_idx) const {
  309. return result[p_idx].shape;
  310. }
  311. PhysicsShapeQueryResult3D::PhysicsShapeQueryResult3D() {
  312. }
  313. void PhysicsShapeQueryResult3D::_bind_methods() {
  314. ClassDB::bind_method(D_METHOD("get_result_count"), &PhysicsShapeQueryResult3D::get_result_count);
  315. ClassDB::bind_method(D_METHOD("get_result_rid", "idx"), &PhysicsShapeQueryResult3D::get_result_rid);
  316. ClassDB::bind_method(D_METHOD("get_result_object_id", "idx"), &PhysicsShapeQueryResult3D::get_result_object_id);
  317. ClassDB::bind_method(D_METHOD("get_result_object", "idx"), &PhysicsShapeQueryResult3D::get_result_object);
  318. ClassDB::bind_method(D_METHOD("get_result_object_shape", "idx"), &PhysicsShapeQueryResult3D::get_result_object_shape);
  319. }
  320. ///////////////////////////////////////
  321. RID PhysicsServer3D::shape_create(ShapeType p_shape) {
  322. switch (p_shape) {
  323. case SHAPE_PLANE:
  324. return plane_shape_create();
  325. case SHAPE_RAY:
  326. return ray_shape_create();
  327. case SHAPE_SPHERE:
  328. return sphere_shape_create();
  329. case SHAPE_BOX:
  330. return box_shape_create();
  331. case SHAPE_CAPSULE:
  332. return capsule_shape_create();
  333. case SHAPE_CYLINDER:
  334. return cylinder_shape_create();
  335. case SHAPE_CONVEX_POLYGON:
  336. return convex_polygon_shape_create();
  337. case SHAPE_CONCAVE_POLYGON:
  338. return concave_polygon_shape_create();
  339. case SHAPE_HEIGHTMAP:
  340. return heightmap_shape_create();
  341. case SHAPE_CUSTOM:
  342. return custom_shape_create();
  343. default:
  344. return RID();
  345. }
  346. }
  347. void PhysicsServer3D::_bind_methods() {
  348. #ifndef _3D_DISABLED
  349. ClassDB::bind_method(D_METHOD("plane_shape_create"), &PhysicsServer3D::plane_shape_create);
  350. ClassDB::bind_method(D_METHOD("ray_shape_create"), &PhysicsServer3D::ray_shape_create);
  351. ClassDB::bind_method(D_METHOD("sphere_shape_create"), &PhysicsServer3D::sphere_shape_create);
  352. ClassDB::bind_method(D_METHOD("box_shape_create"), &PhysicsServer3D::box_shape_create);
  353. ClassDB::bind_method(D_METHOD("capsule_shape_create"), &PhysicsServer3D::capsule_shape_create);
  354. ClassDB::bind_method(D_METHOD("cylinder_shape_create"), &PhysicsServer3D::cylinder_shape_create);
  355. ClassDB::bind_method(D_METHOD("convex_polygon_shape_create"), &PhysicsServer3D::convex_polygon_shape_create);
  356. ClassDB::bind_method(D_METHOD("concave_polygon_shape_create"), &PhysicsServer3D::concave_polygon_shape_create);
  357. ClassDB::bind_method(D_METHOD("heightmap_shape_create"), &PhysicsServer3D::heightmap_shape_create);
  358. ClassDB::bind_method(D_METHOD("custom_shape_create"), &PhysicsServer3D::custom_shape_create);
  359. ClassDB::bind_method(D_METHOD("shape_set_data", "shape", "data"), &PhysicsServer3D::shape_set_data);
  360. ClassDB::bind_method(D_METHOD("shape_get_type", "shape"), &PhysicsServer3D::shape_get_type);
  361. ClassDB::bind_method(D_METHOD("shape_get_data", "shape"), &PhysicsServer3D::shape_get_data);
  362. ClassDB::bind_method(D_METHOD("space_create"), &PhysicsServer3D::space_create);
  363. ClassDB::bind_method(D_METHOD("space_set_active", "space", "active"), &PhysicsServer3D::space_set_active);
  364. ClassDB::bind_method(D_METHOD("space_is_active", "space"), &PhysicsServer3D::space_is_active);
  365. ClassDB::bind_method(D_METHOD("space_set_param", "space", "param", "value"), &PhysicsServer3D::space_set_param);
  366. ClassDB::bind_method(D_METHOD("space_get_param", "space", "param"), &PhysicsServer3D::space_get_param);
  367. ClassDB::bind_method(D_METHOD("space_get_direct_state", "space"), &PhysicsServer3D::space_get_direct_state);
  368. ClassDB::bind_method(D_METHOD("area_create"), &PhysicsServer3D::area_create);
  369. ClassDB::bind_method(D_METHOD("area_set_space", "area", "space"), &PhysicsServer3D::area_set_space);
  370. ClassDB::bind_method(D_METHOD("area_get_space", "area"), &PhysicsServer3D::area_get_space);
  371. ClassDB::bind_method(D_METHOD("area_set_space_override_mode", "area", "mode"), &PhysicsServer3D::area_set_space_override_mode);
  372. ClassDB::bind_method(D_METHOD("area_get_space_override_mode", "area"), &PhysicsServer3D::area_get_space_override_mode);
  373. ClassDB::bind_method(D_METHOD("area_add_shape", "area", "shape", "transform", "disabled"), &PhysicsServer3D::area_add_shape, DEFVAL(Transform()), DEFVAL(false));
  374. ClassDB::bind_method(D_METHOD("area_set_shape", "area", "shape_idx", "shape"), &PhysicsServer3D::area_set_shape);
  375. ClassDB::bind_method(D_METHOD("area_set_shape_transform", "area", "shape_idx", "transform"), &PhysicsServer3D::area_set_shape_transform);
  376. ClassDB::bind_method(D_METHOD("area_set_shape_disabled", "area", "shape_idx", "disabled"), &PhysicsServer3D::area_set_shape_disabled);
  377. ClassDB::bind_method(D_METHOD("area_get_shape_count", "area"), &PhysicsServer3D::area_get_shape_count);
  378. ClassDB::bind_method(D_METHOD("area_get_shape", "area", "shape_idx"), &PhysicsServer3D::area_get_shape);
  379. ClassDB::bind_method(D_METHOD("area_get_shape_transform", "area", "shape_idx"), &PhysicsServer3D::area_get_shape_transform);
  380. ClassDB::bind_method(D_METHOD("area_remove_shape", "area", "shape_idx"), &PhysicsServer3D::area_remove_shape);
  381. ClassDB::bind_method(D_METHOD("area_clear_shapes", "area"), &PhysicsServer3D::area_clear_shapes);
  382. ClassDB::bind_method(D_METHOD("area_set_collision_layer", "area", "layer"), &PhysicsServer3D::area_set_collision_layer);
  383. ClassDB::bind_method(D_METHOD("area_set_collision_mask", "area", "mask"), &PhysicsServer3D::area_set_collision_mask);
  384. ClassDB::bind_method(D_METHOD("area_set_param", "area", "param", "value"), &PhysicsServer3D::area_set_param);
  385. ClassDB::bind_method(D_METHOD("area_set_transform", "area", "transform"), &PhysicsServer3D::area_set_transform);
  386. ClassDB::bind_method(D_METHOD("area_get_param", "area", "param"), &PhysicsServer3D::area_get_param);
  387. ClassDB::bind_method(D_METHOD("area_get_transform", "area"), &PhysicsServer3D::area_get_transform);
  388. ClassDB::bind_method(D_METHOD("area_attach_object_instance_id", "area", "id"), &PhysicsServer3D::area_attach_object_instance_id);
  389. ClassDB::bind_method(D_METHOD("area_get_object_instance_id", "area"), &PhysicsServer3D::area_get_object_instance_id);
  390. ClassDB::bind_method(D_METHOD("area_set_monitor_callback", "area", "receiver", "method"), &PhysicsServer3D::area_set_monitor_callback);
  391. ClassDB::bind_method(D_METHOD("area_set_area_monitor_callback", "area", "receiver", "method"), &PhysicsServer3D::area_set_area_monitor_callback);
  392. ClassDB::bind_method(D_METHOD("area_set_monitorable", "area", "monitorable"), &PhysicsServer3D::area_set_monitorable);
  393. ClassDB::bind_method(D_METHOD("area_set_ray_pickable", "area", "enable"), &PhysicsServer3D::area_set_ray_pickable);
  394. ClassDB::bind_method(D_METHOD("body_create"), &PhysicsServer3D::body_create);
  395. ClassDB::bind_method(D_METHOD("body_set_space", "body", "space"), &PhysicsServer3D::body_set_space);
  396. ClassDB::bind_method(D_METHOD("body_get_space", "body"), &PhysicsServer3D::body_get_space);
  397. ClassDB::bind_method(D_METHOD("body_set_mode", "body", "mode"), &PhysicsServer3D::body_set_mode);
  398. ClassDB::bind_method(D_METHOD("body_get_mode", "body"), &PhysicsServer3D::body_get_mode);
  399. ClassDB::bind_method(D_METHOD("body_set_collision_layer", "body", "layer"), &PhysicsServer3D::body_set_collision_layer);
  400. ClassDB::bind_method(D_METHOD("body_get_collision_layer", "body"), &PhysicsServer3D::body_get_collision_layer);
  401. ClassDB::bind_method(D_METHOD("body_set_collision_mask", "body", "mask"), &PhysicsServer3D::body_set_collision_mask);
  402. ClassDB::bind_method(D_METHOD("body_get_collision_mask", "body"), &PhysicsServer3D::body_get_collision_mask);
  403. ClassDB::bind_method(D_METHOD("body_add_shape", "body", "shape", "transform", "disabled"), &PhysicsServer3D::body_add_shape, DEFVAL(Transform()), DEFVAL(false));
  404. ClassDB::bind_method(D_METHOD("body_set_shape", "body", "shape_idx", "shape"), &PhysicsServer3D::body_set_shape);
  405. ClassDB::bind_method(D_METHOD("body_set_shape_transform", "body", "shape_idx", "transform"), &PhysicsServer3D::body_set_shape_transform);
  406. ClassDB::bind_method(D_METHOD("body_set_shape_disabled", "body", "shape_idx", "disabled"), &PhysicsServer3D::body_set_shape_disabled);
  407. ClassDB::bind_method(D_METHOD("body_get_shape_count", "body"), &PhysicsServer3D::body_get_shape_count);
  408. ClassDB::bind_method(D_METHOD("body_get_shape", "body", "shape_idx"), &PhysicsServer3D::body_get_shape);
  409. ClassDB::bind_method(D_METHOD("body_get_shape_transform", "body", "shape_idx"), &PhysicsServer3D::body_get_shape_transform);
  410. ClassDB::bind_method(D_METHOD("body_remove_shape", "body", "shape_idx"), &PhysicsServer3D::body_remove_shape);
  411. ClassDB::bind_method(D_METHOD("body_clear_shapes", "body"), &PhysicsServer3D::body_clear_shapes);
  412. ClassDB::bind_method(D_METHOD("body_attach_object_instance_id", "body", "id"), &PhysicsServer3D::body_attach_object_instance_id);
  413. ClassDB::bind_method(D_METHOD("body_get_object_instance_id", "body"), &PhysicsServer3D::body_get_object_instance_id);
  414. ClassDB::bind_method(D_METHOD("body_set_enable_continuous_collision_detection", "body", "enable"), &PhysicsServer3D::body_set_enable_continuous_collision_detection);
  415. ClassDB::bind_method(D_METHOD("body_is_continuous_collision_detection_enabled", "body"), &PhysicsServer3D::body_is_continuous_collision_detection_enabled);
  416. ClassDB::bind_method(D_METHOD("body_set_param", "body", "param", "value"), &PhysicsServer3D::body_set_param);
  417. ClassDB::bind_method(D_METHOD("body_get_param", "body", "param"), &PhysicsServer3D::body_get_param);
  418. ClassDB::bind_method(D_METHOD("body_set_kinematic_safe_margin", "body", "margin"), &PhysicsServer3D::body_set_kinematic_safe_margin);
  419. ClassDB::bind_method(D_METHOD("body_get_kinematic_safe_margin", "body"), &PhysicsServer3D::body_get_kinematic_safe_margin);
  420. ClassDB::bind_method(D_METHOD("body_set_state", "body", "state", "value"), &PhysicsServer3D::body_set_state);
  421. ClassDB::bind_method(D_METHOD("body_get_state", "body", "state"), &PhysicsServer3D::body_get_state);
  422. ClassDB::bind_method(D_METHOD("body_add_central_force", "body", "force"), &PhysicsServer3D::body_add_central_force);
  423. ClassDB::bind_method(D_METHOD("body_add_force", "body", "force", "position"), &PhysicsServer3D::body_add_force, Vector3());
  424. ClassDB::bind_method(D_METHOD("body_add_torque", "body", "torque"), &PhysicsServer3D::body_add_torque);
  425. ClassDB::bind_method(D_METHOD("body_apply_central_impulse", "body", "impulse"), &PhysicsServer3D::body_apply_central_impulse);
  426. ClassDB::bind_method(D_METHOD("body_apply_impulse", "body", "impulse", "position"), &PhysicsServer3D::body_apply_impulse, Vector3());
  427. ClassDB::bind_method(D_METHOD("body_apply_torque_impulse", "body", "impulse"), &PhysicsServer3D::body_apply_torque_impulse);
  428. ClassDB::bind_method(D_METHOD("body_set_axis_velocity", "body", "axis_velocity"), &PhysicsServer3D::body_set_axis_velocity);
  429. ClassDB::bind_method(D_METHOD("body_set_axis_lock", "body", "axis", "lock"), &PhysicsServer3D::body_set_axis_lock);
  430. ClassDB::bind_method(D_METHOD("body_is_axis_locked", "body", "axis"), &PhysicsServer3D::body_is_axis_locked);
  431. ClassDB::bind_method(D_METHOD("body_add_collision_exception", "body", "excepted_body"), &PhysicsServer3D::body_add_collision_exception);
  432. ClassDB::bind_method(D_METHOD("body_remove_collision_exception", "body", "excepted_body"), &PhysicsServer3D::body_remove_collision_exception);
  433. ClassDB::bind_method(D_METHOD("body_set_max_contacts_reported", "body", "amount"), &PhysicsServer3D::body_set_max_contacts_reported);
  434. ClassDB::bind_method(D_METHOD("body_get_max_contacts_reported", "body"), &PhysicsServer3D::body_get_max_contacts_reported);
  435. ClassDB::bind_method(D_METHOD("body_set_omit_force_integration", "body", "enable"), &PhysicsServer3D::body_set_omit_force_integration);
  436. ClassDB::bind_method(D_METHOD("body_is_omitting_force_integration", "body"), &PhysicsServer3D::body_is_omitting_force_integration);
  437. ClassDB::bind_method(D_METHOD("body_set_force_integration_callback", "body", "receiver", "method", "userdata"), &PhysicsServer3D::body_set_force_integration_callback, DEFVAL(Variant()));
  438. ClassDB::bind_method(D_METHOD("body_set_ray_pickable", "body", "enable"), &PhysicsServer3D::body_set_ray_pickable);
  439. ClassDB::bind_method(D_METHOD("body_get_direct_state", "body"), &PhysicsServer3D::body_get_direct_state);
  440. /* JOINT API */
  441. ClassDB::bind_method(D_METHOD("joint_create"), &PhysicsServer3D::joint_create);
  442. ClassDB::bind_method(D_METHOD("joint_clear", "joint"), &PhysicsServer3D::joint_clear);
  443. BIND_ENUM_CONSTANT(JOINT_TYPE_PIN);
  444. BIND_ENUM_CONSTANT(JOINT_TYPE_HINGE);
  445. BIND_ENUM_CONSTANT(JOINT_TYPE_SLIDER);
  446. BIND_ENUM_CONSTANT(JOINT_TYPE_CONE_TWIST);
  447. BIND_ENUM_CONSTANT(JOINT_TYPE_6DOF);
  448. BIND_ENUM_CONSTANT(JOINT_TYPE_MAX);
  449. ClassDB::bind_method(D_METHOD("joint_make_pin", "joint", "body_A", "local_A", "body_B", "local_B"), &PhysicsServer3D::joint_make_pin);
  450. ClassDB::bind_method(D_METHOD("pin_joint_set_param", "joint", "param", "value"), &PhysicsServer3D::pin_joint_set_param);
  451. ClassDB::bind_method(D_METHOD("pin_joint_get_param", "joint", "param"), &PhysicsServer3D::pin_joint_get_param);
  452. ClassDB::bind_method(D_METHOD("pin_joint_set_local_a", "joint", "local_A"), &PhysicsServer3D::pin_joint_set_local_a);
  453. ClassDB::bind_method(D_METHOD("pin_joint_get_local_a", "joint"), &PhysicsServer3D::pin_joint_get_local_a);
  454. ClassDB::bind_method(D_METHOD("pin_joint_set_local_b", "joint", "local_B"), &PhysicsServer3D::pin_joint_set_local_b);
  455. ClassDB::bind_method(D_METHOD("pin_joint_get_local_b", "joint"), &PhysicsServer3D::pin_joint_get_local_b);
  456. BIND_ENUM_CONSTANT(PIN_JOINT_BIAS);
  457. BIND_ENUM_CONSTANT(PIN_JOINT_DAMPING);
  458. BIND_ENUM_CONSTANT(PIN_JOINT_IMPULSE_CLAMP);
  459. BIND_ENUM_CONSTANT(HINGE_JOINT_BIAS);
  460. BIND_ENUM_CONSTANT(HINGE_JOINT_LIMIT_UPPER);
  461. BIND_ENUM_CONSTANT(HINGE_JOINT_LIMIT_LOWER);
  462. BIND_ENUM_CONSTANT(HINGE_JOINT_LIMIT_BIAS);
  463. BIND_ENUM_CONSTANT(HINGE_JOINT_LIMIT_SOFTNESS);
  464. BIND_ENUM_CONSTANT(HINGE_JOINT_LIMIT_RELAXATION);
  465. BIND_ENUM_CONSTANT(HINGE_JOINT_MOTOR_TARGET_VELOCITY);
  466. BIND_ENUM_CONSTANT(HINGE_JOINT_MOTOR_MAX_IMPULSE);
  467. BIND_ENUM_CONSTANT(HINGE_JOINT_FLAG_USE_LIMIT);
  468. BIND_ENUM_CONSTANT(HINGE_JOINT_FLAG_ENABLE_MOTOR);
  469. ClassDB::bind_method(D_METHOD("joint_make_hinge", "joint", "body_A", "hinge_A", "body_B", "hinge_B"), &PhysicsServer3D::joint_make_hinge);
  470. ClassDB::bind_method(D_METHOD("hinge_joint_set_param", "joint", "param", "value"), &PhysicsServer3D::hinge_joint_set_param);
  471. ClassDB::bind_method(D_METHOD("hinge_joint_get_param", "joint", "param"), &PhysicsServer3D::hinge_joint_get_param);
  472. ClassDB::bind_method(D_METHOD("hinge_joint_set_flag", "joint", "flag", "enabled"), &PhysicsServer3D::hinge_joint_set_flag);
  473. ClassDB::bind_method(D_METHOD("hinge_joint_get_flag", "joint", "flag"), &PhysicsServer3D::hinge_joint_get_flag);
  474. ClassDB::bind_method(D_METHOD("joint_make_slider", "joint", "body_A", "local_ref_A", "body_B", "local_ref_B"), &PhysicsServer3D::joint_make_slider);
  475. ClassDB::bind_method(D_METHOD("slider_joint_set_param", "joint", "param", "value"), &PhysicsServer3D::slider_joint_set_param);
  476. ClassDB::bind_method(D_METHOD("slider_joint_get_param", "joint", "param"), &PhysicsServer3D::slider_joint_get_param);
  477. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_LIMIT_UPPER);
  478. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_LIMIT_LOWER);
  479. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS);
  480. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION);
  481. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_LIMIT_DAMPING);
  482. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_MOTION_SOFTNESS);
  483. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_MOTION_RESTITUTION);
  484. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_MOTION_DAMPING);
  485. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_ORTHOGONAL_SOFTNESS);
  486. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_ORTHOGONAL_RESTITUTION);
  487. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_ORTHOGONAL_DAMPING);
  488. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_LIMIT_UPPER);
  489. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_LIMIT_LOWER);
  490. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS);
  491. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_LIMIT_RESTITUTION);
  492. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_LIMIT_DAMPING);
  493. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_MOTION_SOFTNESS);
  494. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_MOTION_RESTITUTION);
  495. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_MOTION_DAMPING);
  496. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_ORTHOGONAL_SOFTNESS);
  497. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_ORTHOGONAL_RESTITUTION);
  498. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_ORTHOGONAL_DAMPING);
  499. BIND_ENUM_CONSTANT(SLIDER_JOINT_MAX);
  500. 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);
  501. ClassDB::bind_method(D_METHOD("cone_twist_joint_set_param", "joint", "param", "value"), &PhysicsServer3D::cone_twist_joint_set_param);
  502. ClassDB::bind_method(D_METHOD("cone_twist_joint_get_param", "joint", "param"), &PhysicsServer3D::cone_twist_joint_get_param);
  503. BIND_ENUM_CONSTANT(CONE_TWIST_JOINT_SWING_SPAN);
  504. BIND_ENUM_CONSTANT(CONE_TWIST_JOINT_TWIST_SPAN);
  505. BIND_ENUM_CONSTANT(CONE_TWIST_JOINT_BIAS);
  506. BIND_ENUM_CONSTANT(CONE_TWIST_JOINT_SOFTNESS);
  507. BIND_ENUM_CONSTANT(CONE_TWIST_JOINT_RELAXATION);
  508. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_LOWER_LIMIT);
  509. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_UPPER_LIMIT);
  510. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS);
  511. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_RESTITUTION);
  512. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_DAMPING);
  513. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_MOTOR_TARGET_VELOCITY);
  514. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_MOTOR_FORCE_LIMIT);
  515. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_LOWER_LIMIT);
  516. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_UPPER_LIMIT);
  517. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS);
  518. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_DAMPING);
  519. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_RESTITUTION);
  520. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_FORCE_LIMIT);
  521. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_ERP);
  522. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_MOTOR_TARGET_VELOCITY);
  523. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_MOTOR_FORCE_LIMIT);
  524. BIND_ENUM_CONSTANT(G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT);
  525. BIND_ENUM_CONSTANT(G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT);
  526. BIND_ENUM_CONSTANT(G6DOF_JOINT_FLAG_ENABLE_MOTOR);
  527. BIND_ENUM_CONSTANT(G6DOF_JOINT_FLAG_ENABLE_LINEAR_MOTOR);
  528. ClassDB::bind_method(D_METHOD("joint_get_type", "joint"), &PhysicsServer3D::joint_get_type);
  529. ClassDB::bind_method(D_METHOD("joint_set_solver_priority", "joint", "priority"), &PhysicsServer3D::joint_set_solver_priority);
  530. ClassDB::bind_method(D_METHOD("joint_get_solver_priority", "joint"), &PhysicsServer3D::joint_get_solver_priority);
  531. 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);
  532. ClassDB::bind_method(D_METHOD("generic_6dof_joint_set_param", "joint", "axis", "param", "value"), &PhysicsServer3D::generic_6dof_joint_set_param);
  533. ClassDB::bind_method(D_METHOD("generic_6dof_joint_get_param", "joint", "axis", "param"), &PhysicsServer3D::generic_6dof_joint_get_param);
  534. ClassDB::bind_method(D_METHOD("generic_6dof_joint_set_flag", "joint", "axis", "flag", "enable"), &PhysicsServer3D::generic_6dof_joint_set_flag);
  535. ClassDB::bind_method(D_METHOD("generic_6dof_joint_get_flag", "joint", "axis", "flag"), &PhysicsServer3D::generic_6dof_joint_get_flag);
  536. ClassDB::bind_method(D_METHOD("free_rid", "rid"), &PhysicsServer3D::free);
  537. ClassDB::bind_method(D_METHOD("set_active", "active"), &PhysicsServer3D::set_active);
  538. ClassDB::bind_method(D_METHOD("get_process_info", "process_info"), &PhysicsServer3D::get_process_info);
  539. BIND_ENUM_CONSTANT(SHAPE_PLANE);
  540. BIND_ENUM_CONSTANT(SHAPE_RAY);
  541. BIND_ENUM_CONSTANT(SHAPE_SPHERE);
  542. BIND_ENUM_CONSTANT(SHAPE_BOX);
  543. BIND_ENUM_CONSTANT(SHAPE_CAPSULE);
  544. BIND_ENUM_CONSTANT(SHAPE_CYLINDER);
  545. BIND_ENUM_CONSTANT(SHAPE_CONVEX_POLYGON);
  546. BIND_ENUM_CONSTANT(SHAPE_CONCAVE_POLYGON);
  547. BIND_ENUM_CONSTANT(SHAPE_HEIGHTMAP);
  548. BIND_ENUM_CONSTANT(SHAPE_CUSTOM);
  549. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY);
  550. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_VECTOR);
  551. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_IS_POINT);
  552. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_DISTANCE_SCALE);
  553. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_POINT_ATTENUATION);
  554. BIND_ENUM_CONSTANT(AREA_PARAM_LINEAR_DAMP);
  555. BIND_ENUM_CONSTANT(AREA_PARAM_ANGULAR_DAMP);
  556. BIND_ENUM_CONSTANT(AREA_PARAM_PRIORITY);
  557. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_DISABLED);
  558. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_COMBINE);
  559. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_COMBINE_REPLACE);
  560. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_REPLACE);
  561. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_REPLACE_COMBINE);
  562. BIND_ENUM_CONSTANT(BODY_MODE_STATIC);
  563. BIND_ENUM_CONSTANT(BODY_MODE_KINEMATIC);
  564. BIND_ENUM_CONSTANT(BODY_MODE_RIGID);
  565. BIND_ENUM_CONSTANT(BODY_MODE_CHARACTER);
  566. BIND_ENUM_CONSTANT(BODY_PARAM_BOUNCE);
  567. BIND_ENUM_CONSTANT(BODY_PARAM_FRICTION);
  568. BIND_ENUM_CONSTANT(BODY_PARAM_MASS);
  569. BIND_ENUM_CONSTANT(BODY_PARAM_GRAVITY_SCALE);
  570. BIND_ENUM_CONSTANT(BODY_PARAM_LINEAR_DAMP);
  571. BIND_ENUM_CONSTANT(BODY_PARAM_ANGULAR_DAMP);
  572. BIND_ENUM_CONSTANT(BODY_PARAM_MAX);
  573. BIND_ENUM_CONSTANT(BODY_STATE_TRANSFORM);
  574. BIND_ENUM_CONSTANT(BODY_STATE_LINEAR_VELOCITY);
  575. BIND_ENUM_CONSTANT(BODY_STATE_ANGULAR_VELOCITY);
  576. BIND_ENUM_CONSTANT(BODY_STATE_SLEEPING);
  577. BIND_ENUM_CONSTANT(BODY_STATE_CAN_SLEEP);
  578. BIND_ENUM_CONSTANT(AREA_BODY_ADDED);
  579. BIND_ENUM_CONSTANT(AREA_BODY_REMOVED);
  580. BIND_ENUM_CONSTANT(INFO_ACTIVE_OBJECTS);
  581. BIND_ENUM_CONSTANT(INFO_COLLISION_PAIRS);
  582. BIND_ENUM_CONSTANT(INFO_ISLAND_COUNT);
  583. BIND_ENUM_CONSTANT(SPACE_PARAM_CONTACT_RECYCLE_RADIUS);
  584. BIND_ENUM_CONSTANT(SPACE_PARAM_CONTACT_MAX_SEPARATION);
  585. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION);
  586. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD);
  587. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD);
  588. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_TIME_TO_SLEEP);
  589. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO);
  590. BIND_ENUM_CONSTANT(SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS);
  591. BIND_ENUM_CONSTANT(SPACE_PARAM_TEST_MOTION_MIN_CONTACT_DEPTH);
  592. BIND_ENUM_CONSTANT(BODY_AXIS_LINEAR_X);
  593. BIND_ENUM_CONSTANT(BODY_AXIS_LINEAR_Y);
  594. BIND_ENUM_CONSTANT(BODY_AXIS_LINEAR_Z);
  595. BIND_ENUM_CONSTANT(BODY_AXIS_ANGULAR_X);
  596. BIND_ENUM_CONSTANT(BODY_AXIS_ANGULAR_Y);
  597. BIND_ENUM_CONSTANT(BODY_AXIS_ANGULAR_Z);
  598. #endif
  599. }
  600. PhysicsServer3D::PhysicsServer3D() {
  601. singleton = this;
  602. }
  603. PhysicsServer3D::~PhysicsServer3D() {
  604. singleton = nullptr;
  605. }
  606. Vector<PhysicsServer3DManager::ClassInfo> PhysicsServer3DManager::physics_servers;
  607. int PhysicsServer3DManager::default_server_id = -1;
  608. int PhysicsServer3DManager::default_server_priority = -1;
  609. const String PhysicsServer3DManager::setting_property_name("physics/3d/physics_engine");
  610. void PhysicsServer3DManager::on_servers_changed() {
  611. String physics_servers2("DEFAULT");
  612. for (int i = get_servers_count() - 1; 0 <= i; --i) {
  613. physics_servers2 += "," + get_server_name(i);
  614. }
  615. ProjectSettings::get_singleton()->set_custom_property_info(setting_property_name, PropertyInfo(Variant::STRING, setting_property_name, PROPERTY_HINT_ENUM, physics_servers2));
  616. }
  617. void PhysicsServer3DManager::register_server(const String &p_name, CreatePhysicsServer3DCallback p_creat_callback) {
  618. ERR_FAIL_COND(!p_creat_callback);
  619. ERR_FAIL_COND(find_server_id(p_name) != -1);
  620. physics_servers.push_back(ClassInfo(p_name, p_creat_callback));
  621. on_servers_changed();
  622. }
  623. void PhysicsServer3DManager::set_default_server(const String &p_name, int p_priority) {
  624. const int id = find_server_id(p_name);
  625. ERR_FAIL_COND(id == -1); // Not found
  626. if (default_server_priority < p_priority) {
  627. default_server_id = id;
  628. default_server_priority = p_priority;
  629. }
  630. }
  631. int PhysicsServer3DManager::find_server_id(const String &p_name) {
  632. for (int i = physics_servers.size() - 1; 0 <= i; --i) {
  633. if (p_name == physics_servers[i].name) {
  634. return i;
  635. }
  636. }
  637. return -1;
  638. }
  639. int PhysicsServer3DManager::get_servers_count() {
  640. return physics_servers.size();
  641. }
  642. String PhysicsServer3DManager::get_server_name(int p_id) {
  643. ERR_FAIL_INDEX_V(p_id, get_servers_count(), "");
  644. return physics_servers[p_id].name;
  645. }
  646. PhysicsServer3D *PhysicsServer3DManager::new_default_server() {
  647. ERR_FAIL_COND_V(default_server_id == -1, nullptr);
  648. return physics_servers[default_server_id].create_callback();
  649. }
  650. PhysicsServer3D *PhysicsServer3DManager::new_server(const String &p_name) {
  651. int id = find_server_id(p_name);
  652. if (id == -1) {
  653. return nullptr;
  654. } else {
  655. return physics_servers[id].create_callback();
  656. }
  657. }