godot_physics_server_3d.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  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) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_NULL(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_NULL(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_NULL_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_NULL_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_NULL_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_NULL_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_NULL(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_NULL_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_NULL(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_NULL_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_NULL_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_NULL(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_NULL_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_NULL_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_NULL(area);
  200. GodotSpace3D *space = nullptr;
  201. if (p_space.is_valid()) {
  202. space = space_owner.get_or_null(p_space);
  203. ERR_FAIL_NULL(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_NULL_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_add_shape(RID p_area, RID p_shape, const Transform3D &p_transform, bool p_disabled) {
  221. GodotArea3D *area = area_owner.get_or_null(p_area);
  222. ERR_FAIL_NULL(area);
  223. GodotShape3D *shape = shape_owner.get_or_null(p_shape);
  224. ERR_FAIL_NULL(shape);
  225. area->add_shape(shape, p_transform, p_disabled);
  226. }
  227. void GodotPhysicsServer3D::area_set_shape(RID p_area, int p_shape_idx, RID p_shape) {
  228. GodotArea3D *area = area_owner.get_or_null(p_area);
  229. ERR_FAIL_NULL(area);
  230. GodotShape3D *shape = shape_owner.get_or_null(p_shape);
  231. ERR_FAIL_NULL(shape);
  232. ERR_FAIL_COND(!shape->is_configured());
  233. area->set_shape(p_shape_idx, shape);
  234. }
  235. void GodotPhysicsServer3D::area_set_shape_transform(RID p_area, int p_shape_idx, const Transform3D &p_transform) {
  236. GodotArea3D *area = area_owner.get_or_null(p_area);
  237. ERR_FAIL_NULL(area);
  238. area->set_shape_transform(p_shape_idx, p_transform);
  239. }
  240. int GodotPhysicsServer3D::area_get_shape_count(RID p_area) const {
  241. GodotArea3D *area = area_owner.get_or_null(p_area);
  242. ERR_FAIL_NULL_V(area, -1);
  243. return area->get_shape_count();
  244. }
  245. RID GodotPhysicsServer3D::area_get_shape(RID p_area, int p_shape_idx) const {
  246. GodotArea3D *area = area_owner.get_or_null(p_area);
  247. ERR_FAIL_NULL_V(area, RID());
  248. GodotShape3D *shape = area->get_shape(p_shape_idx);
  249. ERR_FAIL_NULL_V(shape, RID());
  250. return shape->get_self();
  251. }
  252. Transform3D GodotPhysicsServer3D::area_get_shape_transform(RID p_area, int p_shape_idx) const {
  253. GodotArea3D *area = area_owner.get_or_null(p_area);
  254. ERR_FAIL_NULL_V(area, Transform3D());
  255. return area->get_shape_transform(p_shape_idx);
  256. }
  257. void GodotPhysicsServer3D::area_remove_shape(RID p_area, int p_shape_idx) {
  258. GodotArea3D *area = area_owner.get_or_null(p_area);
  259. ERR_FAIL_NULL(area);
  260. area->remove_shape(p_shape_idx);
  261. }
  262. void GodotPhysicsServer3D::area_clear_shapes(RID p_area) {
  263. GodotArea3D *area = area_owner.get_or_null(p_area);
  264. ERR_FAIL_NULL(area);
  265. while (area->get_shape_count()) {
  266. area->remove_shape(0);
  267. }
  268. }
  269. void GodotPhysicsServer3D::area_set_shape_disabled(RID p_area, int p_shape_idx, bool p_disabled) {
  270. GodotArea3D *area = area_owner.get_or_null(p_area);
  271. ERR_FAIL_NULL(area);
  272. ERR_FAIL_INDEX(p_shape_idx, area->get_shape_count());
  273. FLUSH_QUERY_CHECK(area);
  274. area->set_shape_disabled(p_shape_idx, p_disabled);
  275. }
  276. void GodotPhysicsServer3D::area_attach_object_instance_id(RID p_area, ObjectID p_id) {
  277. if (space_owner.owns(p_area)) {
  278. GodotSpace3D *space = space_owner.get_or_null(p_area);
  279. p_area = space->get_default_area()->get_self();
  280. }
  281. GodotArea3D *area = area_owner.get_or_null(p_area);
  282. ERR_FAIL_NULL(area);
  283. area->set_instance_id(p_id);
  284. }
  285. ObjectID GodotPhysicsServer3D::area_get_object_instance_id(RID p_area) const {
  286. if (space_owner.owns(p_area)) {
  287. GodotSpace3D *space = space_owner.get_or_null(p_area);
  288. p_area = space->get_default_area()->get_self();
  289. }
  290. GodotArea3D *area = area_owner.get_or_null(p_area);
  291. ERR_FAIL_NULL_V(area, ObjectID());
  292. return area->get_instance_id();
  293. }
  294. void GodotPhysicsServer3D::area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) {
  295. if (space_owner.owns(p_area)) {
  296. GodotSpace3D *space = space_owner.get_or_null(p_area);
  297. p_area = space->get_default_area()->get_self();
  298. }
  299. GodotArea3D *area = area_owner.get_or_null(p_area);
  300. ERR_FAIL_NULL(area);
  301. area->set_param(p_param, p_value);
  302. }
  303. void GodotPhysicsServer3D::area_set_transform(RID p_area, const Transform3D &p_transform) {
  304. GodotArea3D *area = area_owner.get_or_null(p_area);
  305. ERR_FAIL_NULL(area);
  306. area->set_transform(p_transform);
  307. }
  308. Variant GodotPhysicsServer3D::area_get_param(RID p_area, AreaParameter p_param) const {
  309. if (space_owner.owns(p_area)) {
  310. GodotSpace3D *space = space_owner.get_or_null(p_area);
  311. p_area = space->get_default_area()->get_self();
  312. }
  313. GodotArea3D *area = area_owner.get_or_null(p_area);
  314. ERR_FAIL_NULL_V(area, Variant());
  315. return area->get_param(p_param);
  316. }
  317. Transform3D GodotPhysicsServer3D::area_get_transform(RID p_area) const {
  318. GodotArea3D *area = area_owner.get_or_null(p_area);
  319. ERR_FAIL_NULL_V(area, Transform3D());
  320. return area->get_transform();
  321. }
  322. void GodotPhysicsServer3D::area_set_collision_layer(RID p_area, uint32_t p_layer) {
  323. GodotArea3D *area = area_owner.get_or_null(p_area);
  324. ERR_FAIL_NULL(area);
  325. area->set_collision_layer(p_layer);
  326. }
  327. uint32_t GodotPhysicsServer3D::area_get_collision_layer(RID p_area) const {
  328. GodotArea3D *area = area_owner.get_or_null(p_area);
  329. ERR_FAIL_NULL_V(area, 0);
  330. return area->get_collision_layer();
  331. }
  332. void GodotPhysicsServer3D::area_set_collision_mask(RID p_area, uint32_t p_mask) {
  333. GodotArea3D *area = area_owner.get_or_null(p_area);
  334. ERR_FAIL_NULL(area);
  335. area->set_collision_mask(p_mask);
  336. }
  337. uint32_t GodotPhysicsServer3D::area_get_collision_mask(RID p_area) const {
  338. GodotArea3D *area = area_owner.get_or_null(p_area);
  339. ERR_FAIL_NULL_V(area, 0);
  340. return area->get_collision_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_NULL(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_NULL(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_NULL(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_NULL(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_NULL(body);
  373. GodotSpace3D *space = nullptr;
  374. if (p_space.is_valid()) {
  375. space = space_owner.get_or_null(p_space);
  376. ERR_FAIL_NULL(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_NULL_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_NULL(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_NULL_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_NULL(body);
  406. GodotShape3D *shape = shape_owner.get_or_null(p_shape);
  407. ERR_FAIL_NULL(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_NULL(body);
  413. GodotShape3D *shape = shape_owner.get_or_null(p_shape);
  414. ERR_FAIL_NULL(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_NULL(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_NULL_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_NULL_V(body, RID());
  431. GodotShape3D *shape = body->get_shape(p_shape_idx);
  432. ERR_FAIL_NULL_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_NULL(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_NULL_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_NULL(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_NULL(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_NULL(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_NULL_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_NULL(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_NULL_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_NULL(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_NULL_V(body, 0);
  487. return body->get_collision_mask();
  488. }
  489. void GodotPhysicsServer3D::body_set_collision_priority(RID p_body, real_t p_priority) {
  490. GodotBody3D *body = body_owner.get_or_null(p_body);
  491. ERR_FAIL_NULL(body);
  492. body->set_collision_priority(p_priority);
  493. }
  494. real_t GodotPhysicsServer3D::body_get_collision_priority(RID p_body) const {
  495. const GodotBody3D *body = body_owner.get_or_null(p_body);
  496. ERR_FAIL_NULL_V(body, 0);
  497. return body->get_collision_priority();
  498. }
  499. void GodotPhysicsServer3D::body_attach_object_instance_id(RID p_body, ObjectID p_id) {
  500. GodotBody3D *body = body_owner.get_or_null(p_body);
  501. if (body) {
  502. body->set_instance_id(p_id);
  503. return;
  504. }
  505. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  506. if (soft_body) {
  507. soft_body->set_instance_id(p_id);
  508. return;
  509. }
  510. ERR_FAIL_MSG("Invalid ID.");
  511. }
  512. ObjectID GodotPhysicsServer3D::body_get_object_instance_id(RID p_body) const {
  513. GodotBody3D *body = body_owner.get_or_null(p_body);
  514. ERR_FAIL_NULL_V(body, ObjectID());
  515. return body->get_instance_id();
  516. }
  517. void GodotPhysicsServer3D::body_set_user_flags(RID p_body, uint32_t p_flags) {
  518. GodotBody3D *body = body_owner.get_or_null(p_body);
  519. ERR_FAIL_NULL(body);
  520. }
  521. uint32_t GodotPhysicsServer3D::body_get_user_flags(RID p_body) const {
  522. GodotBody3D *body = body_owner.get_or_null(p_body);
  523. ERR_FAIL_NULL_V(body, 0);
  524. return 0;
  525. }
  526. void GodotPhysicsServer3D::body_set_param(RID p_body, BodyParameter p_param, const Variant &p_value) {
  527. GodotBody3D *body = body_owner.get_or_null(p_body);
  528. ERR_FAIL_NULL(body);
  529. body->set_param(p_param, p_value);
  530. }
  531. Variant GodotPhysicsServer3D::body_get_param(RID p_body, BodyParameter p_param) const {
  532. GodotBody3D *body = body_owner.get_or_null(p_body);
  533. ERR_FAIL_NULL_V(body, 0);
  534. return body->get_param(p_param);
  535. }
  536. void GodotPhysicsServer3D::body_reset_mass_properties(RID p_body) {
  537. GodotBody3D *body = body_owner.get_or_null(p_body);
  538. ERR_FAIL_NULL(body);
  539. return body->reset_mass_properties();
  540. }
  541. void GodotPhysicsServer3D::body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
  542. GodotBody3D *body = body_owner.get_or_null(p_body);
  543. ERR_FAIL_NULL(body);
  544. body->set_state(p_state, p_variant);
  545. }
  546. Variant GodotPhysicsServer3D::body_get_state(RID p_body, BodyState p_state) const {
  547. GodotBody3D *body = body_owner.get_or_null(p_body);
  548. ERR_FAIL_NULL_V(body, Variant());
  549. return body->get_state(p_state);
  550. }
  551. void GodotPhysicsServer3D::body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) {
  552. GodotBody3D *body = body_owner.get_or_null(p_body);
  553. ERR_FAIL_NULL(body);
  554. _update_shapes();
  555. body->apply_central_impulse(p_impulse);
  556. body->wakeup();
  557. }
  558. void GodotPhysicsServer3D::body_apply_impulse(RID p_body, const Vector3 &p_impulse, const Vector3 &p_position) {
  559. GodotBody3D *body = body_owner.get_or_null(p_body);
  560. ERR_FAIL_NULL(body);
  561. _update_shapes();
  562. body->apply_impulse(p_impulse, p_position);
  563. body->wakeup();
  564. }
  565. void GodotPhysicsServer3D::body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse) {
  566. GodotBody3D *body = body_owner.get_or_null(p_body);
  567. ERR_FAIL_NULL(body);
  568. _update_shapes();
  569. body->apply_torque_impulse(p_impulse);
  570. body->wakeup();
  571. }
  572. void GodotPhysicsServer3D::body_apply_central_force(RID p_body, const Vector3 &p_force) {
  573. GodotBody3D *body = body_owner.get_or_null(p_body);
  574. ERR_FAIL_NULL(body);
  575. body->apply_central_force(p_force);
  576. body->wakeup();
  577. }
  578. void GodotPhysicsServer3D::body_apply_force(RID p_body, const Vector3 &p_force, const Vector3 &p_position) {
  579. GodotBody3D *body = body_owner.get_or_null(p_body);
  580. ERR_FAIL_NULL(body);
  581. body->apply_force(p_force, p_position);
  582. body->wakeup();
  583. }
  584. void GodotPhysicsServer3D::body_apply_torque(RID p_body, const Vector3 &p_torque) {
  585. GodotBody3D *body = body_owner.get_or_null(p_body);
  586. ERR_FAIL_NULL(body);
  587. body->apply_torque(p_torque);
  588. body->wakeup();
  589. }
  590. void GodotPhysicsServer3D::body_add_constant_central_force(RID p_body, const Vector3 &p_force) {
  591. GodotBody3D *body = body_owner.get_or_null(p_body);
  592. ERR_FAIL_NULL(body);
  593. body->add_constant_central_force(p_force);
  594. body->wakeup();
  595. }
  596. void GodotPhysicsServer3D::body_add_constant_force(RID p_body, const Vector3 &p_force, const Vector3 &p_position) {
  597. GodotBody3D *body = body_owner.get_or_null(p_body);
  598. ERR_FAIL_NULL(body);
  599. body->add_constant_force(p_force, p_position);
  600. body->wakeup();
  601. }
  602. void GodotPhysicsServer3D::body_add_constant_torque(RID p_body, const Vector3 &p_torque) {
  603. GodotBody3D *body = body_owner.get_or_null(p_body);
  604. ERR_FAIL_NULL(body);
  605. body->add_constant_torque(p_torque);
  606. body->wakeup();
  607. }
  608. void GodotPhysicsServer3D::body_set_constant_force(RID p_body, const Vector3 &p_force) {
  609. GodotBody3D *body = body_owner.get_or_null(p_body);
  610. ERR_FAIL_NULL(body);
  611. body->set_constant_force(p_force);
  612. if (!p_force.is_zero_approx()) {
  613. body->wakeup();
  614. }
  615. }
  616. Vector3 GodotPhysicsServer3D::body_get_constant_force(RID p_body) const {
  617. GodotBody3D *body = body_owner.get_or_null(p_body);
  618. ERR_FAIL_NULL_V(body, Vector3());
  619. return body->get_constant_force();
  620. }
  621. void GodotPhysicsServer3D::body_set_constant_torque(RID p_body, const Vector3 &p_torque) {
  622. GodotBody3D *body = body_owner.get_or_null(p_body);
  623. ERR_FAIL_NULL(body);
  624. body->set_constant_torque(p_torque);
  625. if (!p_torque.is_zero_approx()) {
  626. body->wakeup();
  627. }
  628. }
  629. Vector3 GodotPhysicsServer3D::body_get_constant_torque(RID p_body) const {
  630. GodotBody3D *body = body_owner.get_or_null(p_body);
  631. ERR_FAIL_NULL_V(body, Vector3());
  632. return body->get_constant_torque();
  633. }
  634. void GodotPhysicsServer3D::body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity) {
  635. GodotBody3D *body = body_owner.get_or_null(p_body);
  636. ERR_FAIL_NULL(body);
  637. _update_shapes();
  638. Vector3 v = body->get_linear_velocity();
  639. Vector3 axis = p_axis_velocity.normalized();
  640. v -= axis * axis.dot(v);
  641. v += p_axis_velocity;
  642. body->set_linear_velocity(v);
  643. body->wakeup();
  644. }
  645. void GodotPhysicsServer3D::body_set_axis_lock(RID p_body, BodyAxis p_axis, bool p_lock) {
  646. GodotBody3D *body = body_owner.get_or_null(p_body);
  647. ERR_FAIL_NULL(body);
  648. body->set_axis_lock(p_axis, p_lock);
  649. body->wakeup();
  650. }
  651. bool GodotPhysicsServer3D::body_is_axis_locked(RID p_body, BodyAxis p_axis) const {
  652. const GodotBody3D *body = body_owner.get_or_null(p_body);
  653. ERR_FAIL_NULL_V(body, false);
  654. return body->is_axis_locked(p_axis);
  655. }
  656. void GodotPhysicsServer3D::body_add_collision_exception(RID p_body, RID p_body_b) {
  657. GodotBody3D *body = body_owner.get_or_null(p_body);
  658. ERR_FAIL_NULL(body);
  659. body->add_exception(p_body_b);
  660. body->wakeup();
  661. }
  662. void GodotPhysicsServer3D::body_remove_collision_exception(RID p_body, RID p_body_b) {
  663. GodotBody3D *body = body_owner.get_or_null(p_body);
  664. ERR_FAIL_NULL(body);
  665. body->remove_exception(p_body_b);
  666. body->wakeup();
  667. }
  668. void GodotPhysicsServer3D::body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  669. GodotBody3D *body = body_owner.get_or_null(p_body);
  670. ERR_FAIL_NULL(body);
  671. for (int i = 0; i < body->get_exceptions().size(); i++) {
  672. p_exceptions->push_back(body->get_exceptions()[i]);
  673. }
  674. }
  675. void GodotPhysicsServer3D::body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) {
  676. GodotBody3D *body = body_owner.get_or_null(p_body);
  677. ERR_FAIL_NULL(body);
  678. }
  679. real_t GodotPhysicsServer3D::body_get_contacts_reported_depth_threshold(RID p_body) const {
  680. GodotBody3D *body = body_owner.get_or_null(p_body);
  681. ERR_FAIL_NULL_V(body, 0);
  682. return 0;
  683. }
  684. void GodotPhysicsServer3D::body_set_omit_force_integration(RID p_body, bool p_omit) {
  685. GodotBody3D *body = body_owner.get_or_null(p_body);
  686. ERR_FAIL_NULL(body);
  687. body->set_omit_force_integration(p_omit);
  688. }
  689. bool GodotPhysicsServer3D::body_is_omitting_force_integration(RID p_body) const {
  690. GodotBody3D *body = body_owner.get_or_null(p_body);
  691. ERR_FAIL_NULL_V(body, false);
  692. return body->get_omit_force_integration();
  693. }
  694. void GodotPhysicsServer3D::body_set_max_contacts_reported(RID p_body, int p_contacts) {
  695. GodotBody3D *body = body_owner.get_or_null(p_body);
  696. ERR_FAIL_NULL(body);
  697. body->set_max_contacts_reported(p_contacts);
  698. }
  699. int GodotPhysicsServer3D::body_get_max_contacts_reported(RID p_body) const {
  700. GodotBody3D *body = body_owner.get_or_null(p_body);
  701. ERR_FAIL_NULL_V(body, -1);
  702. return body->get_max_contacts_reported();
  703. }
  704. void GodotPhysicsServer3D::body_set_state_sync_callback(RID p_body, const Callable &p_callable) {
  705. GodotBody3D *body = body_owner.get_or_null(p_body);
  706. ERR_FAIL_NULL(body);
  707. body->set_state_sync_callback(p_callable);
  708. }
  709. void GodotPhysicsServer3D::body_set_force_integration_callback(RID p_body, const Callable &p_callable, const Variant &p_udata) {
  710. GodotBody3D *body = body_owner.get_or_null(p_body);
  711. ERR_FAIL_NULL(body);
  712. body->set_force_integration_callback(p_callable, p_udata);
  713. }
  714. void GodotPhysicsServer3D::body_set_ray_pickable(RID p_body, bool p_enable) {
  715. GodotBody3D *body = body_owner.get_or_null(p_body);
  716. ERR_FAIL_NULL(body);
  717. body->set_ray_pickable(p_enable);
  718. }
  719. bool GodotPhysicsServer3D::body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result) {
  720. GodotBody3D *body = body_owner.get_or_null(p_body);
  721. ERR_FAIL_NULL_V(body, false);
  722. ERR_FAIL_NULL_V(body->get_space(), false);
  723. ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
  724. _update_shapes();
  725. return body->get_space()->test_body_motion(body, p_parameters, r_result);
  726. }
  727. PhysicsDirectBodyState3D *GodotPhysicsServer3D::body_get_direct_state(RID p_body) {
  728. ERR_FAIL_COND_V_MSG((using_threads && !doing_sync), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
  729. if (!body_owner.owns(p_body)) {
  730. return nullptr;
  731. }
  732. GodotBody3D *body = body_owner.get_or_null(p_body);
  733. ERR_FAIL_NULL_V(body, nullptr);
  734. if (!body->get_space()) {
  735. return nullptr;
  736. }
  737. ERR_FAIL_COND_V_MSG(body->get_space()->is_locked(), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
  738. return body->get_direct_state();
  739. }
  740. /* SOFT BODY */
  741. RID GodotPhysicsServer3D::soft_body_create() {
  742. GodotSoftBody3D *soft_body = memnew(GodotSoftBody3D);
  743. RID rid = soft_body_owner.make_rid(soft_body);
  744. soft_body->set_self(rid);
  745. return rid;
  746. }
  747. void GodotPhysicsServer3D::soft_body_update_rendering_server(RID p_body, PhysicsServer3DRenderingServerHandler *p_rendering_server_handler) {
  748. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  749. ERR_FAIL_NULL(soft_body);
  750. soft_body->update_rendering_server(p_rendering_server_handler);
  751. }
  752. void GodotPhysicsServer3D::soft_body_set_space(RID p_body, RID p_space) {
  753. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  754. ERR_FAIL_NULL(soft_body);
  755. GodotSpace3D *space = nullptr;
  756. if (p_space.is_valid()) {
  757. space = space_owner.get_or_null(p_space);
  758. ERR_FAIL_NULL(space);
  759. }
  760. if (soft_body->get_space() == space) {
  761. return;
  762. }
  763. soft_body->set_space(space);
  764. }
  765. RID GodotPhysicsServer3D::soft_body_get_space(RID p_body) const {
  766. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  767. ERR_FAIL_NULL_V(soft_body, RID());
  768. GodotSpace3D *space = soft_body->get_space();
  769. if (!space) {
  770. return RID();
  771. }
  772. return space->get_self();
  773. }
  774. void GodotPhysicsServer3D::soft_body_set_collision_layer(RID p_body, uint32_t p_layer) {
  775. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  776. ERR_FAIL_NULL(soft_body);
  777. soft_body->set_collision_layer(p_layer);
  778. }
  779. uint32_t GodotPhysicsServer3D::soft_body_get_collision_layer(RID p_body) const {
  780. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  781. ERR_FAIL_NULL_V(soft_body, 0);
  782. return soft_body->get_collision_layer();
  783. }
  784. void GodotPhysicsServer3D::soft_body_set_collision_mask(RID p_body, uint32_t p_mask) {
  785. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  786. ERR_FAIL_NULL(soft_body);
  787. soft_body->set_collision_mask(p_mask);
  788. }
  789. uint32_t GodotPhysicsServer3D::soft_body_get_collision_mask(RID p_body) const {
  790. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  791. ERR_FAIL_NULL_V(soft_body, 0);
  792. return soft_body->get_collision_mask();
  793. }
  794. void GodotPhysicsServer3D::soft_body_add_collision_exception(RID p_body, RID p_body_b) {
  795. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  796. ERR_FAIL_NULL(soft_body);
  797. soft_body->add_exception(p_body_b);
  798. }
  799. void GodotPhysicsServer3D::soft_body_remove_collision_exception(RID p_body, RID p_body_b) {
  800. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  801. ERR_FAIL_NULL(soft_body);
  802. soft_body->remove_exception(p_body_b);
  803. }
  804. void GodotPhysicsServer3D::soft_body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  805. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  806. ERR_FAIL_NULL(soft_body);
  807. for (int i = 0; i < soft_body->get_exceptions().size(); i++) {
  808. p_exceptions->push_back(soft_body->get_exceptions()[i]);
  809. }
  810. }
  811. void GodotPhysicsServer3D::soft_body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
  812. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  813. ERR_FAIL_NULL(soft_body);
  814. soft_body->set_state(p_state, p_variant);
  815. }
  816. Variant GodotPhysicsServer3D::soft_body_get_state(RID p_body, BodyState p_state) const {
  817. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  818. ERR_FAIL_NULL_V(soft_body, Variant());
  819. return soft_body->get_state(p_state);
  820. }
  821. void GodotPhysicsServer3D::soft_body_set_transform(RID p_body, const Transform3D &p_transform) {
  822. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  823. ERR_FAIL_NULL(soft_body);
  824. soft_body->set_state(BODY_STATE_TRANSFORM, p_transform);
  825. }
  826. void GodotPhysicsServer3D::soft_body_set_ray_pickable(RID p_body, bool p_enable) {
  827. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  828. ERR_FAIL_NULL(soft_body);
  829. soft_body->set_ray_pickable(p_enable);
  830. }
  831. void GodotPhysicsServer3D::soft_body_set_simulation_precision(RID p_body, int p_simulation_precision) {
  832. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  833. ERR_FAIL_NULL(soft_body);
  834. soft_body->set_iteration_count(p_simulation_precision);
  835. }
  836. int GodotPhysicsServer3D::soft_body_get_simulation_precision(RID p_body) const {
  837. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  838. ERR_FAIL_NULL_V(soft_body, 0.f);
  839. return soft_body->get_iteration_count();
  840. }
  841. void GodotPhysicsServer3D::soft_body_set_total_mass(RID p_body, real_t p_total_mass) {
  842. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  843. ERR_FAIL_NULL(soft_body);
  844. soft_body->set_total_mass(p_total_mass);
  845. }
  846. real_t GodotPhysicsServer3D::soft_body_get_total_mass(RID p_body) const {
  847. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  848. ERR_FAIL_NULL_V(soft_body, 0.f);
  849. return soft_body->get_total_mass();
  850. }
  851. void GodotPhysicsServer3D::soft_body_set_linear_stiffness(RID p_body, real_t p_stiffness) {
  852. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  853. ERR_FAIL_NULL(soft_body);
  854. soft_body->set_linear_stiffness(p_stiffness);
  855. }
  856. real_t GodotPhysicsServer3D::soft_body_get_linear_stiffness(RID p_body) const {
  857. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  858. ERR_FAIL_NULL_V(soft_body, 0.f);
  859. return soft_body->get_linear_stiffness();
  860. }
  861. void GodotPhysicsServer3D::soft_body_set_pressure_coefficient(RID p_body, real_t p_pressure_coefficient) {
  862. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  863. ERR_FAIL_NULL(soft_body);
  864. soft_body->set_pressure_coefficient(p_pressure_coefficient);
  865. }
  866. real_t GodotPhysicsServer3D::soft_body_get_pressure_coefficient(RID p_body) const {
  867. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  868. ERR_FAIL_NULL_V(soft_body, 0.f);
  869. return soft_body->get_pressure_coefficient();
  870. }
  871. void GodotPhysicsServer3D::soft_body_set_damping_coefficient(RID p_body, real_t p_damping_coefficient) {
  872. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  873. ERR_FAIL_NULL(soft_body);
  874. soft_body->set_damping_coefficient(p_damping_coefficient);
  875. }
  876. real_t GodotPhysicsServer3D::soft_body_get_damping_coefficient(RID p_body) const {
  877. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  878. ERR_FAIL_NULL_V(soft_body, 0.f);
  879. return soft_body->get_damping_coefficient();
  880. }
  881. void GodotPhysicsServer3D::soft_body_set_drag_coefficient(RID p_body, real_t p_drag_coefficient) {
  882. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  883. ERR_FAIL_NULL(soft_body);
  884. soft_body->set_drag_coefficient(p_drag_coefficient);
  885. }
  886. real_t GodotPhysicsServer3D::soft_body_get_drag_coefficient(RID p_body) const {
  887. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  888. ERR_FAIL_NULL_V(soft_body, 0.f);
  889. return soft_body->get_drag_coefficient();
  890. }
  891. void GodotPhysicsServer3D::soft_body_set_mesh(RID p_body, RID p_mesh) {
  892. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  893. ERR_FAIL_NULL(soft_body);
  894. soft_body->set_mesh(p_mesh);
  895. }
  896. AABB GodotPhysicsServer3D::soft_body_get_bounds(RID p_body) const {
  897. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  898. ERR_FAIL_NULL_V(soft_body, AABB());
  899. return soft_body->get_bounds();
  900. }
  901. void GodotPhysicsServer3D::soft_body_move_point(RID p_body, int p_point_index, const Vector3 &p_global_position) {
  902. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  903. ERR_FAIL_NULL(soft_body);
  904. soft_body->set_vertex_position(p_point_index, p_global_position);
  905. }
  906. Vector3 GodotPhysicsServer3D::soft_body_get_point_global_position(RID p_body, int p_point_index) const {
  907. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  908. ERR_FAIL_NULL_V(soft_body, Vector3());
  909. return soft_body->get_vertex_position(p_point_index);
  910. }
  911. void GodotPhysicsServer3D::soft_body_remove_all_pinned_points(RID p_body) {
  912. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  913. ERR_FAIL_NULL(soft_body);
  914. soft_body->unpin_all_vertices();
  915. }
  916. void GodotPhysicsServer3D::soft_body_pin_point(RID p_body, int p_point_index, bool p_pin) {
  917. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  918. ERR_FAIL_NULL(soft_body);
  919. if (p_pin) {
  920. soft_body->pin_vertex(p_point_index);
  921. } else {
  922. soft_body->unpin_vertex(p_point_index);
  923. }
  924. }
  925. bool GodotPhysicsServer3D::soft_body_is_point_pinned(RID p_body, int p_point_index) const {
  926. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
  927. ERR_FAIL_NULL_V(soft_body, false);
  928. return soft_body->is_vertex_pinned(p_point_index);
  929. }
  930. /* JOINT API */
  931. RID GodotPhysicsServer3D::joint_create() {
  932. GodotJoint3D *joint = memnew(GodotJoint3D);
  933. RID rid = joint_owner.make_rid(joint);
  934. joint->set_self(rid);
  935. return rid;
  936. }
  937. void GodotPhysicsServer3D::joint_clear(RID p_joint) {
  938. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  939. ERR_FAIL_NULL(joint);
  940. if (joint->get_type() != JOINT_TYPE_MAX) {
  941. GodotJoint3D *empty_joint = memnew(GodotJoint3D);
  942. empty_joint->copy_settings_from(joint);
  943. joint_owner.replace(p_joint, empty_joint);
  944. memdelete(joint);
  945. }
  946. }
  947. 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) {
  948. GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
  949. ERR_FAIL_NULL(body_A);
  950. if (!p_body_B.is_valid()) {
  951. ERR_FAIL_NULL(body_A->get_space());
  952. p_body_B = body_A->get_space()->get_static_global_body();
  953. }
  954. GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
  955. ERR_FAIL_NULL(body_B);
  956. ERR_FAIL_COND(body_A == body_B);
  957. GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
  958. ERR_FAIL_NULL(prev_joint);
  959. GodotJoint3D *joint = memnew(GodotPinJoint3D(body_A, p_local_A, body_B, p_local_B));
  960. joint->copy_settings_from(prev_joint);
  961. joint_owner.replace(p_joint, joint);
  962. memdelete(prev_joint);
  963. }
  964. void GodotPhysicsServer3D::pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) {
  965. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  966. ERR_FAIL_NULL(joint);
  967. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
  968. GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
  969. pin_joint->set_param(p_param, p_value);
  970. }
  971. real_t GodotPhysicsServer3D::pin_joint_get_param(RID p_joint, PinJointParam p_param) const {
  972. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  973. ERR_FAIL_NULL_V(joint, 0);
  974. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, 0);
  975. GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
  976. return pin_joint->get_param(p_param);
  977. }
  978. void GodotPhysicsServer3D::pin_joint_set_local_a(RID p_joint, const Vector3 &p_A) {
  979. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  980. ERR_FAIL_NULL(joint);
  981. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
  982. GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
  983. pin_joint->set_pos_a(p_A);
  984. }
  985. Vector3 GodotPhysicsServer3D::pin_joint_get_local_a(RID p_joint) const {
  986. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  987. ERR_FAIL_NULL_V(joint, Vector3());
  988. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, Vector3());
  989. GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
  990. return pin_joint->get_position_a();
  991. }
  992. void GodotPhysicsServer3D::pin_joint_set_local_b(RID p_joint, const Vector3 &p_B) {
  993. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  994. ERR_FAIL_NULL(joint);
  995. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
  996. GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
  997. pin_joint->set_pos_b(p_B);
  998. }
  999. Vector3 GodotPhysicsServer3D::pin_joint_get_local_b(RID p_joint) const {
  1000. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1001. ERR_FAIL_NULL_V(joint, Vector3());
  1002. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, Vector3());
  1003. GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
  1004. return pin_joint->get_position_b();
  1005. }
  1006. 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) {
  1007. GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
  1008. ERR_FAIL_NULL(body_A);
  1009. if (!p_body_B.is_valid()) {
  1010. ERR_FAIL_NULL(body_A->get_space());
  1011. p_body_B = body_A->get_space()->get_static_global_body();
  1012. }
  1013. GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
  1014. ERR_FAIL_NULL(body_B);
  1015. ERR_FAIL_COND(body_A == body_B);
  1016. GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
  1017. ERR_FAIL_NULL(prev_joint);
  1018. GodotJoint3D *joint = memnew(GodotHingeJoint3D(body_A, body_B, p_frame_A, p_frame_B));
  1019. joint->copy_settings_from(prev_joint);
  1020. joint_owner.replace(p_joint, joint);
  1021. memdelete(prev_joint);
  1022. }
  1023. 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) {
  1024. GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
  1025. ERR_FAIL_NULL(body_A);
  1026. if (!p_body_B.is_valid()) {
  1027. ERR_FAIL_NULL(body_A->get_space());
  1028. p_body_B = body_A->get_space()->get_static_global_body();
  1029. }
  1030. GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
  1031. ERR_FAIL_NULL(body_B);
  1032. ERR_FAIL_COND(body_A == body_B);
  1033. GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
  1034. ERR_FAIL_NULL(prev_joint);
  1035. GodotJoint3D *joint = memnew(GodotHingeJoint3D(body_A, body_B, p_pivot_A, p_pivot_B, p_axis_A, p_axis_B));
  1036. joint->copy_settings_from(prev_joint);
  1037. joint_owner.replace(p_joint, joint);
  1038. memdelete(prev_joint);
  1039. }
  1040. void GodotPhysicsServer3D::hinge_joint_set_param(RID p_joint, HingeJointParam p_param, real_t p_value) {
  1041. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1042. ERR_FAIL_NULL(joint);
  1043. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_HINGE);
  1044. GodotHingeJoint3D *hinge_joint = static_cast<GodotHingeJoint3D *>(joint);
  1045. hinge_joint->set_param(p_param, p_value);
  1046. }
  1047. real_t GodotPhysicsServer3D::hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const {
  1048. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1049. ERR_FAIL_NULL_V(joint, 0);
  1050. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, 0);
  1051. GodotHingeJoint3D *hinge_joint = static_cast<GodotHingeJoint3D *>(joint);
  1052. return hinge_joint->get_param(p_param);
  1053. }
  1054. void GodotPhysicsServer3D::hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_enabled) {
  1055. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1056. ERR_FAIL_NULL(joint);
  1057. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_HINGE);
  1058. GodotHingeJoint3D *hinge_joint = static_cast<GodotHingeJoint3D *>(joint);
  1059. hinge_joint->set_flag(p_flag, p_enabled);
  1060. }
  1061. bool GodotPhysicsServer3D::hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const {
  1062. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1063. ERR_FAIL_NULL_V(joint, false);
  1064. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, false);
  1065. GodotHingeJoint3D *hinge_joint = static_cast<GodotHingeJoint3D *>(joint);
  1066. return hinge_joint->get_flag(p_flag);
  1067. }
  1068. void GodotPhysicsServer3D::joint_set_solver_priority(RID p_joint, int p_priority) {
  1069. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1070. ERR_FAIL_NULL(joint);
  1071. joint->set_priority(p_priority);
  1072. }
  1073. int GodotPhysicsServer3D::joint_get_solver_priority(RID p_joint) const {
  1074. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1075. ERR_FAIL_NULL_V(joint, 0);
  1076. return joint->get_priority();
  1077. }
  1078. void GodotPhysicsServer3D::joint_disable_collisions_between_bodies(RID p_joint, bool p_disable) {
  1079. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1080. ERR_FAIL_NULL(joint);
  1081. joint->disable_collisions_between_bodies(p_disable);
  1082. if (2 == joint->get_body_count()) {
  1083. GodotBody3D *body_a = *joint->get_body_ptr();
  1084. GodotBody3D *body_b = *(joint->get_body_ptr() + 1);
  1085. if (p_disable) {
  1086. body_add_collision_exception(body_a->get_self(), body_b->get_self());
  1087. body_add_collision_exception(body_b->get_self(), body_a->get_self());
  1088. } else {
  1089. body_remove_collision_exception(body_a->get_self(), body_b->get_self());
  1090. body_remove_collision_exception(body_b->get_self(), body_a->get_self());
  1091. }
  1092. }
  1093. }
  1094. bool GodotPhysicsServer3D::joint_is_disabled_collisions_between_bodies(RID p_joint) const {
  1095. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1096. ERR_FAIL_NULL_V(joint, true);
  1097. return joint->is_disabled_collisions_between_bodies();
  1098. }
  1099. GodotPhysicsServer3D::JointType GodotPhysicsServer3D::joint_get_type(RID p_joint) const {
  1100. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1101. ERR_FAIL_NULL_V(joint, JOINT_TYPE_PIN);
  1102. return joint->get_type();
  1103. }
  1104. 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) {
  1105. GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
  1106. ERR_FAIL_NULL(body_A);
  1107. if (!p_body_B.is_valid()) {
  1108. ERR_FAIL_NULL(body_A->get_space());
  1109. p_body_B = body_A->get_space()->get_static_global_body();
  1110. }
  1111. GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
  1112. ERR_FAIL_NULL(body_B);
  1113. ERR_FAIL_COND(body_A == body_B);
  1114. GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
  1115. ERR_FAIL_NULL(prev_joint);
  1116. GodotJoint3D *joint = memnew(GodotSliderJoint3D(body_A, body_B, p_local_frame_A, p_local_frame_B));
  1117. joint->copy_settings_from(prev_joint);
  1118. joint_owner.replace(p_joint, joint);
  1119. memdelete(prev_joint);
  1120. }
  1121. void GodotPhysicsServer3D::slider_joint_set_param(RID p_joint, SliderJointParam p_param, real_t p_value) {
  1122. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1123. ERR_FAIL_NULL(joint);
  1124. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_SLIDER);
  1125. GodotSliderJoint3D *slider_joint = static_cast<GodotSliderJoint3D *>(joint);
  1126. slider_joint->set_param(p_param, p_value);
  1127. }
  1128. real_t GodotPhysicsServer3D::slider_joint_get_param(RID p_joint, SliderJointParam p_param) const {
  1129. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1130. ERR_FAIL_NULL_V(joint, 0);
  1131. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, 0);
  1132. GodotSliderJoint3D *slider_joint = static_cast<GodotSliderJoint3D *>(joint);
  1133. return slider_joint->get_param(p_param);
  1134. }
  1135. 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) {
  1136. GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
  1137. ERR_FAIL_NULL(body_A);
  1138. if (!p_body_B.is_valid()) {
  1139. ERR_FAIL_NULL(body_A->get_space());
  1140. p_body_B = body_A->get_space()->get_static_global_body();
  1141. }
  1142. GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
  1143. ERR_FAIL_NULL(body_B);
  1144. ERR_FAIL_COND(body_A == body_B);
  1145. GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
  1146. ERR_FAIL_NULL(prev_joint);
  1147. GodotJoint3D *joint = memnew(GodotConeTwistJoint3D(body_A, body_B, p_local_frame_A, p_local_frame_B));
  1148. joint->copy_settings_from(prev_joint);
  1149. joint_owner.replace(p_joint, joint);
  1150. memdelete(prev_joint);
  1151. }
  1152. void GodotPhysicsServer3D::cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, real_t p_value) {
  1153. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1154. ERR_FAIL_NULL(joint);
  1155. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_CONE_TWIST);
  1156. GodotConeTwistJoint3D *cone_twist_joint = static_cast<GodotConeTwistJoint3D *>(joint);
  1157. cone_twist_joint->set_param(p_param, p_value);
  1158. }
  1159. real_t GodotPhysicsServer3D::cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const {
  1160. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1161. ERR_FAIL_NULL_V(joint, 0);
  1162. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, 0);
  1163. GodotConeTwistJoint3D *cone_twist_joint = static_cast<GodotConeTwistJoint3D *>(joint);
  1164. return cone_twist_joint->get_param(p_param);
  1165. }
  1166. 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) {
  1167. GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
  1168. ERR_FAIL_NULL(body_A);
  1169. if (!p_body_B.is_valid()) {
  1170. ERR_FAIL_NULL(body_A->get_space());
  1171. p_body_B = body_A->get_space()->get_static_global_body();
  1172. }
  1173. GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
  1174. ERR_FAIL_NULL(body_B);
  1175. ERR_FAIL_COND(body_A == body_B);
  1176. GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
  1177. ERR_FAIL_NULL(prev_joint);
  1178. GodotJoint3D *joint = memnew(GodotGeneric6DOFJoint3D(body_A, body_B, p_local_frame_A, p_local_frame_B, true));
  1179. joint->copy_settings_from(prev_joint);
  1180. joint_owner.replace(p_joint, joint);
  1181. memdelete(prev_joint);
  1182. }
  1183. void GodotPhysicsServer3D::generic_6dof_joint_set_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param, real_t p_value) {
  1184. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1185. ERR_FAIL_NULL(joint);
  1186. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_6DOF);
  1187. GodotGeneric6DOFJoint3D *generic_6dof_joint = static_cast<GodotGeneric6DOFJoint3D *>(joint);
  1188. generic_6dof_joint->set_param(p_axis, p_param, p_value);
  1189. }
  1190. real_t GodotPhysicsServer3D::generic_6dof_joint_get_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param) const {
  1191. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1192. ERR_FAIL_NULL_V(joint, 0);
  1193. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, 0);
  1194. GodotGeneric6DOFJoint3D *generic_6dof_joint = static_cast<GodotGeneric6DOFJoint3D *>(joint);
  1195. return generic_6dof_joint->get_param(p_axis, p_param);
  1196. }
  1197. void GodotPhysicsServer3D::generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag, bool p_enable) {
  1198. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1199. ERR_FAIL_NULL(joint);
  1200. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_6DOF);
  1201. GodotGeneric6DOFJoint3D *generic_6dof_joint = static_cast<GodotGeneric6DOFJoint3D *>(joint);
  1202. generic_6dof_joint->set_flag(p_axis, p_flag, p_enable);
  1203. }
  1204. bool GodotPhysicsServer3D::generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag) const {
  1205. GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
  1206. ERR_FAIL_NULL_V(joint, false);
  1207. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, false);
  1208. GodotGeneric6DOFJoint3D *generic_6dof_joint = static_cast<GodotGeneric6DOFJoint3D *>(joint);
  1209. return generic_6dof_joint->get_flag(p_axis, p_flag);
  1210. }
  1211. void GodotPhysicsServer3D::free(RID p_rid) {
  1212. _update_shapes(); //just in case
  1213. if (shape_owner.owns(p_rid)) {
  1214. GodotShape3D *shape = shape_owner.get_or_null(p_rid);
  1215. while (shape->get_owners().size()) {
  1216. GodotShapeOwner3D *so = shape->get_owners().begin()->key;
  1217. so->remove_shape(shape);
  1218. }
  1219. shape_owner.free(p_rid);
  1220. memdelete(shape);
  1221. } else if (body_owner.owns(p_rid)) {
  1222. GodotBody3D *body = body_owner.get_or_null(p_rid);
  1223. body->set_space(nullptr);
  1224. while (body->get_shape_count()) {
  1225. body->remove_shape(0);
  1226. }
  1227. body_owner.free(p_rid);
  1228. memdelete(body);
  1229. } else if (soft_body_owner.owns(p_rid)) {
  1230. GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_rid);
  1231. soft_body->set_space(nullptr);
  1232. soft_body_owner.free(p_rid);
  1233. memdelete(soft_body);
  1234. } else if (area_owner.owns(p_rid)) {
  1235. GodotArea3D *area = area_owner.get_or_null(p_rid);
  1236. area->set_space(nullptr);
  1237. while (area->get_shape_count()) {
  1238. area->remove_shape(0);
  1239. }
  1240. area_owner.free(p_rid);
  1241. memdelete(area);
  1242. } else if (space_owner.owns(p_rid)) {
  1243. GodotSpace3D *space = space_owner.get_or_null(p_rid);
  1244. while (space->get_objects().size()) {
  1245. GodotCollisionObject3D *co = static_cast<GodotCollisionObject3D *>(*space->get_objects().begin());
  1246. co->set_space(nullptr);
  1247. }
  1248. active_spaces.erase(space);
  1249. free(space->get_default_area()->get_self());
  1250. free(space->get_static_global_body());
  1251. space_owner.free(p_rid);
  1252. memdelete(space);
  1253. } else if (joint_owner.owns(p_rid)) {
  1254. GodotJoint3D *joint = joint_owner.get_or_null(p_rid);
  1255. joint_owner.free(p_rid);
  1256. memdelete(joint);
  1257. } else {
  1258. ERR_FAIL_MSG("Invalid ID.");
  1259. }
  1260. }
  1261. void GodotPhysicsServer3D::set_active(bool p_active) {
  1262. active = p_active;
  1263. }
  1264. void GodotPhysicsServer3D::init() {
  1265. stepper = memnew(GodotStep3D);
  1266. }
  1267. void GodotPhysicsServer3D::step(real_t p_step) {
  1268. if (!active) {
  1269. return;
  1270. }
  1271. _update_shapes();
  1272. island_count = 0;
  1273. active_objects = 0;
  1274. collision_pairs = 0;
  1275. for (const GodotSpace3D *E : active_spaces) {
  1276. stepper->step(const_cast<GodotSpace3D *>(E), p_step);
  1277. island_count += E->get_island_count();
  1278. active_objects += E->get_active_objects();
  1279. collision_pairs += E->get_collision_pairs();
  1280. }
  1281. }
  1282. void GodotPhysicsServer3D::sync() {
  1283. doing_sync = true;
  1284. }
  1285. void GodotPhysicsServer3D::flush_queries() {
  1286. if (!active) {
  1287. return;
  1288. }
  1289. flushing_queries = true;
  1290. uint64_t time_beg = OS::get_singleton()->get_ticks_usec();
  1291. for (const GodotSpace3D *E : active_spaces) {
  1292. GodotSpace3D *space = const_cast<GodotSpace3D *>(E);
  1293. space->call_queries();
  1294. }
  1295. flushing_queries = false;
  1296. if (EngineDebugger::is_profiling("servers")) {
  1297. uint64_t total_time[GodotSpace3D::ELAPSED_TIME_MAX];
  1298. static const char *time_name[GodotSpace3D::ELAPSED_TIME_MAX] = {
  1299. "integrate_forces",
  1300. "generate_islands",
  1301. "setup_constraints",
  1302. "solve_constraints",
  1303. "integrate_velocities"
  1304. };
  1305. for (int i = 0; i < GodotSpace3D::ELAPSED_TIME_MAX; i++) {
  1306. total_time[i] = 0;
  1307. }
  1308. for (const GodotSpace3D *E : active_spaces) {
  1309. for (int i = 0; i < GodotSpace3D::ELAPSED_TIME_MAX; i++) {
  1310. total_time[i] += E->get_elapsed_time(GodotSpace3D::ElapsedTime(i));
  1311. }
  1312. }
  1313. Array values;
  1314. values.resize(GodotSpace3D::ELAPSED_TIME_MAX * 2);
  1315. for (int i = 0; i < GodotSpace3D::ELAPSED_TIME_MAX; i++) {
  1316. values[i * 2 + 0] = time_name[i];
  1317. values[i * 2 + 1] = USEC_TO_SEC(total_time[i]);
  1318. }
  1319. values.push_back("flush_queries");
  1320. values.push_back(USEC_TO_SEC(OS::get_singleton()->get_ticks_usec() - time_beg));
  1321. values.push_front("physics_3d");
  1322. EngineDebugger::profiler_add_frame_data("servers", values);
  1323. }
  1324. }
  1325. void GodotPhysicsServer3D::end_sync() {
  1326. doing_sync = false;
  1327. }
  1328. void GodotPhysicsServer3D::finish() {
  1329. memdelete(stepper);
  1330. }
  1331. int GodotPhysicsServer3D::get_process_info(ProcessInfo p_info) {
  1332. switch (p_info) {
  1333. case INFO_ACTIVE_OBJECTS: {
  1334. return active_objects;
  1335. } break;
  1336. case INFO_COLLISION_PAIRS: {
  1337. return collision_pairs;
  1338. } break;
  1339. case INFO_ISLAND_COUNT: {
  1340. return island_count;
  1341. } break;
  1342. }
  1343. return 0;
  1344. }
  1345. void GodotPhysicsServer3D::_update_shapes() {
  1346. while (pending_shape_update_list.first()) {
  1347. pending_shape_update_list.first()->self()->_shape_changed();
  1348. pending_shape_update_list.remove(pending_shape_update_list.first());
  1349. }
  1350. }
  1351. void GodotPhysicsServer3D::_shape_col_cbk(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal, void *p_userdata) {
  1352. CollCbkData *cbk = static_cast<CollCbkData *>(p_userdata);
  1353. if (cbk->max == 0) {
  1354. return;
  1355. }
  1356. if (cbk->amount == cbk->max) {
  1357. //find least deep
  1358. real_t min_depth = 1e20;
  1359. int min_depth_idx = 0;
  1360. for (int i = 0; i < cbk->amount; i++) {
  1361. real_t d = cbk->ptr[i * 2 + 0].distance_squared_to(cbk->ptr[i * 2 + 1]);
  1362. if (d < min_depth) {
  1363. min_depth = d;
  1364. min_depth_idx = i;
  1365. }
  1366. }
  1367. real_t d = p_point_A.distance_squared_to(p_point_B);
  1368. if (d < min_depth) {
  1369. return;
  1370. }
  1371. cbk->ptr[min_depth_idx * 2 + 0] = p_point_A;
  1372. cbk->ptr[min_depth_idx * 2 + 1] = p_point_B;
  1373. } else {
  1374. cbk->ptr[cbk->amount * 2 + 0] = p_point_A;
  1375. cbk->ptr[cbk->amount * 2 + 1] = p_point_B;
  1376. cbk->amount++;
  1377. }
  1378. }
  1379. GodotPhysicsServer3D *GodotPhysicsServer3D::godot_singleton = nullptr;
  1380. GodotPhysicsServer3D::GodotPhysicsServer3D(bool p_using_threads) {
  1381. godot_singleton = this;
  1382. GodotBroadPhase3D::create_func = GodotBroadPhase3DBVH::_create;
  1383. using_threads = p_using_threads;
  1384. }