physics_server_3d.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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::TRANSFORM3D, "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 Transform3D &p_transform) {
  129. transform = p_transform;
  130. }
  131. Transform3D 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(uint32_t p_collision_mask) {
  141. collision_mask = p_collision_mask;
  142. }
  143. uint32_t 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::TRANSFORM3D, "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. Vector3 PhysicsTestMotionResult3D::get_motion() const {
  322. return result.motion;
  323. }
  324. Vector3 PhysicsTestMotionResult3D::get_motion_remainder() const {
  325. return result.remainder;
  326. }
  327. Vector3 PhysicsTestMotionResult3D::get_collision_point() const {
  328. return result.collision_point;
  329. }
  330. Vector3 PhysicsTestMotionResult3D::get_collision_normal() const {
  331. return result.collision_normal;
  332. }
  333. Vector3 PhysicsTestMotionResult3D::get_collider_velocity() const {
  334. return result.collider_velocity;
  335. }
  336. ObjectID PhysicsTestMotionResult3D::get_collider_id() const {
  337. return result.collider_id;
  338. }
  339. RID PhysicsTestMotionResult3D::get_collider_rid() const {
  340. return result.collider;
  341. }
  342. Object *PhysicsTestMotionResult3D::get_collider() const {
  343. return ObjectDB::get_instance(result.collider_id);
  344. }
  345. int PhysicsTestMotionResult3D::get_collider_shape() const {
  346. return result.collider_shape;
  347. }
  348. real_t PhysicsTestMotionResult3D::get_collision_depth() const {
  349. return result.collision_depth;
  350. }
  351. real_t PhysicsTestMotionResult3D::get_collision_safe_fraction() const {
  352. return result.collision_safe_fraction;
  353. }
  354. real_t PhysicsTestMotionResult3D::get_collision_unsafe_fraction() const {
  355. return result.collision_unsafe_fraction;
  356. }
  357. void PhysicsTestMotionResult3D::_bind_methods() {
  358. ClassDB::bind_method(D_METHOD("get_motion"), &PhysicsTestMotionResult3D::get_motion);
  359. ClassDB::bind_method(D_METHOD("get_motion_remainder"), &PhysicsTestMotionResult3D::get_motion_remainder);
  360. ClassDB::bind_method(D_METHOD("get_collision_point"), &PhysicsTestMotionResult3D::get_collision_point);
  361. ClassDB::bind_method(D_METHOD("get_collision_normal"), &PhysicsTestMotionResult3D::get_collision_normal);
  362. ClassDB::bind_method(D_METHOD("get_collider_velocity"), &PhysicsTestMotionResult3D::get_collider_velocity);
  363. ClassDB::bind_method(D_METHOD("get_collider_id"), &PhysicsTestMotionResult3D::get_collider_id);
  364. ClassDB::bind_method(D_METHOD("get_collider_rid"), &PhysicsTestMotionResult3D::get_collider_rid);
  365. ClassDB::bind_method(D_METHOD("get_collider"), &PhysicsTestMotionResult3D::get_collider);
  366. ClassDB::bind_method(D_METHOD("get_collider_shape"), &PhysicsTestMotionResult3D::get_collider_shape);
  367. ClassDB::bind_method(D_METHOD("get_collision_depth"), &PhysicsTestMotionResult3D::get_collision_depth);
  368. ClassDB::bind_method(D_METHOD("get_collision_safe_fraction"), &PhysicsTestMotionResult3D::get_collision_safe_fraction);
  369. ClassDB::bind_method(D_METHOD("get_collision_unsafe_fraction"), &PhysicsTestMotionResult3D::get_collision_unsafe_fraction);
  370. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "motion"), "", "get_motion");
  371. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "motion_remainder"), "", "get_motion_remainder");
  372. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "collision_point"), "", "get_collision_point");
  373. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "collision_normal"), "", "get_collision_normal");
  374. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "collider_velocity"), "", "get_collider_velocity");
  375. ADD_PROPERTY(PropertyInfo(Variant::INT, "collider_id", PROPERTY_HINT_OBJECT_ID), "", "get_collider_id");
  376. ADD_PROPERTY(PropertyInfo(Variant::RID, "collider_rid"), "", "get_collider_rid");
  377. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "collider"), "", "get_collider");
  378. ADD_PROPERTY(PropertyInfo(Variant::INT, "collider_shape"), "", "get_collider_shape");
  379. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_depth"), "", "get_collision_depth");
  380. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_safe_fraction"), "", "get_collision_safe_fraction");
  381. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_unsafe_fraction"), "", "get_collision_unsafe_fraction");
  382. }
  383. ///////////////////////////////////////
  384. bool PhysicsServer3D::_body_test_motion(RID p_body, const Transform3D &p_from, const Vector3 &p_motion, bool p_infinite_inertia, real_t p_margin, const Ref<PhysicsTestMotionResult3D> &p_result) {
  385. MotionResult *r = nullptr;
  386. if (p_result.is_valid()) {
  387. r = p_result->get_result_ptr();
  388. }
  389. return body_test_motion(p_body, p_from, p_motion, p_infinite_inertia, p_margin, r);
  390. }
  391. RID PhysicsServer3D::shape_create(ShapeType p_shape) {
  392. switch (p_shape) {
  393. case SHAPE_PLANE:
  394. return plane_shape_create();
  395. case SHAPE_RAY:
  396. return ray_shape_create();
  397. case SHAPE_SPHERE:
  398. return sphere_shape_create();
  399. case SHAPE_BOX:
  400. return box_shape_create();
  401. case SHAPE_CAPSULE:
  402. return capsule_shape_create();
  403. case SHAPE_CYLINDER:
  404. return cylinder_shape_create();
  405. case SHAPE_CONVEX_POLYGON:
  406. return convex_polygon_shape_create();
  407. case SHAPE_CONCAVE_POLYGON:
  408. return concave_polygon_shape_create();
  409. case SHAPE_HEIGHTMAP:
  410. return heightmap_shape_create();
  411. case SHAPE_CUSTOM:
  412. return custom_shape_create();
  413. default:
  414. return RID();
  415. }
  416. }
  417. void PhysicsServer3D::_bind_methods() {
  418. #ifndef _3D_DISABLED
  419. ClassDB::bind_method(D_METHOD("plane_shape_create"), &PhysicsServer3D::plane_shape_create);
  420. ClassDB::bind_method(D_METHOD("ray_shape_create"), &PhysicsServer3D::ray_shape_create);
  421. ClassDB::bind_method(D_METHOD("sphere_shape_create"), &PhysicsServer3D::sphere_shape_create);
  422. ClassDB::bind_method(D_METHOD("box_shape_create"), &PhysicsServer3D::box_shape_create);
  423. ClassDB::bind_method(D_METHOD("capsule_shape_create"), &PhysicsServer3D::capsule_shape_create);
  424. ClassDB::bind_method(D_METHOD("cylinder_shape_create"), &PhysicsServer3D::cylinder_shape_create);
  425. ClassDB::bind_method(D_METHOD("convex_polygon_shape_create"), &PhysicsServer3D::convex_polygon_shape_create);
  426. ClassDB::bind_method(D_METHOD("concave_polygon_shape_create"), &PhysicsServer3D::concave_polygon_shape_create);
  427. ClassDB::bind_method(D_METHOD("heightmap_shape_create"), &PhysicsServer3D::heightmap_shape_create);
  428. ClassDB::bind_method(D_METHOD("custom_shape_create"), &PhysicsServer3D::custom_shape_create);
  429. ClassDB::bind_method(D_METHOD("shape_set_data", "shape", "data"), &PhysicsServer3D::shape_set_data);
  430. ClassDB::bind_method(D_METHOD("shape_get_type", "shape"), &PhysicsServer3D::shape_get_type);
  431. ClassDB::bind_method(D_METHOD("shape_get_data", "shape"), &PhysicsServer3D::shape_get_data);
  432. ClassDB::bind_method(D_METHOD("space_create"), &PhysicsServer3D::space_create);
  433. ClassDB::bind_method(D_METHOD("space_set_active", "space", "active"), &PhysicsServer3D::space_set_active);
  434. ClassDB::bind_method(D_METHOD("space_is_active", "space"), &PhysicsServer3D::space_is_active);
  435. ClassDB::bind_method(D_METHOD("space_set_param", "space", "param", "value"), &PhysicsServer3D::space_set_param);
  436. ClassDB::bind_method(D_METHOD("space_get_param", "space", "param"), &PhysicsServer3D::space_get_param);
  437. ClassDB::bind_method(D_METHOD("space_get_direct_state", "space"), &PhysicsServer3D::space_get_direct_state);
  438. ClassDB::bind_method(D_METHOD("area_create"), &PhysicsServer3D::area_create);
  439. ClassDB::bind_method(D_METHOD("area_set_space", "area", "space"), &PhysicsServer3D::area_set_space);
  440. ClassDB::bind_method(D_METHOD("area_get_space", "area"), &PhysicsServer3D::area_get_space);
  441. ClassDB::bind_method(D_METHOD("area_set_space_override_mode", "area", "mode"), &PhysicsServer3D::area_set_space_override_mode);
  442. ClassDB::bind_method(D_METHOD("area_get_space_override_mode", "area"), &PhysicsServer3D::area_get_space_override_mode);
  443. ClassDB::bind_method(D_METHOD("area_add_shape", "area", "shape", "transform", "disabled"), &PhysicsServer3D::area_add_shape, DEFVAL(Transform3D()), DEFVAL(false));
  444. ClassDB::bind_method(D_METHOD("area_set_shape", "area", "shape_idx", "shape"), &PhysicsServer3D::area_set_shape);
  445. ClassDB::bind_method(D_METHOD("area_set_shape_transform", "area", "shape_idx", "transform"), &PhysicsServer3D::area_set_shape_transform);
  446. ClassDB::bind_method(D_METHOD("area_set_shape_disabled", "area", "shape_idx", "disabled"), &PhysicsServer3D::area_set_shape_disabled);
  447. ClassDB::bind_method(D_METHOD("area_get_shape_count", "area"), &PhysicsServer3D::area_get_shape_count);
  448. ClassDB::bind_method(D_METHOD("area_get_shape", "area", "shape_idx"), &PhysicsServer3D::area_get_shape);
  449. ClassDB::bind_method(D_METHOD("area_get_shape_transform", "area", "shape_idx"), &PhysicsServer3D::area_get_shape_transform);
  450. ClassDB::bind_method(D_METHOD("area_remove_shape", "area", "shape_idx"), &PhysicsServer3D::area_remove_shape);
  451. ClassDB::bind_method(D_METHOD("area_clear_shapes", "area"), &PhysicsServer3D::area_clear_shapes);
  452. ClassDB::bind_method(D_METHOD("area_set_collision_layer", "area", "layer"), &PhysicsServer3D::area_set_collision_layer);
  453. ClassDB::bind_method(D_METHOD("area_set_collision_mask", "area", "mask"), &PhysicsServer3D::area_set_collision_mask);
  454. ClassDB::bind_method(D_METHOD("area_set_param", "area", "param", "value"), &PhysicsServer3D::area_set_param);
  455. ClassDB::bind_method(D_METHOD("area_set_transform", "area", "transform"), &PhysicsServer3D::area_set_transform);
  456. ClassDB::bind_method(D_METHOD("area_get_param", "area", "param"), &PhysicsServer3D::area_get_param);
  457. ClassDB::bind_method(D_METHOD("area_get_transform", "area"), &PhysicsServer3D::area_get_transform);
  458. ClassDB::bind_method(D_METHOD("area_attach_object_instance_id", "area", "id"), &PhysicsServer3D::area_attach_object_instance_id);
  459. ClassDB::bind_method(D_METHOD("area_get_object_instance_id", "area"), &PhysicsServer3D::area_get_object_instance_id);
  460. ClassDB::bind_method(D_METHOD("area_set_monitor_callback", "area", "receiver", "method"), &PhysicsServer3D::area_set_monitor_callback);
  461. ClassDB::bind_method(D_METHOD("area_set_area_monitor_callback", "area", "receiver", "method"), &PhysicsServer3D::area_set_area_monitor_callback);
  462. ClassDB::bind_method(D_METHOD("area_set_monitorable", "area", "monitorable"), &PhysicsServer3D::area_set_monitorable);
  463. ClassDB::bind_method(D_METHOD("area_set_ray_pickable", "area", "enable"), &PhysicsServer3D::area_set_ray_pickable);
  464. ClassDB::bind_method(D_METHOD("body_create"), &PhysicsServer3D::body_create);
  465. ClassDB::bind_method(D_METHOD("body_set_space", "body", "space"), &PhysicsServer3D::body_set_space);
  466. ClassDB::bind_method(D_METHOD("body_get_space", "body"), &PhysicsServer3D::body_get_space);
  467. ClassDB::bind_method(D_METHOD("body_set_mode", "body", "mode"), &PhysicsServer3D::body_set_mode);
  468. ClassDB::bind_method(D_METHOD("body_get_mode", "body"), &PhysicsServer3D::body_get_mode);
  469. ClassDB::bind_method(D_METHOD("body_set_collision_layer", "body", "layer"), &PhysicsServer3D::body_set_collision_layer);
  470. ClassDB::bind_method(D_METHOD("body_get_collision_layer", "body"), &PhysicsServer3D::body_get_collision_layer);
  471. ClassDB::bind_method(D_METHOD("body_set_collision_mask", "body", "mask"), &PhysicsServer3D::body_set_collision_mask);
  472. ClassDB::bind_method(D_METHOD("body_get_collision_mask", "body"), &PhysicsServer3D::body_get_collision_mask);
  473. ClassDB::bind_method(D_METHOD("body_add_shape", "body", "shape", "transform", "disabled"), &PhysicsServer3D::body_add_shape, DEFVAL(Transform3D()), DEFVAL(false));
  474. ClassDB::bind_method(D_METHOD("body_set_shape", "body", "shape_idx", "shape"), &PhysicsServer3D::body_set_shape);
  475. ClassDB::bind_method(D_METHOD("body_set_shape_transform", "body", "shape_idx", "transform"), &PhysicsServer3D::body_set_shape_transform);
  476. ClassDB::bind_method(D_METHOD("body_set_shape_disabled", "body", "shape_idx", "disabled"), &PhysicsServer3D::body_set_shape_disabled);
  477. ClassDB::bind_method(D_METHOD("body_get_shape_count", "body"), &PhysicsServer3D::body_get_shape_count);
  478. ClassDB::bind_method(D_METHOD("body_get_shape", "body", "shape_idx"), &PhysicsServer3D::body_get_shape);
  479. ClassDB::bind_method(D_METHOD("body_get_shape_transform", "body", "shape_idx"), &PhysicsServer3D::body_get_shape_transform);
  480. ClassDB::bind_method(D_METHOD("body_remove_shape", "body", "shape_idx"), &PhysicsServer3D::body_remove_shape);
  481. ClassDB::bind_method(D_METHOD("body_clear_shapes", "body"), &PhysicsServer3D::body_clear_shapes);
  482. ClassDB::bind_method(D_METHOD("body_attach_object_instance_id", "body", "id"), &PhysicsServer3D::body_attach_object_instance_id);
  483. ClassDB::bind_method(D_METHOD("body_get_object_instance_id", "body"), &PhysicsServer3D::body_get_object_instance_id);
  484. ClassDB::bind_method(D_METHOD("body_set_enable_continuous_collision_detection", "body", "enable"), &PhysicsServer3D::body_set_enable_continuous_collision_detection);
  485. ClassDB::bind_method(D_METHOD("body_is_continuous_collision_detection_enabled", "body"), &PhysicsServer3D::body_is_continuous_collision_detection_enabled);
  486. ClassDB::bind_method(D_METHOD("body_set_param", "body", "param", "value"), &PhysicsServer3D::body_set_param);
  487. ClassDB::bind_method(D_METHOD("body_get_param", "body", "param"), &PhysicsServer3D::body_get_param);
  488. ClassDB::bind_method(D_METHOD("body_set_state", "body", "state", "value"), &PhysicsServer3D::body_set_state);
  489. ClassDB::bind_method(D_METHOD("body_get_state", "body", "state"), &PhysicsServer3D::body_get_state);
  490. ClassDB::bind_method(D_METHOD("body_add_central_force", "body", "force"), &PhysicsServer3D::body_add_central_force);
  491. ClassDB::bind_method(D_METHOD("body_add_force", "body", "force", "position"), &PhysicsServer3D::body_add_force, Vector3());
  492. ClassDB::bind_method(D_METHOD("body_add_torque", "body", "torque"), &PhysicsServer3D::body_add_torque);
  493. ClassDB::bind_method(D_METHOD("body_apply_central_impulse", "body", "impulse"), &PhysicsServer3D::body_apply_central_impulse);
  494. ClassDB::bind_method(D_METHOD("body_apply_impulse", "body", "impulse", "position"), &PhysicsServer3D::body_apply_impulse, Vector3());
  495. ClassDB::bind_method(D_METHOD("body_apply_torque_impulse", "body", "impulse"), &PhysicsServer3D::body_apply_torque_impulse);
  496. ClassDB::bind_method(D_METHOD("body_set_axis_velocity", "body", "axis_velocity"), &PhysicsServer3D::body_set_axis_velocity);
  497. ClassDB::bind_method(D_METHOD("body_set_axis_lock", "body", "axis", "lock"), &PhysicsServer3D::body_set_axis_lock);
  498. ClassDB::bind_method(D_METHOD("body_is_axis_locked", "body", "axis"), &PhysicsServer3D::body_is_axis_locked);
  499. ClassDB::bind_method(D_METHOD("body_add_collision_exception", "body", "excepted_body"), &PhysicsServer3D::body_add_collision_exception);
  500. ClassDB::bind_method(D_METHOD("body_remove_collision_exception", "body", "excepted_body"), &PhysicsServer3D::body_remove_collision_exception);
  501. ClassDB::bind_method(D_METHOD("body_set_max_contacts_reported", "body", "amount"), &PhysicsServer3D::body_set_max_contacts_reported);
  502. ClassDB::bind_method(D_METHOD("body_get_max_contacts_reported", "body"), &PhysicsServer3D::body_get_max_contacts_reported);
  503. ClassDB::bind_method(D_METHOD("body_set_omit_force_integration", "body", "enable"), &PhysicsServer3D::body_set_omit_force_integration);
  504. ClassDB::bind_method(D_METHOD("body_is_omitting_force_integration", "body"), &PhysicsServer3D::body_is_omitting_force_integration);
  505. ClassDB::bind_method(D_METHOD("body_set_force_integration_callback", "body", "callable", "userdata"), &PhysicsServer3D::body_set_force_integration_callback, DEFVAL(Variant()));
  506. ClassDB::bind_method(D_METHOD("body_set_ray_pickable", "body", "enable"), &PhysicsServer3D::body_set_ray_pickable);
  507. ClassDB::bind_method(D_METHOD("body_test_motion", "body", "from", "motion", "infinite_inertia", "margin", "result"), &PhysicsServer3D::_body_test_motion, DEFVAL(0.001), DEFVAL(Variant()));
  508. ClassDB::bind_method(D_METHOD("body_get_direct_state", "body"), &PhysicsServer3D::body_get_direct_state);
  509. /* SOFT BODY API */
  510. ClassDB::bind_method(D_METHOD("soft_body_get_bounds", "body"), &PhysicsServer3D::soft_body_get_bounds);
  511. /* JOINT API */
  512. ClassDB::bind_method(D_METHOD("joint_create"), &PhysicsServer3D::joint_create);
  513. ClassDB::bind_method(D_METHOD("joint_clear", "joint"), &PhysicsServer3D::joint_clear);
  514. BIND_ENUM_CONSTANT(JOINT_TYPE_PIN);
  515. BIND_ENUM_CONSTANT(JOINT_TYPE_HINGE);
  516. BIND_ENUM_CONSTANT(JOINT_TYPE_SLIDER);
  517. BIND_ENUM_CONSTANT(JOINT_TYPE_CONE_TWIST);
  518. BIND_ENUM_CONSTANT(JOINT_TYPE_6DOF);
  519. BIND_ENUM_CONSTANT(JOINT_TYPE_MAX);
  520. ClassDB::bind_method(D_METHOD("joint_make_pin", "joint", "body_A", "local_A", "body_B", "local_B"), &PhysicsServer3D::joint_make_pin);
  521. ClassDB::bind_method(D_METHOD("pin_joint_set_param", "joint", "param", "value"), &PhysicsServer3D::pin_joint_set_param);
  522. ClassDB::bind_method(D_METHOD("pin_joint_get_param", "joint", "param"), &PhysicsServer3D::pin_joint_get_param);
  523. ClassDB::bind_method(D_METHOD("pin_joint_set_local_a", "joint", "local_A"), &PhysicsServer3D::pin_joint_set_local_a);
  524. ClassDB::bind_method(D_METHOD("pin_joint_get_local_a", "joint"), &PhysicsServer3D::pin_joint_get_local_a);
  525. ClassDB::bind_method(D_METHOD("pin_joint_set_local_b", "joint", "local_B"), &PhysicsServer3D::pin_joint_set_local_b);
  526. ClassDB::bind_method(D_METHOD("pin_joint_get_local_b", "joint"), &PhysicsServer3D::pin_joint_get_local_b);
  527. BIND_ENUM_CONSTANT(PIN_JOINT_BIAS);
  528. BIND_ENUM_CONSTANT(PIN_JOINT_DAMPING);
  529. BIND_ENUM_CONSTANT(PIN_JOINT_IMPULSE_CLAMP);
  530. BIND_ENUM_CONSTANT(HINGE_JOINT_BIAS);
  531. BIND_ENUM_CONSTANT(HINGE_JOINT_LIMIT_UPPER);
  532. BIND_ENUM_CONSTANT(HINGE_JOINT_LIMIT_LOWER);
  533. BIND_ENUM_CONSTANT(HINGE_JOINT_LIMIT_BIAS);
  534. BIND_ENUM_CONSTANT(HINGE_JOINT_LIMIT_SOFTNESS);
  535. BIND_ENUM_CONSTANT(HINGE_JOINT_LIMIT_RELAXATION);
  536. BIND_ENUM_CONSTANT(HINGE_JOINT_MOTOR_TARGET_VELOCITY);
  537. BIND_ENUM_CONSTANT(HINGE_JOINT_MOTOR_MAX_IMPULSE);
  538. BIND_ENUM_CONSTANT(HINGE_JOINT_FLAG_USE_LIMIT);
  539. BIND_ENUM_CONSTANT(HINGE_JOINT_FLAG_ENABLE_MOTOR);
  540. ClassDB::bind_method(D_METHOD("joint_make_hinge", "joint", "body_A", "hinge_A", "body_B", "hinge_B"), &PhysicsServer3D::joint_make_hinge);
  541. ClassDB::bind_method(D_METHOD("hinge_joint_set_param", "joint", "param", "value"), &PhysicsServer3D::hinge_joint_set_param);
  542. ClassDB::bind_method(D_METHOD("hinge_joint_get_param", "joint", "param"), &PhysicsServer3D::hinge_joint_get_param);
  543. ClassDB::bind_method(D_METHOD("hinge_joint_set_flag", "joint", "flag", "enabled"), &PhysicsServer3D::hinge_joint_set_flag);
  544. ClassDB::bind_method(D_METHOD("hinge_joint_get_flag", "joint", "flag"), &PhysicsServer3D::hinge_joint_get_flag);
  545. ClassDB::bind_method(D_METHOD("joint_make_slider", "joint", "body_A", "local_ref_A", "body_B", "local_ref_B"), &PhysicsServer3D::joint_make_slider);
  546. ClassDB::bind_method(D_METHOD("slider_joint_set_param", "joint", "param", "value"), &PhysicsServer3D::slider_joint_set_param);
  547. ClassDB::bind_method(D_METHOD("slider_joint_get_param", "joint", "param"), &PhysicsServer3D::slider_joint_get_param);
  548. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_LIMIT_UPPER);
  549. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_LIMIT_LOWER);
  550. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS);
  551. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION);
  552. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_LIMIT_DAMPING);
  553. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_MOTION_SOFTNESS);
  554. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_MOTION_RESTITUTION);
  555. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_MOTION_DAMPING);
  556. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_ORTHOGONAL_SOFTNESS);
  557. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_ORTHOGONAL_RESTITUTION);
  558. BIND_ENUM_CONSTANT(SLIDER_JOINT_LINEAR_ORTHOGONAL_DAMPING);
  559. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_LIMIT_UPPER);
  560. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_LIMIT_LOWER);
  561. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS);
  562. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_LIMIT_RESTITUTION);
  563. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_LIMIT_DAMPING);
  564. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_MOTION_SOFTNESS);
  565. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_MOTION_RESTITUTION);
  566. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_MOTION_DAMPING);
  567. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_ORTHOGONAL_SOFTNESS);
  568. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_ORTHOGONAL_RESTITUTION);
  569. BIND_ENUM_CONSTANT(SLIDER_JOINT_ANGULAR_ORTHOGONAL_DAMPING);
  570. BIND_ENUM_CONSTANT(SLIDER_JOINT_MAX);
  571. 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);
  572. ClassDB::bind_method(D_METHOD("cone_twist_joint_set_param", "joint", "param", "value"), &PhysicsServer3D::cone_twist_joint_set_param);
  573. ClassDB::bind_method(D_METHOD("cone_twist_joint_get_param", "joint", "param"), &PhysicsServer3D::cone_twist_joint_get_param);
  574. BIND_ENUM_CONSTANT(CONE_TWIST_JOINT_SWING_SPAN);
  575. BIND_ENUM_CONSTANT(CONE_TWIST_JOINT_TWIST_SPAN);
  576. BIND_ENUM_CONSTANT(CONE_TWIST_JOINT_BIAS);
  577. BIND_ENUM_CONSTANT(CONE_TWIST_JOINT_SOFTNESS);
  578. BIND_ENUM_CONSTANT(CONE_TWIST_JOINT_RELAXATION);
  579. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_LOWER_LIMIT);
  580. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_UPPER_LIMIT);
  581. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS);
  582. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_RESTITUTION);
  583. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_DAMPING);
  584. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_MOTOR_TARGET_VELOCITY);
  585. BIND_ENUM_CONSTANT(G6DOF_JOINT_LINEAR_MOTOR_FORCE_LIMIT);
  586. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_LOWER_LIMIT);
  587. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_UPPER_LIMIT);
  588. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS);
  589. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_DAMPING);
  590. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_RESTITUTION);
  591. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_FORCE_LIMIT);
  592. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_ERP);
  593. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_MOTOR_TARGET_VELOCITY);
  594. BIND_ENUM_CONSTANT(G6DOF_JOINT_ANGULAR_MOTOR_FORCE_LIMIT);
  595. BIND_ENUM_CONSTANT(G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT);
  596. BIND_ENUM_CONSTANT(G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT);
  597. BIND_ENUM_CONSTANT(G6DOF_JOINT_FLAG_ENABLE_MOTOR);
  598. BIND_ENUM_CONSTANT(G6DOF_JOINT_FLAG_ENABLE_LINEAR_MOTOR);
  599. ClassDB::bind_method(D_METHOD("joint_get_type", "joint"), &PhysicsServer3D::joint_get_type);
  600. ClassDB::bind_method(D_METHOD("joint_set_solver_priority", "joint", "priority"), &PhysicsServer3D::joint_set_solver_priority);
  601. ClassDB::bind_method(D_METHOD("joint_get_solver_priority", "joint"), &PhysicsServer3D::joint_get_solver_priority);
  602. 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);
  603. ClassDB::bind_method(D_METHOD("generic_6dof_joint_set_param", "joint", "axis", "param", "value"), &PhysicsServer3D::generic_6dof_joint_set_param);
  604. ClassDB::bind_method(D_METHOD("generic_6dof_joint_get_param", "joint", "axis", "param"), &PhysicsServer3D::generic_6dof_joint_get_param);
  605. ClassDB::bind_method(D_METHOD("generic_6dof_joint_set_flag", "joint", "axis", "flag", "enable"), &PhysicsServer3D::generic_6dof_joint_set_flag);
  606. ClassDB::bind_method(D_METHOD("generic_6dof_joint_get_flag", "joint", "axis", "flag"), &PhysicsServer3D::generic_6dof_joint_get_flag);
  607. ClassDB::bind_method(D_METHOD("free_rid", "rid"), &PhysicsServer3D::free);
  608. ClassDB::bind_method(D_METHOD("set_active", "active"), &PhysicsServer3D::set_active);
  609. ClassDB::bind_method(D_METHOD("get_process_info", "process_info"), &PhysicsServer3D::get_process_info);
  610. BIND_ENUM_CONSTANT(SHAPE_PLANE);
  611. BIND_ENUM_CONSTANT(SHAPE_RAY);
  612. BIND_ENUM_CONSTANT(SHAPE_SPHERE);
  613. BIND_ENUM_CONSTANT(SHAPE_BOX);
  614. BIND_ENUM_CONSTANT(SHAPE_CAPSULE);
  615. BIND_ENUM_CONSTANT(SHAPE_CYLINDER);
  616. BIND_ENUM_CONSTANT(SHAPE_CONVEX_POLYGON);
  617. BIND_ENUM_CONSTANT(SHAPE_CONCAVE_POLYGON);
  618. BIND_ENUM_CONSTANT(SHAPE_HEIGHTMAP);
  619. BIND_ENUM_CONSTANT(SHAPE_SOFT_BODY);
  620. BIND_ENUM_CONSTANT(SHAPE_CUSTOM);
  621. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY);
  622. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_VECTOR);
  623. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_IS_POINT);
  624. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_DISTANCE_SCALE);
  625. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_POINT_ATTENUATION);
  626. BIND_ENUM_CONSTANT(AREA_PARAM_LINEAR_DAMP);
  627. BIND_ENUM_CONSTANT(AREA_PARAM_ANGULAR_DAMP);
  628. BIND_ENUM_CONSTANT(AREA_PARAM_PRIORITY);
  629. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_DISABLED);
  630. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_COMBINE);
  631. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_COMBINE_REPLACE);
  632. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_REPLACE);
  633. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_REPLACE_COMBINE);
  634. BIND_ENUM_CONSTANT(BODY_MODE_STATIC);
  635. BIND_ENUM_CONSTANT(BODY_MODE_KINEMATIC);
  636. BIND_ENUM_CONSTANT(BODY_MODE_DYNAMIC);
  637. BIND_ENUM_CONSTANT(BODY_MODE_DYNAMIC_LOCKED);
  638. BIND_ENUM_CONSTANT(BODY_PARAM_BOUNCE);
  639. BIND_ENUM_CONSTANT(BODY_PARAM_FRICTION);
  640. BIND_ENUM_CONSTANT(BODY_PARAM_MASS);
  641. BIND_ENUM_CONSTANT(BODY_PARAM_GRAVITY_SCALE);
  642. BIND_ENUM_CONSTANT(BODY_PARAM_LINEAR_DAMP);
  643. BIND_ENUM_CONSTANT(BODY_PARAM_ANGULAR_DAMP);
  644. BIND_ENUM_CONSTANT(BODY_PARAM_MAX);
  645. BIND_ENUM_CONSTANT(BODY_STATE_TRANSFORM);
  646. BIND_ENUM_CONSTANT(BODY_STATE_LINEAR_VELOCITY);
  647. BIND_ENUM_CONSTANT(BODY_STATE_ANGULAR_VELOCITY);
  648. BIND_ENUM_CONSTANT(BODY_STATE_SLEEPING);
  649. BIND_ENUM_CONSTANT(BODY_STATE_CAN_SLEEP);
  650. BIND_ENUM_CONSTANT(AREA_BODY_ADDED);
  651. BIND_ENUM_CONSTANT(AREA_BODY_REMOVED);
  652. BIND_ENUM_CONSTANT(INFO_ACTIVE_OBJECTS);
  653. BIND_ENUM_CONSTANT(INFO_COLLISION_PAIRS);
  654. BIND_ENUM_CONSTANT(INFO_ISLAND_COUNT);
  655. BIND_ENUM_CONSTANT(SPACE_PARAM_CONTACT_RECYCLE_RADIUS);
  656. BIND_ENUM_CONSTANT(SPACE_PARAM_CONTACT_MAX_SEPARATION);
  657. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION);
  658. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD);
  659. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD);
  660. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_TIME_TO_SLEEP);
  661. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO);
  662. BIND_ENUM_CONSTANT(SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS);
  663. BIND_ENUM_CONSTANT(SPACE_PARAM_TEST_MOTION_MIN_CONTACT_DEPTH);
  664. BIND_ENUM_CONSTANT(BODY_AXIS_LINEAR_X);
  665. BIND_ENUM_CONSTANT(BODY_AXIS_LINEAR_Y);
  666. BIND_ENUM_CONSTANT(BODY_AXIS_LINEAR_Z);
  667. BIND_ENUM_CONSTANT(BODY_AXIS_ANGULAR_X);
  668. BIND_ENUM_CONSTANT(BODY_AXIS_ANGULAR_Y);
  669. BIND_ENUM_CONSTANT(BODY_AXIS_ANGULAR_Z);
  670. #endif
  671. }
  672. PhysicsServer3D::PhysicsServer3D() {
  673. singleton = this;
  674. }
  675. PhysicsServer3D::~PhysicsServer3D() {
  676. singleton = nullptr;
  677. }
  678. Vector<PhysicsServer3DManager::ClassInfo> PhysicsServer3DManager::physics_servers;
  679. int PhysicsServer3DManager::default_server_id = -1;
  680. int PhysicsServer3DManager::default_server_priority = -1;
  681. const String PhysicsServer3DManager::setting_property_name("physics/3d/physics_engine");
  682. void PhysicsServer3DManager::on_servers_changed() {
  683. String physics_servers2("DEFAULT");
  684. for (int i = get_servers_count() - 1; 0 <= i; --i) {
  685. physics_servers2 += "," + get_server_name(i);
  686. }
  687. ProjectSettings::get_singleton()->set_custom_property_info(setting_property_name, PropertyInfo(Variant::STRING, setting_property_name, PROPERTY_HINT_ENUM, physics_servers2));
  688. }
  689. void PhysicsServer3DManager::register_server(const String &p_name, CreatePhysicsServer3DCallback p_creat_callback) {
  690. ERR_FAIL_COND(!p_creat_callback);
  691. ERR_FAIL_COND(find_server_id(p_name) != -1);
  692. physics_servers.push_back(ClassInfo(p_name, p_creat_callback));
  693. on_servers_changed();
  694. }
  695. void PhysicsServer3DManager::set_default_server(const String &p_name, int p_priority) {
  696. const int id = find_server_id(p_name);
  697. ERR_FAIL_COND(id == -1); // Not found
  698. if (default_server_priority < p_priority) {
  699. default_server_id = id;
  700. default_server_priority = p_priority;
  701. }
  702. }
  703. int PhysicsServer3DManager::find_server_id(const String &p_name) {
  704. for (int i = physics_servers.size() - 1; 0 <= i; --i) {
  705. if (p_name == physics_servers[i].name) {
  706. return i;
  707. }
  708. }
  709. return -1;
  710. }
  711. int PhysicsServer3DManager::get_servers_count() {
  712. return physics_servers.size();
  713. }
  714. String PhysicsServer3DManager::get_server_name(int p_id) {
  715. ERR_FAIL_INDEX_V(p_id, get_servers_count(), "");
  716. return physics_servers[p_id].name;
  717. }
  718. PhysicsServer3D *PhysicsServer3DManager::new_default_server() {
  719. ERR_FAIL_COND_V(default_server_id == -1, nullptr);
  720. return physics_servers[default_server_id].create_callback();
  721. }
  722. PhysicsServer3D *PhysicsServer3DManager::new_server(const String &p_name) {
  723. int id = find_server_id(p_name);
  724. if (id == -1) {
  725. return nullptr;
  726. } else {
  727. return physics_servers[id].create_callback();
  728. }
  729. }