godot_physics_server_3d.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  1. /*************************************************************************/
  2. /* godot_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 "godot_physics_server_3d.h"
  31. #include "godot_body_direct_state_3d.h"
  32. #include "godot_broad_phase_3d_bvh.h"
  33. #include "joints/godot_cone_twist_joint_3d.h"
  34. #include "joints/godot_generic_6dof_joint_3d.h"
  35. #include "joints/godot_hinge_joint_3d.h"
  36. #include "joints/godot_pin_joint_3d.h"
  37. #include "joints/godot_slider_joint_3d.h"
  38. #include "core/debugger/engine_debugger.h"
  39. #include "core/os/os.h"
  40. #define FLUSH_QUERY_CHECK(m_object) \
  41. ERR_FAIL_COND_MSG(m_object->get_space() && flushing_queries, "Can't change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead.");
  42. RID GodotPhysicsServer3D::world_boundary_shape_create() {
  43. GodotShape3D *shape = memnew(GodotWorldBoundaryShape3D);
  44. RID rid = shape_owner.make_rid(shape);
  45. shape->set_self(rid);
  46. return rid;
  47. }
  48. RID GodotPhysicsServer3D::separation_ray_shape_create() {
  49. GodotShape3D *shape = memnew(GodotSeparationRayShape3D);
  50. RID rid = shape_owner.make_rid(shape);
  51. shape->set_self(rid);
  52. return rid;
  53. }
  54. RID GodotPhysicsServer3D::sphere_shape_create() {
  55. GodotShape3D *shape = memnew(GodotSphereShape3D);
  56. RID rid = shape_owner.make_rid(shape);
  57. shape->set_self(rid);
  58. return rid;
  59. }
  60. RID GodotPhysicsServer3D::box_shape_create() {
  61. GodotShape3D *shape = memnew(GodotBoxShape3D);
  62. RID rid = shape_owner.make_rid(shape);
  63. shape->set_self(rid);
  64. return rid;
  65. }
  66. RID GodotPhysicsServer3D::capsule_shape_create() {
  67. GodotShape3D *shape = memnew(GodotCapsuleShape3D);
  68. RID rid = shape_owner.make_rid(shape);
  69. shape->set_self(rid);
  70. return rid;
  71. }
  72. RID GodotPhysicsServer3D::cylinder_shape_create() {
  73. GodotShape3D *shape = memnew(GodotCylinderShape3D);
  74. RID rid = shape_owner.make_rid(shape);
  75. shape->set_self(rid);
  76. return rid;
  77. }
  78. RID GodotPhysicsServer3D::convex_polygon_shape_create() {
  79. GodotShape3D *shape = memnew(GodotConvexPolygonShape3D);
  80. RID rid = shape_owner.make_rid(shape);
  81. shape->set_self(rid);
  82. return rid;
  83. }
  84. RID GodotPhysicsServer3D::concave_polygon_shape_create() {
  85. GodotShape3D *shape = memnew(GodotConcavePolygonShape3D);
  86. RID rid = shape_owner.make_rid(shape);
  87. shape->set_self(rid);
  88. return rid;
  89. }
  90. RID GodotPhysicsServer3D::heightmap_shape_create() {
  91. GodotShape3D *shape = memnew(GodotHeightMapShape3D);
  92. RID rid = shape_owner.make_rid(shape);
  93. shape->set_self(rid);
  94. return rid;
  95. }
  96. RID GodotPhysicsServer3D::custom_shape_create() {
  97. ERR_FAIL_V(RID());
  98. }
  99. void GodotPhysicsServer3D::shape_set_data(RID p_shape, const Variant &p_data) {
  100. GodotShape3D *shape = shape_owner.get_or_null(p_shape);
  101. ERR_FAIL_COND(!shape);
  102. shape->set_data(p_data);
  103. };
  104. void GodotPhysicsServer3D::shape_set_custom_solver_bias(RID p_shape, real_t p_bias) {
  105. GodotShape3D *shape = shape_owner.get_or_null(p_shape);
  106. ERR_FAIL_COND(!shape);
  107. shape->set_custom_bias(p_bias);
  108. }
  109. PhysicsServer3D::ShapeType GodotPhysicsServer3D::shape_get_type(RID p_shape) const {
  110. const GodotShape3D *shape = shape_owner.get_or_null(p_shape);
  111. ERR_FAIL_COND_V(!shape, SHAPE_CUSTOM);
  112. return shape->get_type();
  113. };
  114. Variant GodotPhysicsServer3D::shape_get_data(RID p_shape) const {
  115. const GodotShape3D *shape = shape_owner.get_or_null(p_shape);
  116. ERR_FAIL_COND_V(!shape, Variant());
  117. ERR_FAIL_COND_V(!shape->is_configured(), Variant());
  118. return shape->get_data();
  119. };
  120. void GodotPhysicsServer3D::shape_set_margin(RID p_shape, real_t p_margin) {
  121. }
  122. real_t GodotPhysicsServer3D::shape_get_margin(RID p_shape) const {
  123. return 0.0;
  124. }
  125. real_t GodotPhysicsServer3D::shape_get_custom_solver_bias(RID p_shape) const {
  126. const GodotShape3D *shape = shape_owner.get_or_null(p_shape);
  127. ERR_FAIL_COND_V(!shape, 0);
  128. return shape->get_custom_bias();
  129. }
  130. RID GodotPhysicsServer3D::space_create() {
  131. GodotSpace3D *space = memnew(GodotSpace3D);
  132. RID id = space_owner.make_rid(space);
  133. space->set_self(id);
  134. RID area_id = area_create();
  135. GodotArea3D *area = area_owner.get_or_null(area_id);
  136. ERR_FAIL_COND_V(!area, RID());
  137. space->set_default_area(area);
  138. area->set_space(space);
  139. area->set_priority(-1);
  140. RID sgb = body_create();
  141. body_set_space(sgb, id);
  142. body_set_mode(sgb, BODY_MODE_STATIC);
  143. space->set_static_global_body(sgb);
  144. return id;
  145. };
  146. void GodotPhysicsServer3D::space_set_active(RID p_space, bool p_active) {
  147. GodotSpace3D *space = space_owner.get_or_null(p_space);
  148. ERR_FAIL_COND(!space);
  149. if (p_active) {
  150. active_spaces.insert(space);
  151. } else {
  152. active_spaces.erase(space);
  153. }
  154. }
  155. bool GodotPhysicsServer3D::space_is_active(RID p_space) const {
  156. const GodotSpace3D *space = space_owner.get_or_null(p_space);
  157. ERR_FAIL_COND_V(!space, false);
  158. return active_spaces.has(space);
  159. }
  160. void GodotPhysicsServer3D::space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) {
  161. GodotSpace3D *space = space_owner.get_or_null(p_space);
  162. ERR_FAIL_COND(!space);
  163. space->set_param(p_param, p_value);
  164. }
  165. real_t GodotPhysicsServer3D::space_get_param(RID p_space, SpaceParameter p_param) const {
  166. const GodotSpace3D *space = space_owner.get_or_null(p_space);
  167. ERR_FAIL_COND_V(!space, 0);
  168. return space->get_param(p_param);
  169. }
  170. PhysicsDirectSpaceState3D *GodotPhysicsServer3D::space_get_direct_state(RID p_space) {
  171. GodotSpace3D *space = space_owner.get_or_null(p_space);
  172. ERR_FAIL_COND_V(!space, nullptr);
  173. ERR_FAIL_COND_V_MSG((using_threads && !doing_sync) || space->is_locked(), nullptr, "Space state is inaccessible right now, wait for iteration or physics process notification.");
  174. return space->get_direct_state();
  175. }
  176. void GodotPhysicsServer3D::space_set_debug_contacts(RID p_space, int p_max_contacts) {
  177. GodotSpace3D *space = space_owner.get_or_null(p_space);
  178. ERR_FAIL_COND(!space);
  179. space->set_debug_contacts(p_max_contacts);
  180. }
  181. Vector<Vector3> GodotPhysicsServer3D::space_get_contacts(RID p_space) const {
  182. GodotSpace3D *space = space_owner.get_or_null(p_space);
  183. ERR_FAIL_COND_V(!space, Vector<Vector3>());
  184. return space->get_debug_contacts();
  185. }
  186. int GodotPhysicsServer3D::space_get_contact_count(RID p_space) const {
  187. GodotSpace3D *space = space_owner.get_or_null(p_space);
  188. ERR_FAIL_COND_V(!space, 0);
  189. return space->get_debug_contact_count();
  190. }
  191. RID GodotPhysicsServer3D::area_create() {
  192. GodotArea3D *area = memnew(GodotArea3D);
  193. RID rid = area_owner.make_rid(area);
  194. area->set_self(rid);
  195. return rid;
  196. };
  197. void GodotPhysicsServer3D::area_set_space(RID p_area, RID p_space) {
  198. GodotArea3D *area = area_owner.get_or_null(p_area);
  199. ERR_FAIL_COND(!area);
  200. GodotSpace3D *space = nullptr;
  201. if (p_space.is_valid()) {
  202. space = space_owner.get_or_null(p_space);
  203. ERR_FAIL_COND(!space);
  204. }
  205. if (area->get_space() == space) {
  206. return; //pointless
  207. }
  208. area->clear_constraints();
  209. area->set_space(space);
  210. };
  211. RID GodotPhysicsServer3D::area_get_space(RID p_area) const {
  212. GodotArea3D *area = area_owner.get_or_null(p_area);
  213. ERR_FAIL_COND_V(!area, RID());
  214. GodotSpace3D *space = area->get_space();
  215. if (!space) {
  216. return RID();
  217. }
  218. return space->get_self();
  219. };
  220. void GodotPhysicsServer3D::area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode) {
  221. GodotArea3D *area = area_owner.get_or_null(p_area);
  222. ERR_FAIL_COND(!area);
  223. area->set_space_override_mode(p_mode);
  224. }
  225. PhysicsServer3D::AreaSpaceOverrideMode GodotPhysicsServer3D::area_get_space_override_mode(RID p_area) const {
  226. const GodotArea3D *area = area_owner.get_or_null(p_area);
  227. ERR_FAIL_COND_V(!area, AREA_SPACE_OVERRIDE_DISABLED);
  228. return area->get_space_override_mode();
  229. }
  230. void GodotPhysicsServer3D::area_add_shape(RID p_area, RID p_shape, const Transform3D &p_transform, bool p_disabled) {
  231. GodotArea3D *area = area_owner.get_or_null(p_area);
  232. ERR_FAIL_COND(!area);
  233. GodotShape3D *shape = shape_owner.get_or_null(p_shape);
  234. ERR_FAIL_COND(!shape);
  235. area->add_shape(shape, p_transform, p_disabled);
  236. }
  237. void GodotPhysicsServer3D::area_set_shape(RID p_area, int p_shape_idx, RID p_shape) {
  238. GodotArea3D *area = area_owner.get_or_null(p_area);
  239. ERR_FAIL_COND(!area);
  240. GodotShape3D *shape = shape_owner.get_or_null(p_shape);
  241. ERR_FAIL_COND(!shape);
  242. ERR_FAIL_COND(!shape->is_configured());
  243. area->set_shape(p_shape_idx, shape);
  244. }
  245. void GodotPhysicsServer3D::area_set_shape_transform(RID p_area, int p_shape_idx, const Transform3D &p_transform) {
  246. GodotArea3D *area = area_owner.get_or_null(p_area);
  247. ERR_FAIL_COND(!area);
  248. area->set_shape_transform(p_shape_idx, p_transform);
  249. }
  250. int GodotPhysicsServer3D::area_get_shape_count(RID p_area) const {
  251. GodotArea3D *area = area_owner.get_or_null(p_area);
  252. ERR_FAIL_COND_V(!area, -1);
  253. return area->get_shape_count();
  254. }
  255. RID GodotPhysicsServer3D::area_get_shape(RID p_area, int p_shape_idx) const {
  256. GodotArea3D *area = area_owner.get_or_null(p_area);
  257. ERR_FAIL_COND_V(!area, RID());
  258. GodotShape3D *shape = area->get_shape(p_shape_idx);
  259. ERR_FAIL_COND_V(!shape, RID());
  260. return shape->get_self();
  261. }
  262. Transform3D GodotPhysicsServer3D::area_get_shape_transform(RID p_area, int p_shape_idx) const {
  263. GodotArea3D *area = area_owner.get_or_null(p_area);
  264. ERR_FAIL_COND_V(!area, Transform3D());
  265. return area->get_shape_transform(p_shape_idx);
  266. }
  267. void GodotPhysicsServer3D::area_remove_shape(RID p_area, int p_shape_idx) {
  268. GodotArea3D *area = area_owner.get_or_null(p_area);
  269. ERR_FAIL_COND(!area);
  270. area->remove_shape(p_shape_idx);
  271. }
  272. void GodotPhysicsServer3D::area_clear_shapes(RID p_area) {
  273. GodotArea3D *area = area_owner.get_or_null(p_area);
  274. ERR_FAIL_COND(!area);
  275. while (area->get_shape_count()) {
  276. area->remove_shape(0);
  277. }
  278. }
  279. void GodotPhysicsServer3D::area_set_shape_disabled(RID p_area, int p_shape_idx, bool p_disabled) {
  280. GodotArea3D *area = area_owner.get_or_null(p_area);
  281. ERR_FAIL_COND(!area);
  282. ERR_FAIL_INDEX(p_shape_idx, area->get_shape_count());
  283. FLUSH_QUERY_CHECK(area);
  284. area->set_shape_disabled(p_shape_idx, p_disabled);
  285. }
  286. void GodotPhysicsServer3D::area_attach_object_instance_id(RID p_area, ObjectID p_id) {
  287. if (space_owner.owns(p_area)) {
  288. GodotSpace3D *space = space_owner.get_or_null(p_area);
  289. p_area = space->get_default_area()->get_self();
  290. }
  291. GodotArea3D *area = area_owner.get_or_null(p_area);
  292. ERR_FAIL_COND(!area);
  293. area->set_instance_id(p_id);
  294. }
  295. ObjectID GodotPhysicsServer3D::area_get_object_instance_id(RID p_area) const {
  296. if (space_owner.owns(p_area)) {
  297. GodotSpace3D *space = space_owner.get_or_null(p_area);
  298. p_area = space->get_default_area()->get_self();
  299. }
  300. GodotArea3D *area = area_owner.get_or_null(p_area);
  301. ERR_FAIL_COND_V(!area, ObjectID());
  302. return area->get_instance_id();
  303. }
  304. void GodotPhysicsServer3D::area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) {
  305. if (space_owner.owns(p_area)) {
  306. GodotSpace3D *space = space_owner.get_or_null(p_area);
  307. p_area = space->get_default_area()->get_self();
  308. }
  309. GodotArea3D *area = area_owner.get_or_null(p_area);
  310. ERR_FAIL_COND(!area);
  311. area->set_param(p_param, p_value);
  312. };
  313. void GodotPhysicsServer3D::area_set_transform(RID p_area, const Transform3D &p_transform) {
  314. GodotArea3D *area = area_owner.get_or_null(p_area);
  315. ERR_FAIL_COND(!area);
  316. area->set_transform(p_transform);
  317. };
  318. Variant GodotPhysicsServer3D::area_get_param(RID p_area, AreaParameter p_param) const {
  319. if (space_owner.owns(p_area)) {
  320. GodotSpace3D *space = space_owner.get_or_null(p_area);
  321. p_area = space->get_default_area()->get_self();
  322. }
  323. GodotArea3D *area = area_owner.get_or_null(p_area);
  324. ERR_FAIL_COND_V(!area, Variant());
  325. return area->get_param(p_param);
  326. };
  327. Transform3D GodotPhysicsServer3D::area_get_transform(RID p_area) const {
  328. GodotArea3D *area = area_owner.get_or_null(p_area);
  329. ERR_FAIL_COND_V(!area, Transform3D());
  330. return area->get_transform();
  331. };
  332. void GodotPhysicsServer3D::area_set_collision_layer(RID p_area, uint32_t p_layer) {
  333. GodotArea3D *area = area_owner.get_or_null(p_area);
  334. ERR_FAIL_COND(!area);
  335. area->set_collision_layer(p_layer);
  336. }
  337. void GodotPhysicsServer3D::area_set_collision_mask(RID p_area, uint32_t p_mask) {
  338. GodotArea3D *area = area_owner.get_or_null(p_area);
  339. ERR_FAIL_COND(!area);
  340. area->set_collision_mask(p_mask);
  341. }
  342. void GodotPhysicsServer3D::area_set_monitorable(RID p_area, bool p_monitorable) {
  343. GodotArea3D *area = area_owner.get_or_null(p_area);
  344. ERR_FAIL_COND(!area);
  345. FLUSH_QUERY_CHECK(area);
  346. area->set_monitorable(p_monitorable);
  347. }
  348. void GodotPhysicsServer3D::area_set_monitor_callback(RID p_area, const Callable &p_callback) {
  349. GodotArea3D *area = area_owner.get_or_null(p_area);
  350. ERR_FAIL_COND(!area);
  351. area->set_monitor_callback(p_callback.is_valid() ? p_callback : Callable());
  352. }
  353. void GodotPhysicsServer3D::area_set_ray_pickable(RID p_area, bool p_enable) {
  354. GodotArea3D *area = area_owner.get_or_null(p_area);
  355. ERR_FAIL_COND(!area);
  356. area->set_ray_pickable(p_enable);
  357. }
  358. void GodotPhysicsServer3D::area_set_area_monitor_callback(RID p_area, const Callable &p_callback) {
  359. GodotArea3D *area = area_owner.get_or_null(p_area);
  360. ERR_FAIL_COND(!area);
  361. area->set_area_monitor_callback(p_callback.is_valid() ? p_callback : Callable());
  362. }
  363. /* BODY API */
  364. RID GodotPhysicsServer3D::body_create() {
  365. GodotBody3D *body = memnew(GodotBody3D);
  366. RID rid = body_owner.make_rid(body);
  367. body->set_self(rid);
  368. return rid;
  369. };
  370. void GodotPhysicsServer3D::body_set_space(RID p_body, RID p_space) {
  371. GodotBody3D *body = body_owner.get_or_null(p_body);
  372. ERR_FAIL_COND(!body);
  373. GodotSpace3D *space = nullptr;
  374. if (p_space.is_valid()) {
  375. space = space_owner.get_or_null(p_space);
  376. ERR_FAIL_COND(!space);
  377. }
  378. if (body->get_space() == space) {
  379. return; //pointless
  380. }
  381. body->clear_constraint_map();
  382. body->set_space(space);
  383. };
  384. RID GodotPhysicsServer3D::body_get_space(RID p_body) const {
  385. GodotBody3D *body = body_owner.get_or_null(p_body);
  386. ERR_FAIL_COND_V(!body, RID());
  387. GodotSpace3D *space = body->get_space();
  388. if (!space) {
  389. return RID();
  390. }
  391. return space->get_self();
  392. };
  393. void GodotPhysicsServer3D::body_set_mode(RID p_body, BodyMode p_mode) {
  394. GodotBody3D *body = body_owner.get_or_null(p_body);
  395. ERR_FAIL_COND(!body);
  396. body->set_mode(p_mode);
  397. };
  398. PhysicsServer3D::BodyMode GodotPhysicsServer3D::body_get_mode(RID p_body) const {
  399. GodotBody3D *body = body_owner.get_or_null(p_body);
  400. ERR_FAIL_COND_V(!body, BODY_MODE_STATIC);
  401. return body->get_mode();
  402. };
  403. void GodotPhysicsServer3D::body_add_shape(RID p_body, RID p_shape, const Transform3D &p_transform, bool p_disabled) {
  404. GodotBody3D *body = body_owner.get_or_null(p_body);
  405. ERR_FAIL_COND(!body);
  406. GodotShape3D *shape = shape_owner.get_or_null(p_shape);
  407. ERR_FAIL_COND(!shape);
  408. body->add_shape(shape, p_transform, p_disabled);
  409. }
  410. void GodotPhysicsServer3D::body_set_shape(RID p_body, int p_shape_idx, RID p_shape) {
  411. GodotBody3D *body = body_owner.get_or_null(p_body);
  412. ERR_FAIL_COND(!body);
  413. GodotShape3D *shape = shape_owner.get_or_null(p_shape);
  414. ERR_FAIL_COND(!shape);
  415. ERR_FAIL_COND(!shape->is_configured());
  416. body->set_shape(p_shape_idx, shape);
  417. }
  418. void GodotPhysicsServer3D::body_set_shape_transform(RID p_body, int p_shape_idx, const Transform3D &p_transform) {
  419. GodotBody3D *body = body_owner.get_or_null(p_body);
  420. ERR_FAIL_COND(!body);
  421. body->set_shape_transform(p_shape_idx, p_transform);
  422. }
  423. int GodotPhysicsServer3D::body_get_shape_count(RID p_body) const {
  424. GodotBody3D *body = body_owner.get_or_null(p_body);
  425. ERR_FAIL_COND_V(!body, -1);
  426. return body->get_shape_count();
  427. }
  428. RID GodotPhysicsServer3D::body_get_shape(RID p_body, int p_shape_idx) const {
  429. GodotBody3D *body = body_owner.get_or_null(p_body);
  430. ERR_FAIL_COND_V(!body, RID());
  431. GodotShape3D *shape = body->get_shape(p_shape_idx);
  432. ERR_FAIL_COND_V(!shape, RID());
  433. return shape->get_self();
  434. }
  435. void GodotPhysicsServer3D::body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled) {
  436. GodotBody3D *body = body_owner.get_or_null(p_body);
  437. ERR_FAIL_COND(!body);
  438. ERR_FAIL_INDEX(p_shape_idx, body->get_shape_count());
  439. FLUSH_QUERY_CHECK(body);
  440. body->set_shape_disabled(p_shape_idx, p_disabled);
  441. }
  442. Transform3D GodotPhysicsServer3D::body_get_shape_transform(RID p_body, int p_shape_idx) const {
  443. GodotBody3D *body = body_owner.get_or_null(p_body);
  444. ERR_FAIL_COND_V(!body, Transform3D());
  445. return body->get_shape_transform(p_shape_idx);
  446. }
  447. void GodotPhysicsServer3D::body_remove_shape(RID p_body, int p_shape_idx) {
  448. GodotBody3D *body = body_owner.get_or_null(p_body);
  449. ERR_FAIL_COND(!body);
  450. body->remove_shape(p_shape_idx);
  451. }
  452. void GodotPhysicsServer3D::body_clear_shapes(RID p_body) {
  453. GodotBody3D *body = body_owner.get_or_null(p_body);
  454. ERR_FAIL_COND(!body);
  455. while (body->get_shape_count()) {
  456. body->remove_shape(0);
  457. }
  458. }
  459. void GodotPhysicsServer3D::body_set_enable_continuous_collision_detection(RID p_body, bool p_enable) {
  460. GodotBody3D *body = body_owner.get_or_null(p_body);
  461. ERR_FAIL_COND(!body);
  462. body->set_continuous_collision_detection(p_enable);
  463. }
  464. bool GodotPhysicsServer3D::body_is_continuous_collision_detection_enabled(RID p_body) const {
  465. GodotBody3D *body = body_owner.get_or_null(p_body);
  466. ERR_FAIL_COND_V(!body, false);
  467. return body->is_continuous_collision_detection_enabled();
  468. }
  469. void GodotPhysicsServer3D::body_set_collision_layer(RID p_body, uint32_t p_layer) {
  470. GodotBody3D *body = body_owner.get_or_null(p_body);
  471. ERR_FAIL_COND(!body);
  472. body->set_collision_layer(p_layer);
  473. }
  474. uint32_t GodotPhysicsServer3D::body_get_collision_layer(RID p_body) const {
  475. const GodotBody3D *body = body_owner.get_or_null(p_body);
  476. ERR_FAIL_COND_V(!body, 0);
  477. return body->get_collision_layer();
  478. }
  479. void GodotPhysicsServer3D::body_set_collision_mask(RID p_body, uint32_t p_mask) {
  480. GodotBody3D *body = body_owner.get_or_null(p_body);
  481. ERR_FAIL_COND(!body);
  482. body->set_collision_mask(p_mask);
  483. }
  484. uint32_t GodotPhysicsServer3D::body_get_collision_mask(RID p_body) const {
  485. const GodotBody3D *body = body_owner.get_or_null(p_body);
  486. ERR_FAIL_COND_V(!body, 0);
  487. return body->get_collision_mask();
  488. }
  489. void GodotPhysicsServer3D::body_attach_object_instance_id(RID p_body, ObjectID p_id) {
  490. GodotBody3D *body = body_owner.get_or_null(p_body);
  491. if (body) {
  492. body->set_instance_id(p_id);
  493. return;
  494. }
  495. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  496. if (soft_body) {
  497. soft_body->set_instance_id(p_id);
  498. return;
  499. }
  500. ERR_FAIL_MSG("Invalid ID.");
  501. };
  502. ObjectID GodotPhysicsServer3D::body_get_object_instance_id(RID p_body) const {
  503. GodotBody3D *body = body_owner.get_or_null(p_body);
  504. ERR_FAIL_COND_V(!body, ObjectID());
  505. return body->get_instance_id();
  506. };
  507. void GodotPhysicsServer3D::body_set_user_flags(RID p_body, uint32_t p_flags) {
  508. GodotBody3D *body = body_owner.get_or_null(p_body);
  509. ERR_FAIL_COND(!body);
  510. };
  511. uint32_t GodotPhysicsServer3D::body_get_user_flags(RID p_body) const {
  512. GodotBody3D *body = body_owner.get_or_null(p_body);
  513. ERR_FAIL_COND_V(!body, 0);
  514. return 0;
  515. };
  516. void GodotPhysicsServer3D::body_set_param(RID p_body, BodyParameter p_param, const Variant &p_value) {
  517. GodotBody3D *body = body_owner.get_or_null(p_body);
  518. ERR_FAIL_COND(!body);
  519. body->set_param(p_param, p_value);
  520. };
  521. Variant GodotPhysicsServer3D::body_get_param(RID p_body, BodyParameter p_param) const {
  522. GodotBody3D *body = body_owner.get_or_null(p_body);
  523. ERR_FAIL_COND_V(!body, 0);
  524. return body->get_param(p_param);
  525. };
  526. void GodotPhysicsServer3D::body_reset_mass_properties(RID p_body) {
  527. GodotBody3D *body = body_owner.get_or_null(p_body);
  528. ERR_FAIL_COND(!body);
  529. return body->reset_mass_properties();
  530. }
  531. void GodotPhysicsServer3D::body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
  532. GodotBody3D *body = body_owner.get_or_null(p_body);
  533. ERR_FAIL_COND(!body);
  534. body->set_state(p_state, p_variant);
  535. };
  536. Variant GodotPhysicsServer3D::body_get_state(RID p_body, BodyState p_state) const {
  537. GodotBody3D *body = body_owner.get_or_null(p_body);
  538. ERR_FAIL_COND_V(!body, Variant());
  539. return body->get_state(p_state);
  540. };
  541. void GodotPhysicsServer3D::body_set_applied_force(RID p_body, const Vector3 &p_force) {
  542. GodotBody3D *body = body_owner.get_or_null(p_body);
  543. ERR_FAIL_COND(!body);
  544. body->set_applied_force(p_force);
  545. body->wakeup();
  546. };
  547. Vector3 GodotPhysicsServer3D::body_get_applied_force(RID p_body) const {
  548. GodotBody3D *body = body_owner.get_or_null(p_body);
  549. ERR_FAIL_COND_V(!body, Vector3());
  550. return body->get_applied_force();
  551. };
  552. void GodotPhysicsServer3D::body_set_applied_torque(RID p_body, const Vector3 &p_torque) {
  553. GodotBody3D *body = body_owner.get_or_null(p_body);
  554. ERR_FAIL_COND(!body);
  555. body->set_applied_torque(p_torque);
  556. body->wakeup();
  557. };
  558. Vector3 GodotPhysicsServer3D::body_get_applied_torque(RID p_body) const {
  559. GodotBody3D *body = body_owner.get_or_null(p_body);
  560. ERR_FAIL_COND_V(!body, Vector3());
  561. return body->get_applied_torque();
  562. };
  563. void GodotPhysicsServer3D::body_add_central_force(RID p_body, const Vector3 &p_force) {
  564. GodotBody3D *body = body_owner.get_or_null(p_body);
  565. ERR_FAIL_COND(!body);
  566. body->add_central_force(p_force);
  567. body->wakeup();
  568. }
  569. void GodotPhysicsServer3D::body_add_force(RID p_body, const Vector3 &p_force, const Vector3 &p_position) {
  570. GodotBody3D *body = body_owner.get_or_null(p_body);
  571. ERR_FAIL_COND(!body);
  572. body->add_force(p_force, p_position);
  573. body->wakeup();
  574. };
  575. void GodotPhysicsServer3D::body_add_torque(RID p_body, const Vector3 &p_torque) {
  576. GodotBody3D *body = body_owner.get_or_null(p_body);
  577. ERR_FAIL_COND(!body);
  578. body->add_torque(p_torque);
  579. body->wakeup();
  580. };
  581. void GodotPhysicsServer3D::body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) {
  582. GodotBody3D *body = body_owner.get_or_null(p_body);
  583. ERR_FAIL_COND(!body);
  584. _update_shapes();
  585. body->apply_central_impulse(p_impulse);
  586. body->wakeup();
  587. }
  588. void GodotPhysicsServer3D::body_apply_impulse(RID p_body, const Vector3 &p_impulse, const Vector3 &p_position) {
  589. GodotBody3D *body = body_owner.get_or_null(p_body);
  590. ERR_FAIL_COND(!body);
  591. _update_shapes();
  592. body->apply_impulse(p_impulse, p_position);
  593. body->wakeup();
  594. };
  595. void GodotPhysicsServer3D::body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse) {
  596. GodotBody3D *body = body_owner.get_or_null(p_body);
  597. ERR_FAIL_COND(!body);
  598. _update_shapes();
  599. body->apply_torque_impulse(p_impulse);
  600. body->wakeup();
  601. };
  602. void GodotPhysicsServer3D::body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity) {
  603. GodotBody3D *body = body_owner.get_or_null(p_body);
  604. ERR_FAIL_COND(!body);
  605. _update_shapes();
  606. Vector3 v = body->get_linear_velocity();
  607. Vector3 axis = p_axis_velocity.normalized();
  608. v -= axis * axis.dot(v);
  609. v += p_axis_velocity;
  610. body->set_linear_velocity(v);
  611. body->wakeup();
  612. };
  613. void GodotPhysicsServer3D::body_set_axis_lock(RID p_body, BodyAxis p_axis, bool p_lock) {
  614. GodotBody3D *body = body_owner.get_or_null(p_body);
  615. ERR_FAIL_COND(!body);
  616. body->set_axis_lock(p_axis, p_lock);
  617. body->wakeup();
  618. }
  619. bool GodotPhysicsServer3D::body_is_axis_locked(RID p_body, BodyAxis p_axis) const {
  620. const GodotBody3D *body = body_owner.get_or_null(p_body);
  621. ERR_FAIL_COND_V(!body, 0);
  622. return body->is_axis_locked(p_axis);
  623. }
  624. void GodotPhysicsServer3D::body_add_collision_exception(RID p_body, RID p_body_b) {
  625. GodotBody3D *body = body_owner.get_or_null(p_body);
  626. ERR_FAIL_COND(!body);
  627. body->add_exception(p_body_b);
  628. body->wakeup();
  629. };
  630. void GodotPhysicsServer3D::body_remove_collision_exception(RID p_body, RID p_body_b) {
  631. GodotBody3D *body = body_owner.get_or_null(p_body);
  632. ERR_FAIL_COND(!body);
  633. body->remove_exception(p_body_b);
  634. body->wakeup();
  635. };
  636. void GodotPhysicsServer3D::body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  637. GodotBody3D *body = body_owner.get_or_null(p_body);
  638. ERR_FAIL_COND(!body);
  639. for (int i = 0; i < body->get_exceptions().size(); i++) {
  640. p_exceptions->push_back(body->get_exceptions()[i]);
  641. }
  642. };
  643. void GodotPhysicsServer3D::body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) {
  644. GodotBody3D *body = body_owner.get_or_null(p_body);
  645. ERR_FAIL_COND(!body);
  646. };
  647. real_t GodotPhysicsServer3D::body_get_contacts_reported_depth_threshold(RID p_body) const {
  648. GodotBody3D *body = body_owner.get_or_null(p_body);
  649. ERR_FAIL_COND_V(!body, 0);
  650. return 0;
  651. };
  652. void GodotPhysicsServer3D::body_set_omit_force_integration(RID p_body, bool p_omit) {
  653. GodotBody3D *body = body_owner.get_or_null(p_body);
  654. ERR_FAIL_COND(!body);
  655. body->set_omit_force_integration(p_omit);
  656. };
  657. bool GodotPhysicsServer3D::body_is_omitting_force_integration(RID p_body) const {
  658. GodotBody3D *body = body_owner.get_or_null(p_body);
  659. ERR_FAIL_COND_V(!body, false);
  660. return body->get_omit_force_integration();
  661. };
  662. void GodotPhysicsServer3D::body_set_max_contacts_reported(RID p_body, int p_contacts) {
  663. GodotBody3D *body = body_owner.get_or_null(p_body);
  664. ERR_FAIL_COND(!body);
  665. body->set_max_contacts_reported(p_contacts);
  666. }
  667. int GodotPhysicsServer3D::body_get_max_contacts_reported(RID p_body) const {
  668. GodotBody3D *body = body_owner.get_or_null(p_body);
  669. ERR_FAIL_COND_V(!body, -1);
  670. return body->get_max_contacts_reported();
  671. }
  672. void GodotPhysicsServer3D::body_set_state_sync_callback(RID p_body, void *p_instance, BodyStateCallback p_callback) {
  673. GodotBody3D *body = body_owner.get_or_null(p_body);
  674. ERR_FAIL_COND(!body);
  675. body->set_state_sync_callback(p_instance, p_callback);
  676. }
  677. void GodotPhysicsServer3D::body_set_force_integration_callback(RID p_body, const Callable &p_callable, const Variant &p_udata) {
  678. GodotBody3D *body = body_owner.get_or_null(p_body);
  679. ERR_FAIL_COND(!body);
  680. body->set_force_integration_callback(p_callable, p_udata);
  681. }
  682. void GodotPhysicsServer3D::body_set_ray_pickable(RID p_body, bool p_enable) {
  683. GodotBody3D *body = body_owner.get_or_null(p_body);
  684. ERR_FAIL_COND(!body);
  685. body->set_ray_pickable(p_enable);
  686. }
  687. bool GodotPhysicsServer3D::body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result) {
  688. GodotBody3D *body = body_owner.get_or_null(p_body);
  689. ERR_FAIL_COND_V(!body, false);
  690. ERR_FAIL_COND_V(!body->get_space(), false);
  691. ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
  692. _update_shapes();
  693. return body->get_space()->test_body_motion(body, p_parameters, r_result);
  694. }
  695. PhysicsDirectBodyState3D *GodotPhysicsServer3D::body_get_direct_state(RID p_body) {
  696. ERR_FAIL_COND_V_MSG((using_threads && !doing_sync), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
  697. GodotBody3D *body = body_owner.get_or_null(p_body);
  698. ERR_FAIL_NULL_V(body, nullptr);
  699. ERR_FAIL_NULL_V(body->get_space(), nullptr);
  700. ERR_FAIL_COND_V_MSG(body->get_space()->is_locked(), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
  701. return body->get_direct_state();
  702. }
  703. /* SOFT BODY */
  704. RID GodotPhysicsServer3D::soft_body_create() {
  705. GodotSoftBody3D *soft_body = memnew(GodotSoftBody3D);
  706. RID rid = soft_body_owner.make_rid(soft_body);
  707. soft_body->set_self(rid);
  708. return rid;
  709. }
  710. void GodotPhysicsServer3D::soft_body_update_rendering_server(RID p_body, RenderingServerHandler *p_rendering_server_handler) {
  711. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  712. ERR_FAIL_COND(!soft_body);
  713. soft_body->update_rendering_server(p_rendering_server_handler);
  714. }
  715. void GodotPhysicsServer3D::soft_body_set_space(RID p_body, RID p_space) {
  716. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  717. ERR_FAIL_COND(!soft_body);
  718. GodotSpace3D *space = nullptr;
  719. if (p_space.is_valid()) {
  720. space = space_owner.get_or_null(p_space);
  721. ERR_FAIL_COND(!space);
  722. }
  723. if (soft_body->get_space() == space) {
  724. return;
  725. }
  726. soft_body->set_space(space);
  727. }
  728. RID GodotPhysicsServer3D::soft_body_get_space(RID p_body) const {
  729. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  730. ERR_FAIL_COND_V(!soft_body, RID());
  731. GodotSpace3D *space = soft_body->get_space();
  732. if (!space) {
  733. return RID();
  734. }
  735. return space->get_self();
  736. }
  737. void GodotPhysicsServer3D::soft_body_set_collision_layer(RID p_body, uint32_t p_layer) {
  738. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  739. ERR_FAIL_COND(!soft_body);
  740. soft_body->set_collision_layer(p_layer);
  741. }
  742. uint32_t GodotPhysicsServer3D::soft_body_get_collision_layer(RID p_body) const {
  743. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  744. ERR_FAIL_COND_V(!soft_body, 0);
  745. return soft_body->get_collision_layer();
  746. }
  747. void GodotPhysicsServer3D::soft_body_set_collision_mask(RID p_body, uint32_t p_mask) {
  748. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  749. ERR_FAIL_COND(!soft_body);
  750. soft_body->set_collision_mask(p_mask);
  751. }
  752. uint32_t GodotPhysicsServer3D::soft_body_get_collision_mask(RID p_body) const {
  753. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  754. ERR_FAIL_COND_V(!soft_body, 0);
  755. return soft_body->get_collision_mask();
  756. }
  757. void GodotPhysicsServer3D::soft_body_add_collision_exception(RID p_body, RID p_body_b) {
  758. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  759. ERR_FAIL_COND(!soft_body);
  760. soft_body->add_exception(p_body_b);
  761. }
  762. void GodotPhysicsServer3D::soft_body_remove_collision_exception(RID p_body, RID p_body_b) {
  763. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  764. ERR_FAIL_COND(!soft_body);
  765. soft_body->remove_exception(p_body_b);
  766. }
  767. void GodotPhysicsServer3D::soft_body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  768. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  769. ERR_FAIL_COND(!soft_body);
  770. for (int i = 0; i < soft_body->get_exceptions().size(); i++) {
  771. p_exceptions->push_back(soft_body->get_exceptions()[i]);
  772. }
  773. }
  774. void GodotPhysicsServer3D::soft_body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
  775. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  776. ERR_FAIL_COND(!soft_body);
  777. soft_body->set_state(p_state, p_variant);
  778. }
  779. Variant GodotPhysicsServer3D::soft_body_get_state(RID p_body, BodyState p_state) const {
  780. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  781. ERR_FAIL_COND_V(!soft_body, Variant());
  782. return soft_body->get_state(p_state);
  783. }
  784. void GodotPhysicsServer3D::soft_body_set_transform(RID p_body, const Transform3D &p_transform) {
  785. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  786. ERR_FAIL_COND(!soft_body);
  787. soft_body->set_state(BODY_STATE_TRANSFORM, p_transform);
  788. }
  789. void GodotPhysicsServer3D::soft_body_set_ray_pickable(RID p_body, bool p_enable) {
  790. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  791. ERR_FAIL_COND(!soft_body);
  792. soft_body->set_ray_pickable(p_enable);
  793. }
  794. void GodotPhysicsServer3D::soft_body_set_simulation_precision(RID p_body, int p_simulation_precision) {
  795. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  796. ERR_FAIL_COND(!soft_body);
  797. soft_body->set_iteration_count(p_simulation_precision);
  798. }
  799. int GodotPhysicsServer3D::soft_body_get_simulation_precision(RID p_body) const {
  800. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  801. ERR_FAIL_COND_V(!soft_body, 0.f);
  802. return soft_body->get_iteration_count();
  803. }
  804. void GodotPhysicsServer3D::soft_body_set_total_mass(RID p_body, real_t p_total_mass) {
  805. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  806. ERR_FAIL_COND(!soft_body);
  807. soft_body->set_total_mass(p_total_mass);
  808. }
  809. real_t GodotPhysicsServer3D::soft_body_get_total_mass(RID p_body) const {
  810. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  811. ERR_FAIL_COND_V(!soft_body, 0.f);
  812. return soft_body->get_total_mass();
  813. }
  814. void GodotPhysicsServer3D::soft_body_set_linear_stiffness(RID p_body, real_t p_stiffness) {
  815. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  816. ERR_FAIL_COND(!soft_body);
  817. soft_body->set_linear_stiffness(p_stiffness);
  818. }
  819. real_t GodotPhysicsServer3D::soft_body_get_linear_stiffness(RID p_body) const {
  820. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  821. ERR_FAIL_COND_V(!soft_body, 0.f);
  822. return soft_body->get_linear_stiffness();
  823. }
  824. void GodotPhysicsServer3D::soft_body_set_pressure_coefficient(RID p_body, real_t p_pressure_coefficient) {
  825. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  826. ERR_FAIL_COND(!soft_body);
  827. soft_body->set_pressure_coefficient(p_pressure_coefficient);
  828. }
  829. real_t GodotPhysicsServer3D::soft_body_get_pressure_coefficient(RID p_body) const {
  830. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  831. ERR_FAIL_COND_V(!soft_body, 0.f);
  832. return soft_body->get_pressure_coefficient();
  833. }
  834. void GodotPhysicsServer3D::soft_body_set_damping_coefficient(RID p_body, real_t p_damping_coefficient) {
  835. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  836. ERR_FAIL_COND(!soft_body);
  837. soft_body->set_damping_coefficient(p_damping_coefficient);
  838. }
  839. real_t GodotPhysicsServer3D::soft_body_get_damping_coefficient(RID p_body) const {
  840. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  841. ERR_FAIL_COND_V(!soft_body, 0.f);
  842. return soft_body->get_damping_coefficient();
  843. }
  844. void GodotPhysicsServer3D::soft_body_set_drag_coefficient(RID p_body, real_t p_drag_coefficient) {
  845. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  846. ERR_FAIL_COND(!soft_body);
  847. soft_body->set_drag_coefficient(p_drag_coefficient);
  848. }
  849. real_t GodotPhysicsServer3D::soft_body_get_drag_coefficient(RID p_body) const {
  850. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  851. ERR_FAIL_COND_V(!soft_body, 0.f);
  852. return soft_body->get_drag_coefficient();
  853. }
  854. void GodotPhysicsServer3D::soft_body_set_mesh(RID p_body, RID p_mesh) {
  855. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  856. ERR_FAIL_COND(!soft_body);
  857. soft_body->set_mesh(p_mesh);
  858. }
  859. AABB GodotPhysicsServer3D::soft_body_get_bounds(RID p_body) const {
  860. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  861. ERR_FAIL_COND_V(!soft_body, AABB());
  862. return soft_body->get_bounds();
  863. }
  864. void GodotPhysicsServer3D::soft_body_move_point(RID p_body, int p_point_index, const Vector3 &p_global_position) {
  865. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  866. ERR_FAIL_COND(!soft_body);
  867. soft_body->set_vertex_position(p_point_index, p_global_position);
  868. }
  869. Vector3 GodotPhysicsServer3D::soft_body_get_point_global_position(RID p_body, int p_point_index) const {
  870. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  871. ERR_FAIL_COND_V(!soft_body, Vector3());
  872. return soft_body->get_vertex_position(p_point_index);
  873. }
  874. void GodotPhysicsServer3D::soft_body_remove_all_pinned_points(RID p_body) {
  875. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  876. ERR_FAIL_COND(!soft_body);
  877. soft_body->unpin_all_vertices();
  878. }
  879. void GodotPhysicsServer3D::soft_body_pin_point(RID p_body, int p_point_index, bool p_pin) {
  880. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  881. ERR_FAIL_COND(!soft_body);
  882. if (p_pin) {
  883. soft_body->pin_vertex(p_point_index);
  884. } else {
  885. soft_body->unpin_vertex(p_point_index);
  886. }
  887. }
  888. bool GodotPhysicsServer3D::soft_body_is_point_pinned(RID p_body, int p_point_index) const {
  889. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  890. ERR_FAIL_COND_V(!soft_body, false);
  891. return soft_body->is_vertex_pinned(p_point_index);
  892. }
  893. /* JOINT API */
  894. RID GodotPhysicsServer3D::joint_create() {
  895. GodotJoint3D *joint = memnew(GodotJoint3D);
  896. RID rid = joint_owner.make_rid(joint);
  897. joint->set_self(rid);
  898. return rid;
  899. }
  900. void GodotPhysicsServer3D::joint_clear(RID p_joint) {
  901. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  902. if (joint->get_type() != JOINT_TYPE_MAX) {
  903. GodotJoint3D *empty_joint = memnew(GodotJoint3D);
  904. empty_joint->copy_settings_from(joint);
  905. joint_owner.replace(p_joint, empty_joint);
  906. memdelete(joint);
  907. }
  908. }
  909. void GodotPhysicsServer3D::joint_make_pin(RID p_joint, RID p_body_A, const Vector3 &p_local_A, RID p_body_B, const Vector3 &p_local_B) {
  910. GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
  911. ERR_FAIL_COND(!body_A);
  912. if (!p_body_B.is_valid()) {
  913. ERR_FAIL_COND(!body_A->get_space());
  914. p_body_B = body_A->get_space()->get_static_global_body();
  915. }
  916. GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
  917. ERR_FAIL_COND(!body_B);
  918. ERR_FAIL_COND(body_A == body_B);
  919. GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
  920. ERR_FAIL_COND(prev_joint == nullptr);
  921. GodotJoint3D *joint = memnew(GodotPinJoint3D(body_A, p_local_A, body_B, p_local_B));
  922. joint->copy_settings_from(prev_joint);
  923. joint_owner.replace(p_joint, joint);
  924. memdelete(prev_joint);
  925. }
  926. void GodotPhysicsServer3D::pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) {
  927. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  928. ERR_FAIL_COND(!joint);
  929. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
  930. GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
  931. pin_joint->set_param(p_param, p_value);
  932. }
  933. real_t GodotPhysicsServer3D::pin_joint_get_param(RID p_joint, PinJointParam p_param) const {
  934. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  935. ERR_FAIL_COND_V(!joint, 0);
  936. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, 0);
  937. GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
  938. return pin_joint->get_param(p_param);
  939. }
  940. void GodotPhysicsServer3D::pin_joint_set_local_a(RID p_joint, const Vector3 &p_A) {
  941. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  942. ERR_FAIL_COND(!joint);
  943. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
  944. GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
  945. pin_joint->set_pos_a(p_A);
  946. }
  947. Vector3 GodotPhysicsServer3D::pin_joint_get_local_a(RID p_joint) const {
  948. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  949. ERR_FAIL_COND_V(!joint, Vector3());
  950. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, Vector3());
  951. GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
  952. return pin_joint->get_position_a();
  953. }
  954. void GodotPhysicsServer3D::pin_joint_set_local_b(RID p_joint, const Vector3 &p_B) {
  955. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  956. ERR_FAIL_COND(!joint);
  957. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
  958. GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
  959. pin_joint->set_pos_b(p_B);
  960. }
  961. Vector3 GodotPhysicsServer3D::pin_joint_get_local_b(RID p_joint) const {
  962. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  963. ERR_FAIL_COND_V(!joint, Vector3());
  964. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, Vector3());
  965. GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
  966. return pin_joint->get_position_b();
  967. }
  968. void GodotPhysicsServer3D::joint_make_hinge(RID p_joint, RID p_body_A, const Transform3D &p_frame_A, RID p_body_B, const Transform3D &p_frame_B) {
  969. GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
  970. ERR_FAIL_COND(!body_A);
  971. if (!p_body_B.is_valid()) {
  972. ERR_FAIL_COND(!body_A->get_space());
  973. p_body_B = body_A->get_space()->get_static_global_body();
  974. }
  975. GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
  976. ERR_FAIL_COND(!body_B);
  977. ERR_FAIL_COND(body_A == body_B);
  978. GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
  979. ERR_FAIL_COND(prev_joint == nullptr);
  980. GodotJoint3D *joint = memnew(GodotHingeJoint3D(body_A, body_B, p_frame_A, p_frame_B));
  981. joint->copy_settings_from(prev_joint);
  982. joint_owner.replace(p_joint, joint);
  983. memdelete(prev_joint);
  984. }
  985. void GodotPhysicsServer3D::joint_make_hinge_simple(RID p_joint, RID p_body_A, const Vector3 &p_pivot_A, const Vector3 &p_axis_A, RID p_body_B, const Vector3 &p_pivot_B, const Vector3 &p_axis_B) {
  986. GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
  987. ERR_FAIL_COND(!body_A);
  988. if (!p_body_B.is_valid()) {
  989. ERR_FAIL_COND(!body_A->get_space());
  990. p_body_B = body_A->get_space()->get_static_global_body();
  991. }
  992. GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
  993. ERR_FAIL_COND(!body_B);
  994. ERR_FAIL_COND(body_A == body_B);
  995. GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
  996. ERR_FAIL_COND(prev_joint == nullptr);
  997. GodotJoint3D *joint = memnew(GodotHingeJoint3D(body_A, body_B, p_pivot_A, p_pivot_B, p_axis_A, p_axis_B));
  998. joint->copy_settings_from(prev_joint);
  999. joint_owner.replace(p_joint, joint);
  1000. memdelete(prev_joint);
  1001. }
  1002. void GodotPhysicsServer3D::hinge_joint_set_param(RID p_joint, HingeJointParam p_param, real_t p_value) {
  1003. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1004. ERR_FAIL_COND(!joint);
  1005. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_HINGE);
  1006. GodotHingeJoint3D *hinge_joint = static_cast<GodotHingeJoint3D *>(joint);
  1007. hinge_joint->set_param(p_param, p_value);
  1008. }
  1009. real_t GodotPhysicsServer3D::hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const {
  1010. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1011. ERR_FAIL_COND_V(!joint, 0);
  1012. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, 0);
  1013. GodotHingeJoint3D *hinge_joint = static_cast<GodotHingeJoint3D *>(joint);
  1014. return hinge_joint->get_param(p_param);
  1015. }
  1016. void GodotPhysicsServer3D::hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_value) {
  1017. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1018. ERR_FAIL_COND(!joint);
  1019. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_HINGE);
  1020. GodotHingeJoint3D *hinge_joint = static_cast<GodotHingeJoint3D *>(joint);
  1021. hinge_joint->set_flag(p_flag, p_value);
  1022. }
  1023. bool GodotPhysicsServer3D::hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const {
  1024. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1025. ERR_FAIL_COND_V(!joint, false);
  1026. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, false);
  1027. GodotHingeJoint3D *hinge_joint = static_cast<GodotHingeJoint3D *>(joint);
  1028. return hinge_joint->get_flag(p_flag);
  1029. }
  1030. void GodotPhysicsServer3D::joint_set_solver_priority(RID p_joint, int p_priority) {
  1031. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1032. ERR_FAIL_COND(!joint);
  1033. joint->set_priority(p_priority);
  1034. }
  1035. int GodotPhysicsServer3D::joint_get_solver_priority(RID p_joint) const {
  1036. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1037. ERR_FAIL_COND_V(!joint, 0);
  1038. return joint->get_priority();
  1039. }
  1040. void GodotPhysicsServer3D::joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable) {
  1041. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1042. ERR_FAIL_COND(!joint);
  1043. joint->disable_collisions_between_bodies(p_disable);
  1044. if (2 == joint->get_body_count()) {
  1045. GodotBody3D *body_a = *joint->get_body_ptr();
  1046. GodotBody3D *body_b = *(joint->get_body_ptr() + 1);
  1047. if (p_disable) {
  1048. body_add_collision_exception(body_a->get_self(), body_b->get_self());
  1049. body_add_collision_exception(body_b->get_self(), body_a->get_self());
  1050. } else {
  1051. body_remove_collision_exception(body_a->get_self(), body_b->get_self());
  1052. body_remove_collision_exception(body_b->get_self(), body_a->get_self());
  1053. }
  1054. }
  1055. }
  1056. bool GodotPhysicsServer3D::joint_is_disabled_collisions_between_bodies(RID p_joint) const {
  1057. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1058. ERR_FAIL_COND_V(!joint, true);
  1059. return joint->is_disabled_collisions_between_bodies();
  1060. }
  1061. GodotPhysicsServer3D::JointType GodotPhysicsServer3D::joint_get_type(RID p_joint) const {
  1062. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1063. ERR_FAIL_COND_V(!joint, JOINT_TYPE_PIN);
  1064. return joint->get_type();
  1065. }
  1066. void GodotPhysicsServer3D::joint_make_slider(RID p_joint, RID p_body_A, const Transform3D &p_local_frame_A, RID p_body_B, const Transform3D &p_local_frame_B) {
  1067. GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
  1068. ERR_FAIL_COND(!body_A);
  1069. if (!p_body_B.is_valid()) {
  1070. ERR_FAIL_COND(!body_A->get_space());
  1071. p_body_B = body_A->get_space()->get_static_global_body();
  1072. }
  1073. GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
  1074. ERR_FAIL_COND(!body_B);
  1075. ERR_FAIL_COND(body_A == body_B);
  1076. GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
  1077. ERR_FAIL_COND(prev_joint == nullptr);
  1078. GodotJoint3D *joint = memnew(GodotSliderJoint3D(body_A, body_B, p_local_frame_A, p_local_frame_B));
  1079. joint->copy_settings_from(prev_joint);
  1080. joint_owner.replace(p_joint, joint);
  1081. memdelete(prev_joint);
  1082. }
  1083. void GodotPhysicsServer3D::slider_joint_set_param(RID p_joint, SliderJointParam p_param, real_t p_value) {
  1084. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1085. ERR_FAIL_COND(!joint);
  1086. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_SLIDER);
  1087. GodotSliderJoint3D *slider_joint = static_cast<GodotSliderJoint3D *>(joint);
  1088. slider_joint->set_param(p_param, p_value);
  1089. }
  1090. real_t GodotPhysicsServer3D::slider_joint_get_param(RID p_joint, SliderJointParam p_param) const {
  1091. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1092. ERR_FAIL_COND_V(!joint, 0);
  1093. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, 0);
  1094. GodotSliderJoint3D *slider_joint = static_cast<GodotSliderJoint3D *>(joint);
  1095. return slider_joint->get_param(p_param);
  1096. }
  1097. void GodotPhysicsServer3D::joint_make_cone_twist(RID p_joint, RID p_body_A, const Transform3D &p_local_frame_A, RID p_body_B, const Transform3D &p_local_frame_B) {
  1098. GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
  1099. ERR_FAIL_COND(!body_A);
  1100. if (!p_body_B.is_valid()) {
  1101. ERR_FAIL_COND(!body_A->get_space());
  1102. p_body_B = body_A->get_space()->get_static_global_body();
  1103. }
  1104. GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
  1105. ERR_FAIL_COND(!body_B);
  1106. ERR_FAIL_COND(body_A == body_B);
  1107. GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
  1108. ERR_FAIL_COND(prev_joint == nullptr);
  1109. GodotJoint3D *joint = memnew(GodotConeTwistJoint3D(body_A, body_B, p_local_frame_A, p_local_frame_B));
  1110. joint->copy_settings_from(prev_joint);
  1111. joint_owner.replace(p_joint, joint);
  1112. memdelete(prev_joint);
  1113. }
  1114. void GodotPhysicsServer3D::cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, real_t p_value) {
  1115. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1116. ERR_FAIL_COND(!joint);
  1117. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_CONE_TWIST);
  1118. GodotConeTwistJoint3D *cone_twist_joint = static_cast<GodotConeTwistJoint3D *>(joint);
  1119. cone_twist_joint->set_param(p_param, p_value);
  1120. }
  1121. real_t GodotPhysicsServer3D::cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const {
  1122. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1123. ERR_FAIL_COND_V(!joint, 0);
  1124. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, 0);
  1125. GodotConeTwistJoint3D *cone_twist_joint = static_cast<GodotConeTwistJoint3D *>(joint);
  1126. return cone_twist_joint->get_param(p_param);
  1127. }
  1128. void GodotPhysicsServer3D::joint_make_generic_6dof(RID p_joint, RID p_body_A, const Transform3D &p_local_frame_A, RID p_body_B, const Transform3D &p_local_frame_B) {
  1129. GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
  1130. ERR_FAIL_COND(!body_A);
  1131. if (!p_body_B.is_valid()) {
  1132. ERR_FAIL_COND(!body_A->get_space());
  1133. p_body_B = body_A->get_space()->get_static_global_body();
  1134. }
  1135. GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
  1136. ERR_FAIL_COND(!body_B);
  1137. ERR_FAIL_COND(body_A == body_B);
  1138. GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
  1139. ERR_FAIL_COND(prev_joint == nullptr);
  1140. GodotJoint3D *joint = memnew(GodotGeneric6DOFJoint3D(body_A, body_B, p_local_frame_A, p_local_frame_B, true));
  1141. joint->copy_settings_from(prev_joint);
  1142. joint_owner.replace(p_joint, joint);
  1143. memdelete(prev_joint);
  1144. }
  1145. void GodotPhysicsServer3D::generic_6dof_joint_set_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param, real_t p_value) {
  1146. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1147. ERR_FAIL_COND(!joint);
  1148. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_6DOF);
  1149. GodotGeneric6DOFJoint3D *generic_6dof_joint = static_cast<GodotGeneric6DOFJoint3D *>(joint);
  1150. generic_6dof_joint->set_param(p_axis, p_param, p_value);
  1151. }
  1152. real_t GodotPhysicsServer3D::generic_6dof_joint_get_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param) const {
  1153. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1154. ERR_FAIL_COND_V(!joint, 0);
  1155. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, 0);
  1156. GodotGeneric6DOFJoint3D *generic_6dof_joint = static_cast<GodotGeneric6DOFJoint3D *>(joint);
  1157. return generic_6dof_joint->get_param(p_axis, p_param);
  1158. }
  1159. void GodotPhysicsServer3D::generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag, bool p_enable) {
  1160. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1161. ERR_FAIL_COND(!joint);
  1162. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_6DOF);
  1163. GodotGeneric6DOFJoint3D *generic_6dof_joint = static_cast<GodotGeneric6DOFJoint3D *>(joint);
  1164. generic_6dof_joint->set_flag(p_axis, p_flag, p_enable);
  1165. }
  1166. bool GodotPhysicsServer3D::generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag) const {
  1167. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1168. ERR_FAIL_COND_V(!joint, false);
  1169. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, false);
  1170. GodotGeneric6DOFJoint3D *generic_6dof_joint = static_cast<GodotGeneric6DOFJoint3D *>(joint);
  1171. return generic_6dof_joint->get_flag(p_axis, p_flag);
  1172. }
  1173. void GodotPhysicsServer3D::free(RID p_rid) {
  1174. _update_shapes(); //just in case
  1175. if (shape_owner.owns(p_rid)) {
  1176. GodotShape3D *shape = shape_owner.get_or_null(p_rid);
  1177. while (shape->get_owners().size()) {
  1178. GodotShapeOwner3D *so = shape->get_owners().front()->key();
  1179. so->remove_shape(shape);
  1180. }
  1181. shape_owner.free(p_rid);
  1182. memdelete(shape);
  1183. } else if (body_owner.owns(p_rid)) {
  1184. GodotBody3D *body = body_owner.get_or_null(p_rid);
  1185. /*
  1186. if (body->get_state_query())
  1187. _clear_query(body->get_state_query());
  1188. if (body->get_direct_state_query())
  1189. _clear_query(body->get_direct_state_query());
  1190. */
  1191. body->set_space(nullptr);
  1192. while (body->get_shape_count()) {
  1193. body->remove_shape(0);
  1194. }
  1195. body_owner.free(p_rid);
  1196. memdelete(body);
  1197. } else if (soft_body_owner.owns(p_rid)) {
  1198. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_rid);
  1199. soft_body->set_space(nullptr);
  1200. soft_body_owner.free(p_rid);
  1201. memdelete(soft_body);
  1202. } else if (area_owner.owns(p_rid)) {
  1203. GodotArea3D *area = area_owner.get_or_null(p_rid);
  1204. /*
  1205. if (area->get_monitor_query())
  1206. _clear_query(area->get_monitor_query());
  1207. */
  1208. area->set_space(nullptr);
  1209. while (area->get_shape_count()) {
  1210. area->remove_shape(0);
  1211. }
  1212. area_owner.free(p_rid);
  1213. memdelete(area);
  1214. } else if (space_owner.owns(p_rid)) {
  1215. GodotSpace3D *space = space_owner.get_or_null(p_rid);
  1216. while (space->get_objects().size()) {
  1217. GodotCollisionObject3D *co = (GodotCollisionObject3D *)space->get_objects().front()->get();
  1218. co->set_space(nullptr);
  1219. }
  1220. active_spaces.erase(space);
  1221. free(space->get_default_area()->get_self());
  1222. free(space->get_static_global_body());
  1223. space_owner.free(p_rid);
  1224. memdelete(space);
  1225. } else if (joint_owner.owns(p_rid)) {
  1226. GodotJoint3D *joint = joint_owner.get_or_null(p_rid);
  1227. joint_owner.free(p_rid);
  1228. memdelete(joint);
  1229. } else {
  1230. ERR_FAIL_MSG("Invalid ID.");
  1231. }
  1232. };
  1233. void GodotPhysicsServer3D::set_active(bool p_active) {
  1234. active = p_active;
  1235. };
  1236. void GodotPhysicsServer3D::set_collision_iterations(int p_iterations) {
  1237. iterations = p_iterations;
  1238. };
  1239. void GodotPhysicsServer3D::init() {
  1240. iterations = 8; // 8?
  1241. stepper = memnew(GodotStep3D);
  1242. };
  1243. void GodotPhysicsServer3D::step(real_t p_step) {
  1244. #ifndef _3D_DISABLED
  1245. if (!active) {
  1246. return;
  1247. }
  1248. _update_shapes();
  1249. island_count = 0;
  1250. active_objects = 0;
  1251. collision_pairs = 0;
  1252. for (Set<const GodotSpace3D *>::Element *E = active_spaces.front(); E; E = E->next()) {
  1253. stepper->step((GodotSpace3D *)E->get(), p_step, iterations);
  1254. island_count += E->get()->get_island_count();
  1255. active_objects += E->get()->get_active_objects();
  1256. collision_pairs += E->get()->get_collision_pairs();
  1257. }
  1258. #endif
  1259. }
  1260. void GodotPhysicsServer3D::sync() {
  1261. doing_sync = true;
  1262. };
  1263. void GodotPhysicsServer3D::flush_queries() {
  1264. #ifndef _3D_DISABLED
  1265. if (!active) {
  1266. return;
  1267. }
  1268. flushing_queries = true;
  1269. uint64_t time_beg = OS::get_singleton()->get_ticks_usec();
  1270. for (Set<const GodotSpace3D *>::Element *E = active_spaces.front(); E; E = E->next()) {
  1271. GodotSpace3D *space = (GodotSpace3D *)E->get();
  1272. space->call_queries();
  1273. }
  1274. flushing_queries = false;
  1275. if (EngineDebugger::is_profiling("servers")) {
  1276. uint64_t total_time[GodotSpace3D::ELAPSED_TIME_MAX];
  1277. static const char *time_name[GodotSpace3D::ELAPSED_TIME_MAX] = {
  1278. "integrate_forces",
  1279. "generate_islands",
  1280. "setup_constraints",
  1281. "solve_constraints",
  1282. "integrate_velocities"
  1283. };
  1284. for (int i = 0; i < GodotSpace3D::ELAPSED_TIME_MAX; i++) {
  1285. total_time[i] = 0;
  1286. }
  1287. for (Set<const GodotSpace3D *>::Element *E = active_spaces.front(); E; E = E->next()) {
  1288. for (int i = 0; i < GodotSpace3D::ELAPSED_TIME_MAX; i++) {
  1289. total_time[i] += E->get()->get_elapsed_time(GodotSpace3D::ElapsedTime(i));
  1290. }
  1291. }
  1292. Array values;
  1293. values.resize(GodotSpace3D::ELAPSED_TIME_MAX * 2);
  1294. for (int i = 0; i < GodotSpace3D::ELAPSED_TIME_MAX; i++) {
  1295. values[i * 2 + 0] = time_name[i];
  1296. values[i * 2 + 1] = USEC_TO_SEC(total_time[i]);
  1297. }
  1298. values.push_back("flush_queries");
  1299. values.push_back(USEC_TO_SEC(OS::get_singleton()->get_ticks_usec() - time_beg));
  1300. values.push_front("physics");
  1301. EngineDebugger::profiler_add_frame_data("servers", values);
  1302. }
  1303. #endif
  1304. };
  1305. void GodotPhysicsServer3D::end_sync() {
  1306. doing_sync = false;
  1307. };
  1308. void GodotPhysicsServer3D::finish() {
  1309. memdelete(stepper);
  1310. };
  1311. int GodotPhysicsServer3D::get_process_info(ProcessInfo p_info) {
  1312. switch (p_info) {
  1313. case INFO_ACTIVE_OBJECTS: {
  1314. return active_objects;
  1315. } break;
  1316. case INFO_COLLISION_PAIRS: {
  1317. return collision_pairs;
  1318. } break;
  1319. case INFO_ISLAND_COUNT: {
  1320. return island_count;
  1321. } break;
  1322. }
  1323. return 0;
  1324. }
  1325. void GodotPhysicsServer3D::_update_shapes() {
  1326. while (pending_shape_update_list.first()) {
  1327. pending_shape_update_list.first()->self()->_shape_changed();
  1328. pending_shape_update_list.remove(pending_shape_update_list.first());
  1329. }
  1330. }
  1331. void GodotPhysicsServer3D::_shape_col_cbk(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, void *p_userdata) {
  1332. CollCbkData *cbk = (CollCbkData *)p_userdata;
  1333. if (cbk->max == 0) {
  1334. return;
  1335. }
  1336. if (cbk->amount == cbk->max) {
  1337. //find least deep
  1338. real_t min_depth = 1e20;
  1339. int min_depth_idx = 0;
  1340. for (int i = 0; i < cbk->amount; i++) {
  1341. real_t d = cbk->ptr[i * 2 + 0].distance_squared_to(cbk->ptr[i * 2 + 1]);
  1342. if (d < min_depth) {
  1343. min_depth = d;
  1344. min_depth_idx = i;
  1345. }
  1346. }
  1347. real_t d = p_point_A.distance_squared_to(p_point_B);
  1348. if (d < min_depth) {
  1349. return;
  1350. }
  1351. cbk->ptr[min_depth_idx * 2 + 0] = p_point_A;
  1352. cbk->ptr[min_depth_idx * 2 + 1] = p_point_B;
  1353. } else {
  1354. cbk->ptr[cbk->amount * 2 + 0] = p_point_A;
  1355. cbk->ptr[cbk->amount * 2 + 1] = p_point_B;
  1356. cbk->amount++;
  1357. }
  1358. }
  1359. GodotPhysicsServer3D *GodotPhysicsServer3D::godot_singleton = nullptr;
  1360. GodotPhysicsServer3D::GodotPhysicsServer3D(bool p_using_threads) {
  1361. godot_singleton = this;
  1362. GodotBroadPhase3D::create_func = GodotBroadPhase3DBVH::_create;
  1363. using_threads = p_using_threads;
  1364. };