physics_server_3d.cpp 45 KB

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