jolt_physics_server_3d.cpp 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020
  1. /**************************************************************************/
  2. /* jolt_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 "jolt_physics_server_3d.h"
  31. #include "joints/jolt_cone_twist_joint_3d.h"
  32. #include "joints/jolt_generic_6dof_joint_3d.h"
  33. #include "joints/jolt_hinge_joint_3d.h"
  34. #include "joints/jolt_joint_3d.h"
  35. #include "joints/jolt_pin_joint_3d.h"
  36. #include "joints/jolt_slider_joint_3d.h"
  37. #include "objects/jolt_area_3d.h"
  38. #include "objects/jolt_body_3d.h"
  39. #include "objects/jolt_soft_body_3d.h"
  40. #include "servers/physics_3d/physics_server_3d_wrap_mt.h"
  41. #include "shapes/jolt_box_shape_3d.h"
  42. #include "shapes/jolt_capsule_shape_3d.h"
  43. #include "shapes/jolt_concave_polygon_shape_3d.h"
  44. #include "shapes/jolt_convex_polygon_shape_3d.h"
  45. #include "shapes/jolt_cylinder_shape_3d.h"
  46. #include "shapes/jolt_height_map_shape_3d.h"
  47. #include "shapes/jolt_separation_ray_shape_3d.h"
  48. #include "shapes/jolt_sphere_shape_3d.h"
  49. #include "shapes/jolt_world_boundary_shape_3d.h"
  50. #include "spaces/jolt_job_system.h"
  51. #include "spaces/jolt_physics_direct_space_state_3d.h"
  52. #include "spaces/jolt_space_3d.h"
  53. JoltPhysicsServer3D::JoltPhysicsServer3D(bool p_on_separate_thread) :
  54. on_separate_thread(p_on_separate_thread) {
  55. singleton = this;
  56. }
  57. JoltPhysicsServer3D::~JoltPhysicsServer3D() {
  58. if (singleton == this) {
  59. singleton = nullptr;
  60. }
  61. }
  62. RID JoltPhysicsServer3D::world_boundary_shape_create() {
  63. JoltShape3D *shape = memnew(JoltWorldBoundaryShape3D);
  64. RID rid = shape_owner.make_rid(shape);
  65. shape->set_rid(rid);
  66. return rid;
  67. }
  68. RID JoltPhysicsServer3D::separation_ray_shape_create() {
  69. JoltShape3D *shape = memnew(JoltSeparationRayShape3D);
  70. RID rid = shape_owner.make_rid(shape);
  71. shape->set_rid(rid);
  72. return rid;
  73. }
  74. RID JoltPhysicsServer3D::sphere_shape_create() {
  75. JoltShape3D *shape = memnew(JoltSphereShape3D);
  76. RID rid = shape_owner.make_rid(shape);
  77. shape->set_rid(rid);
  78. return rid;
  79. }
  80. RID JoltPhysicsServer3D::box_shape_create() {
  81. JoltShape3D *shape = memnew(JoltBoxShape3D);
  82. RID rid = shape_owner.make_rid(shape);
  83. shape->set_rid(rid);
  84. return rid;
  85. }
  86. RID JoltPhysicsServer3D::capsule_shape_create() {
  87. JoltShape3D *shape = memnew(JoltCapsuleShape3D);
  88. RID rid = shape_owner.make_rid(shape);
  89. shape->set_rid(rid);
  90. return rid;
  91. }
  92. RID JoltPhysicsServer3D::cylinder_shape_create() {
  93. JoltShape3D *shape = memnew(JoltCylinderShape3D);
  94. RID rid = shape_owner.make_rid(shape);
  95. shape->set_rid(rid);
  96. return rid;
  97. }
  98. RID JoltPhysicsServer3D::convex_polygon_shape_create() {
  99. JoltShape3D *shape = memnew(JoltConvexPolygonShape3D);
  100. RID rid = shape_owner.make_rid(shape);
  101. shape->set_rid(rid);
  102. return rid;
  103. }
  104. RID JoltPhysicsServer3D::concave_polygon_shape_create() {
  105. JoltShape3D *shape = memnew(JoltConcavePolygonShape3D);
  106. RID rid = shape_owner.make_rid(shape);
  107. shape->set_rid(rid);
  108. return rid;
  109. }
  110. RID JoltPhysicsServer3D::heightmap_shape_create() {
  111. JoltShape3D *shape = memnew(JoltHeightMapShape3D);
  112. RID rid = shape_owner.make_rid(shape);
  113. shape->set_rid(rid);
  114. return rid;
  115. }
  116. RID JoltPhysicsServer3D::custom_shape_create() {
  117. ERR_FAIL_V_MSG(RID(), "Custom shapes are not supported.");
  118. }
  119. void JoltPhysicsServer3D::shape_set_data(RID p_shape, const Variant &p_data) {
  120. JoltShape3D *shape = shape_owner.get_or_null(p_shape);
  121. ERR_FAIL_NULL(shape);
  122. shape->set_data(p_data);
  123. }
  124. Variant JoltPhysicsServer3D::shape_get_data(RID p_shape) const {
  125. const JoltShape3D *shape = shape_owner.get_or_null(p_shape);
  126. ERR_FAIL_NULL_V(shape, Variant());
  127. return shape->get_data();
  128. }
  129. void JoltPhysicsServer3D::shape_set_custom_solver_bias(RID p_shape, real_t p_bias) {
  130. JoltShape3D *shape = shape_owner.get_or_null(p_shape);
  131. ERR_FAIL_NULL(shape);
  132. shape->set_solver_bias((float)p_bias);
  133. }
  134. PhysicsServer3D::ShapeType JoltPhysicsServer3D::shape_get_type(RID p_shape) const {
  135. const JoltShape3D *shape = shape_owner.get_or_null(p_shape);
  136. ERR_FAIL_NULL_V(shape, SHAPE_CUSTOM);
  137. return shape->get_type();
  138. }
  139. void JoltPhysicsServer3D::shape_set_margin(RID p_shape, real_t p_margin) {
  140. JoltShape3D *shape = shape_owner.get_or_null(p_shape);
  141. ERR_FAIL_NULL(shape);
  142. shape->set_margin((float)p_margin);
  143. }
  144. real_t JoltPhysicsServer3D::shape_get_margin(RID p_shape) const {
  145. const JoltShape3D *shape = shape_owner.get_or_null(p_shape);
  146. ERR_FAIL_NULL_V(shape, 0.0);
  147. return (real_t)shape->get_margin();
  148. }
  149. real_t JoltPhysicsServer3D::shape_get_custom_solver_bias(RID p_shape) const {
  150. const JoltShape3D *shape = shape_owner.get_or_null(p_shape);
  151. ERR_FAIL_NULL_V(shape, 0.0);
  152. return (real_t)shape->get_solver_bias();
  153. }
  154. RID JoltPhysicsServer3D::space_create() {
  155. JoltSpace3D *space = memnew(JoltSpace3D(job_system));
  156. RID rid = space_owner.make_rid(space);
  157. space->set_rid(rid);
  158. const RID default_area_rid = area_create();
  159. JoltArea3D *default_area = area_owner.get_or_null(default_area_rid);
  160. ERR_FAIL_NULL_V(default_area, RID());
  161. space->set_default_area(default_area);
  162. default_area->set_space(space);
  163. return rid;
  164. }
  165. void JoltPhysicsServer3D::space_set_active(RID p_space, bool p_active) {
  166. JoltSpace3D *space = space_owner.get_or_null(p_space);
  167. ERR_FAIL_NULL(space);
  168. if (p_active) {
  169. space->set_active(true);
  170. active_spaces.insert(space);
  171. } else {
  172. space->set_active(false);
  173. active_spaces.erase(space);
  174. }
  175. }
  176. bool JoltPhysicsServer3D::space_is_active(RID p_space) const {
  177. JoltSpace3D *space = space_owner.get_or_null(p_space);
  178. ERR_FAIL_NULL_V(space, false);
  179. return active_spaces.has(space);
  180. }
  181. void JoltPhysicsServer3D::space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) {
  182. JoltSpace3D *space = space_owner.get_or_null(p_space);
  183. ERR_FAIL_NULL(space);
  184. space->set_param(p_param, (double)p_value);
  185. }
  186. real_t JoltPhysicsServer3D::space_get_param(RID p_space, SpaceParameter p_param) const {
  187. const JoltSpace3D *space = space_owner.get_or_null(p_space);
  188. ERR_FAIL_NULL_V(space, 0.0);
  189. return (real_t)space->get_param(p_param);
  190. }
  191. PhysicsDirectSpaceState3D *JoltPhysicsServer3D::space_get_direct_state(RID p_space) {
  192. JoltSpace3D *space = space_owner.get_or_null(p_space);
  193. ERR_FAIL_NULL_V(space, nullptr);
  194. ERR_FAIL_COND_V_MSG((on_separate_thread && !doing_sync) || space->is_stepping(), nullptr, "Space state is inaccessible right now, wait for iteration or physics process notification.");
  195. return space->get_direct_state();
  196. }
  197. void JoltPhysicsServer3D::space_set_debug_contacts(RID p_space, int p_max_contacts) {
  198. #ifdef DEBUG_ENABLED
  199. JoltSpace3D *space = space_owner.get_or_null(p_space);
  200. ERR_FAIL_NULL(space);
  201. space->set_max_debug_contacts(p_max_contacts);
  202. #endif
  203. }
  204. PackedVector3Array JoltPhysicsServer3D::space_get_contacts(RID p_space) const {
  205. #ifdef DEBUG_ENABLED
  206. JoltSpace3D *space = space_owner.get_or_null(p_space);
  207. ERR_FAIL_NULL_V(space, PackedVector3Array());
  208. return space->get_debug_contacts();
  209. #else
  210. return PackedVector3Array();
  211. #endif
  212. }
  213. int JoltPhysicsServer3D::space_get_contact_count(RID p_space) const {
  214. #ifdef DEBUG_ENABLED
  215. JoltSpace3D *space = space_owner.get_or_null(p_space);
  216. ERR_FAIL_NULL_V(space, 0);
  217. return space->get_debug_contact_count();
  218. #else
  219. return 0;
  220. #endif
  221. }
  222. RID JoltPhysicsServer3D::area_create() {
  223. JoltArea3D *area = memnew(JoltArea3D);
  224. RID rid = area_owner.make_rid(area);
  225. area->set_rid(rid);
  226. return rid;
  227. }
  228. void JoltPhysicsServer3D::area_set_space(RID p_area, RID p_space) {
  229. JoltArea3D *area = area_owner.get_or_null(p_area);
  230. ERR_FAIL_NULL(area);
  231. JoltSpace3D *space = nullptr;
  232. if (p_space.is_valid()) {
  233. space = space_owner.get_or_null(p_space);
  234. ERR_FAIL_NULL(space);
  235. }
  236. area->set_space(space);
  237. }
  238. void JoltPhysicsServer3D::soft_body_apply_point_impulse(RID p_body, int p_point_index, const Vector3 &p_impulse) {
  239. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  240. ERR_FAIL_NULL(body);
  241. body->apply_vertex_impulse(p_point_index, p_impulse);
  242. }
  243. void JoltPhysicsServer3D::soft_body_apply_point_force(RID p_body, int p_point_index, const Vector3 &p_force) {
  244. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  245. ERR_FAIL_NULL(body);
  246. body->apply_vertex_force(p_point_index, p_force);
  247. }
  248. void JoltPhysicsServer3D::soft_body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) {
  249. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  250. ERR_FAIL_NULL(body);
  251. body->apply_central_impulse(p_impulse);
  252. }
  253. void JoltPhysicsServer3D::soft_body_apply_central_force(RID p_body, const Vector3 &p_force) {
  254. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  255. ERR_FAIL_NULL(body);
  256. body->apply_central_force(p_force);
  257. }
  258. RID JoltPhysicsServer3D::area_get_space(RID p_area) const {
  259. const JoltArea3D *area = area_owner.get_or_null(p_area);
  260. ERR_FAIL_NULL_V(area, RID());
  261. const JoltSpace3D *space = area->get_space();
  262. if (space == nullptr) {
  263. return RID();
  264. }
  265. return space->get_rid();
  266. }
  267. void JoltPhysicsServer3D::area_add_shape(RID p_area, RID p_shape, const Transform3D &p_transform, bool p_disabled) {
  268. JoltArea3D *area = area_owner.get_or_null(p_area);
  269. ERR_FAIL_NULL(area);
  270. JoltShape3D *shape = shape_owner.get_or_null(p_shape);
  271. ERR_FAIL_NULL(shape);
  272. area->add_shape(shape, p_transform, p_disabled);
  273. }
  274. void JoltPhysicsServer3D::area_set_shape(RID p_area, int p_shape_idx, RID p_shape) {
  275. JoltArea3D *area = area_owner.get_or_null(p_area);
  276. ERR_FAIL_NULL(area);
  277. JoltShape3D *shape = shape_owner.get_or_null(p_shape);
  278. ERR_FAIL_NULL(shape);
  279. area->set_shape(p_shape_idx, shape);
  280. }
  281. RID JoltPhysicsServer3D::area_get_shape(RID p_area, int p_shape_idx) const {
  282. const JoltArea3D *area = area_owner.get_or_null(p_area);
  283. ERR_FAIL_NULL_V(area, RID());
  284. const JoltShape3D *shape = area->get_shape(p_shape_idx);
  285. ERR_FAIL_NULL_V(shape, RID());
  286. return shape->get_rid();
  287. }
  288. void JoltPhysicsServer3D::area_set_shape_transform(RID p_area, int p_shape_idx, const Transform3D &p_transform) {
  289. JoltArea3D *area = area_owner.get_or_null(p_area);
  290. ERR_FAIL_NULL(area);
  291. area->set_shape_transform(p_shape_idx, p_transform);
  292. }
  293. Transform3D JoltPhysicsServer3D::area_get_shape_transform(RID p_area, int p_shape_idx) const {
  294. const JoltArea3D *area = area_owner.get_or_null(p_area);
  295. ERR_FAIL_NULL_V(area, Transform3D());
  296. return area->get_shape_transform_scaled(p_shape_idx);
  297. }
  298. int JoltPhysicsServer3D::area_get_shape_count(RID p_area) const {
  299. const JoltArea3D *area = area_owner.get_or_null(p_area);
  300. ERR_FAIL_NULL_V(area, 0);
  301. return area->get_shape_count();
  302. }
  303. void JoltPhysicsServer3D::area_remove_shape(RID p_area, int p_shape_idx) {
  304. JoltArea3D *area = area_owner.get_or_null(p_area);
  305. ERR_FAIL_NULL(area);
  306. area->remove_shape(p_shape_idx);
  307. }
  308. void JoltPhysicsServer3D::area_clear_shapes(RID p_area) {
  309. JoltArea3D *area = area_owner.get_or_null(p_area);
  310. ERR_FAIL_NULL(area);
  311. area->clear_shapes();
  312. }
  313. void JoltPhysicsServer3D::area_set_shape_disabled(RID p_area, int p_shape_idx, bool p_disabled) {
  314. JoltArea3D *area = area_owner.get_or_null(p_area);
  315. ERR_FAIL_NULL(area);
  316. area->set_shape_disabled(p_shape_idx, p_disabled);
  317. }
  318. void JoltPhysicsServer3D::area_attach_object_instance_id(RID p_area, ObjectID p_id) {
  319. RID area_rid = p_area;
  320. if (space_owner.owns(area_rid)) {
  321. const JoltSpace3D *space = space_owner.get_or_null(area_rid);
  322. area_rid = space->get_default_area()->get_rid();
  323. }
  324. JoltArea3D *area = area_owner.get_or_null(area_rid);
  325. ERR_FAIL_NULL(area);
  326. area->set_instance_id(p_id);
  327. }
  328. ObjectID JoltPhysicsServer3D::area_get_object_instance_id(RID p_area) const {
  329. RID area_rid = p_area;
  330. if (space_owner.owns(area_rid)) {
  331. const JoltSpace3D *space = space_owner.get_or_null(area_rid);
  332. area_rid = space->get_default_area()->get_rid();
  333. }
  334. JoltArea3D *area = area_owner.get_or_null(area_rid);
  335. ERR_FAIL_NULL_V(area, ObjectID());
  336. return area->get_instance_id();
  337. }
  338. void JoltPhysicsServer3D::area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) {
  339. RID area_rid = p_area;
  340. if (space_owner.owns(area_rid)) {
  341. const JoltSpace3D *space = space_owner.get_or_null(area_rid);
  342. area_rid = space->get_default_area()->get_rid();
  343. }
  344. JoltArea3D *area = area_owner.get_or_null(area_rid);
  345. ERR_FAIL_NULL(area);
  346. area->set_param(p_param, p_value);
  347. }
  348. Variant JoltPhysicsServer3D::area_get_param(RID p_area, AreaParameter p_param) const {
  349. RID area_rid = p_area;
  350. if (space_owner.owns(area_rid)) {
  351. const JoltSpace3D *space = space_owner.get_or_null(area_rid);
  352. area_rid = space->get_default_area()->get_rid();
  353. }
  354. JoltArea3D *area = area_owner.get_or_null(area_rid);
  355. ERR_FAIL_NULL_V(area, Variant());
  356. return area->get_param(p_param);
  357. }
  358. void JoltPhysicsServer3D::area_set_transform(RID p_area, const Transform3D &p_transform) {
  359. JoltArea3D *area = area_owner.get_or_null(p_area);
  360. ERR_FAIL_NULL(area);
  361. return area->set_transform(p_transform);
  362. }
  363. Transform3D JoltPhysicsServer3D::area_get_transform(RID p_area) const {
  364. const JoltArea3D *area = area_owner.get_or_null(p_area);
  365. ERR_FAIL_NULL_V(area, Transform3D());
  366. return area->get_transform_scaled();
  367. }
  368. void JoltPhysicsServer3D::area_set_collision_mask(RID p_area, uint32_t p_mask) {
  369. JoltArea3D *area = area_owner.get_or_null(p_area);
  370. ERR_FAIL_NULL(area);
  371. area->set_collision_mask(p_mask);
  372. }
  373. uint32_t JoltPhysicsServer3D::area_get_collision_mask(RID p_area) const {
  374. const JoltArea3D *area = area_owner.get_or_null(p_area);
  375. ERR_FAIL_NULL_V(area, 0);
  376. return area->get_collision_mask();
  377. }
  378. void JoltPhysicsServer3D::area_set_collision_layer(RID p_area, uint32_t p_layer) {
  379. JoltArea3D *area = area_owner.get_or_null(p_area);
  380. ERR_FAIL_NULL(area);
  381. area->set_collision_layer(p_layer);
  382. }
  383. uint32_t JoltPhysicsServer3D::area_get_collision_layer(RID p_area) const {
  384. const JoltArea3D *area = area_owner.get_or_null(p_area);
  385. ERR_FAIL_NULL_V(area, 0);
  386. return area->get_collision_layer();
  387. }
  388. void JoltPhysicsServer3D::area_set_monitorable(RID p_area, bool p_monitorable) {
  389. JoltArea3D *area = area_owner.get_or_null(p_area);
  390. ERR_FAIL_NULL(area);
  391. area->set_monitorable(p_monitorable);
  392. }
  393. void JoltPhysicsServer3D::area_set_monitor_callback(RID p_area, const Callable &p_callback) {
  394. JoltArea3D *area = area_owner.get_or_null(p_area);
  395. ERR_FAIL_NULL(area);
  396. area->set_body_monitor_callback(p_callback);
  397. }
  398. void JoltPhysicsServer3D::area_set_area_monitor_callback(RID p_area, const Callable &p_callback) {
  399. JoltArea3D *area = area_owner.get_or_null(p_area);
  400. ERR_FAIL_NULL(area);
  401. area->set_area_monitor_callback(p_callback);
  402. }
  403. void JoltPhysicsServer3D::area_set_ray_pickable(RID p_area, bool p_enable) {
  404. JoltArea3D *area = area_owner.get_or_null(p_area);
  405. ERR_FAIL_NULL(area);
  406. area->set_pickable(p_enable);
  407. }
  408. RID JoltPhysicsServer3D::body_create() {
  409. JoltBody3D *body = memnew(JoltBody3D);
  410. RID rid = body_owner.make_rid(body);
  411. body->set_rid(rid);
  412. return rid;
  413. }
  414. void JoltPhysicsServer3D::body_set_space(RID p_body, RID p_space) {
  415. JoltBody3D *body = body_owner.get_or_null(p_body);
  416. ERR_FAIL_NULL(body);
  417. JoltSpace3D *space = nullptr;
  418. if (p_space.is_valid()) {
  419. space = space_owner.get_or_null(p_space);
  420. ERR_FAIL_NULL(space);
  421. }
  422. body->set_space(space);
  423. }
  424. RID JoltPhysicsServer3D::body_get_space(RID p_body) const {
  425. const JoltBody3D *body = body_owner.get_or_null(p_body);
  426. ERR_FAIL_NULL_V(body, RID());
  427. const JoltSpace3D *space = body->get_space();
  428. if (space == nullptr) {
  429. return RID();
  430. }
  431. return space->get_rid();
  432. }
  433. void JoltPhysicsServer3D::body_set_mode(RID p_body, BodyMode p_mode) {
  434. JoltBody3D *body = body_owner.get_or_null(p_body);
  435. ERR_FAIL_NULL(body);
  436. body->set_mode(p_mode);
  437. }
  438. PhysicsServer3D::BodyMode JoltPhysicsServer3D::body_get_mode(RID p_body) const {
  439. const JoltBody3D *body = body_owner.get_or_null(p_body);
  440. ERR_FAIL_NULL_V(body, BODY_MODE_STATIC);
  441. return body->get_mode();
  442. }
  443. void JoltPhysicsServer3D::body_add_shape(RID p_body, RID p_shape, const Transform3D &p_transform, bool p_disabled) {
  444. JoltBody3D *body = body_owner.get_or_null(p_body);
  445. ERR_FAIL_NULL(body);
  446. JoltShape3D *shape = shape_owner.get_or_null(p_shape);
  447. ERR_FAIL_NULL(shape);
  448. body->add_shape(shape, p_transform, p_disabled);
  449. }
  450. void JoltPhysicsServer3D::body_set_shape(RID p_body, int p_shape_idx, RID p_shape) {
  451. JoltBody3D *body = body_owner.get_or_null(p_body);
  452. ERR_FAIL_NULL(body);
  453. JoltShape3D *shape = shape_owner.get_or_null(p_shape);
  454. ERR_FAIL_NULL(shape);
  455. body->set_shape(p_shape_idx, shape);
  456. }
  457. RID JoltPhysicsServer3D::body_get_shape(RID p_body, int p_shape_idx) const {
  458. const JoltBody3D *body = body_owner.get_or_null(p_body);
  459. ERR_FAIL_NULL_V(body, RID());
  460. const JoltShape3D *shape = body->get_shape(p_shape_idx);
  461. ERR_FAIL_NULL_V(shape, RID());
  462. return shape->get_rid();
  463. }
  464. void JoltPhysicsServer3D::body_set_shape_transform(RID p_body, int p_shape_idx, const Transform3D &p_transform) {
  465. JoltBody3D *body = body_owner.get_or_null(p_body);
  466. ERR_FAIL_NULL(body);
  467. body->set_shape_transform(p_shape_idx, p_transform);
  468. }
  469. Transform3D JoltPhysicsServer3D::body_get_shape_transform(RID p_body, int p_shape_idx) const {
  470. const JoltBody3D *body = body_owner.get_or_null(p_body);
  471. ERR_FAIL_NULL_V(body, Transform3D());
  472. return body->get_shape_transform_scaled(p_shape_idx);
  473. }
  474. int JoltPhysicsServer3D::body_get_shape_count(RID p_body) const {
  475. const JoltBody3D *body = body_owner.get_or_null(p_body);
  476. ERR_FAIL_NULL_V(body, 0);
  477. return body->get_shape_count();
  478. }
  479. void JoltPhysicsServer3D::body_remove_shape(RID p_body, int p_shape_idx) {
  480. JoltBody3D *body = body_owner.get_or_null(p_body);
  481. ERR_FAIL_NULL(body);
  482. body->remove_shape(p_shape_idx);
  483. }
  484. void JoltPhysicsServer3D::body_clear_shapes(RID p_body) {
  485. JoltBody3D *body = body_owner.get_or_null(p_body);
  486. ERR_FAIL_NULL(body);
  487. body->clear_shapes();
  488. }
  489. void JoltPhysicsServer3D::body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled) {
  490. JoltBody3D *body = body_owner.get_or_null(p_body);
  491. ERR_FAIL_NULL(body);
  492. body->set_shape_disabled(p_shape_idx, p_disabled);
  493. }
  494. void JoltPhysicsServer3D::body_attach_object_instance_id(RID p_body, ObjectID p_id) {
  495. if (JoltBody3D *body = body_owner.get_or_null(p_body)) {
  496. body->set_instance_id(p_id);
  497. } else if (JoltSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body)) {
  498. soft_body->set_instance_id(p_id);
  499. } else {
  500. ERR_FAIL();
  501. }
  502. }
  503. ObjectID JoltPhysicsServer3D::body_get_object_instance_id(RID p_body) const {
  504. const JoltBody3D *body = body_owner.get_or_null(p_body);
  505. ERR_FAIL_NULL_V(body, ObjectID());
  506. return body->get_instance_id();
  507. }
  508. void JoltPhysicsServer3D::body_set_enable_continuous_collision_detection(RID p_body, bool p_enable) {
  509. JoltBody3D *body = body_owner.get_or_null(p_body);
  510. ERR_FAIL_NULL(body);
  511. body->set_ccd_enabled(p_enable);
  512. }
  513. bool JoltPhysicsServer3D::body_is_continuous_collision_detection_enabled(RID p_body) const {
  514. const JoltBody3D *body = body_owner.get_or_null(p_body);
  515. ERR_FAIL_NULL_V(body, false);
  516. return body->is_ccd_enabled();
  517. }
  518. void JoltPhysicsServer3D::body_set_collision_layer(RID p_body, uint32_t p_layer) {
  519. JoltBody3D *body = body_owner.get_or_null(p_body);
  520. ERR_FAIL_NULL(body);
  521. body->set_collision_layer(p_layer);
  522. }
  523. uint32_t JoltPhysicsServer3D::body_get_collision_layer(RID p_body) const {
  524. const JoltBody3D *body = body_owner.get_or_null(p_body);
  525. ERR_FAIL_NULL_V(body, 0);
  526. return body->get_collision_layer();
  527. }
  528. void JoltPhysicsServer3D::body_set_collision_mask(RID p_body, uint32_t p_mask) {
  529. JoltBody3D *body = body_owner.get_or_null(p_body);
  530. ERR_FAIL_NULL(body);
  531. body->set_collision_mask(p_mask);
  532. }
  533. uint32_t JoltPhysicsServer3D::body_get_collision_mask(RID p_body) const {
  534. const JoltBody3D *body = body_owner.get_or_null(p_body);
  535. ERR_FAIL_NULL_V(body, 0);
  536. return body->get_collision_mask();
  537. }
  538. void JoltPhysicsServer3D::body_set_collision_priority(RID p_body, real_t p_priority) {
  539. JoltBody3D *body = body_owner.get_or_null(p_body);
  540. ERR_FAIL_NULL(body);
  541. body->set_collision_priority((float)p_priority);
  542. }
  543. real_t JoltPhysicsServer3D::body_get_collision_priority(RID p_body) const {
  544. const JoltBody3D *body = body_owner.get_or_null(p_body);
  545. ERR_FAIL_NULL_V(body, 0.0);
  546. return (real_t)body->get_collision_priority();
  547. }
  548. void JoltPhysicsServer3D::body_set_user_flags(RID p_body, uint32_t p_flags) {
  549. WARN_PRINT("Body user flags are not supported. Any such value will be ignored.");
  550. }
  551. uint32_t JoltPhysicsServer3D::body_get_user_flags(RID p_body) const {
  552. return 0;
  553. }
  554. void JoltPhysicsServer3D::body_set_param(RID p_body, BodyParameter p_param, const Variant &p_value) {
  555. JoltBody3D *body = body_owner.get_or_null(p_body);
  556. ERR_FAIL_NULL(body);
  557. body->set_param(p_param, p_value);
  558. }
  559. Variant JoltPhysicsServer3D::body_get_param(RID p_body, BodyParameter p_param) const {
  560. const JoltBody3D *body = body_owner.get_or_null(p_body);
  561. ERR_FAIL_NULL_V(body, Variant());
  562. return body->get_param(p_param);
  563. }
  564. void JoltPhysicsServer3D::body_reset_mass_properties(RID p_body) {
  565. JoltBody3D *body = body_owner.get_or_null(p_body);
  566. ERR_FAIL_NULL(body);
  567. body->reset_mass_properties();
  568. }
  569. void JoltPhysicsServer3D::body_set_state(RID p_body, BodyState p_state, const Variant &p_value) {
  570. JoltBody3D *body = body_owner.get_or_null(p_body);
  571. ERR_FAIL_NULL(body);
  572. body->set_state(p_state, p_value);
  573. }
  574. Variant JoltPhysicsServer3D::body_get_state(RID p_body, BodyState p_state) const {
  575. JoltBody3D *body = body_owner.get_or_null(p_body);
  576. ERR_FAIL_NULL_V(body, Variant());
  577. return body->get_state(p_state);
  578. }
  579. void JoltPhysicsServer3D::body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) {
  580. JoltBody3D *body = body_owner.get_or_null(p_body);
  581. ERR_FAIL_NULL(body);
  582. return body->apply_central_impulse(p_impulse);
  583. }
  584. void JoltPhysicsServer3D::body_apply_impulse(RID p_body, const Vector3 &p_impulse, const Vector3 &p_position) {
  585. JoltBody3D *body = body_owner.get_or_null(p_body);
  586. ERR_FAIL_NULL(body);
  587. return body->apply_impulse(p_impulse, p_position);
  588. }
  589. void JoltPhysicsServer3D::body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse) {
  590. JoltBody3D *body = body_owner.get_or_null(p_body);
  591. ERR_FAIL_NULL(body);
  592. return body->apply_torque_impulse(p_impulse);
  593. }
  594. void JoltPhysicsServer3D::body_apply_central_force(RID p_body, const Vector3 &p_force) {
  595. JoltBody3D *body = body_owner.get_or_null(p_body);
  596. ERR_FAIL_NULL(body);
  597. return body->apply_central_force(p_force);
  598. }
  599. void JoltPhysicsServer3D::body_apply_force(RID p_body, const Vector3 &p_force, const Vector3 &p_position) {
  600. JoltBody3D *body = body_owner.get_or_null(p_body);
  601. ERR_FAIL_NULL(body);
  602. return body->apply_force(p_force, p_position);
  603. }
  604. void JoltPhysicsServer3D::body_apply_torque(RID p_body, const Vector3 &p_torque) {
  605. JoltBody3D *body = body_owner.get_or_null(p_body);
  606. ERR_FAIL_NULL(body);
  607. return body->apply_torque(p_torque);
  608. }
  609. void JoltPhysicsServer3D::body_add_constant_central_force(RID p_body, const Vector3 &p_force) {
  610. JoltBody3D *body = body_owner.get_or_null(p_body);
  611. ERR_FAIL_NULL(body);
  612. body->add_constant_central_force(p_force);
  613. }
  614. void JoltPhysicsServer3D::body_add_constant_force(RID p_body, const Vector3 &p_force, const Vector3 &p_position) {
  615. JoltBody3D *body = body_owner.get_or_null(p_body);
  616. ERR_FAIL_NULL(body);
  617. body->add_constant_force(p_force, p_position);
  618. }
  619. void JoltPhysicsServer3D::body_add_constant_torque(RID p_body, const Vector3 &p_torque) {
  620. JoltBody3D *body = body_owner.get_or_null(p_body);
  621. ERR_FAIL_NULL(body);
  622. body->add_constant_torque(p_torque);
  623. }
  624. void JoltPhysicsServer3D::body_set_constant_force(RID p_body, const Vector3 &p_force) {
  625. JoltBody3D *body = body_owner.get_or_null(p_body);
  626. ERR_FAIL_NULL(body);
  627. body->set_constant_force(p_force);
  628. }
  629. Vector3 JoltPhysicsServer3D::body_get_constant_force(RID p_body) const {
  630. const JoltBody3D *body = body_owner.get_or_null(p_body);
  631. ERR_FAIL_NULL_V(body, Vector3());
  632. return body->get_constant_force();
  633. }
  634. void JoltPhysicsServer3D::body_set_constant_torque(RID p_body, const Vector3 &p_torque) {
  635. JoltBody3D *body = body_owner.get_or_null(p_body);
  636. ERR_FAIL_NULL(body);
  637. body->set_constant_torque(p_torque);
  638. }
  639. Vector3 JoltPhysicsServer3D::body_get_constant_torque(RID p_body) const {
  640. const JoltBody3D *body = body_owner.get_or_null(p_body);
  641. ERR_FAIL_NULL_V(body, Vector3());
  642. return body->get_constant_torque();
  643. }
  644. void JoltPhysicsServer3D::body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity) {
  645. JoltBody3D *body = body_owner.get_or_null(p_body);
  646. ERR_FAIL_NULL(body);
  647. body->set_axis_velocity(p_axis_velocity);
  648. }
  649. void JoltPhysicsServer3D::body_set_axis_lock(RID p_body, BodyAxis p_axis, bool p_lock) {
  650. JoltBody3D *body = body_owner.get_or_null(p_body);
  651. ERR_FAIL_NULL(body);
  652. body->set_axis_lock(p_axis, p_lock);
  653. }
  654. bool JoltPhysicsServer3D::body_is_axis_locked(RID p_body, BodyAxis p_axis) const {
  655. const JoltBody3D *body = body_owner.get_or_null(p_body);
  656. ERR_FAIL_NULL_V(body, false);
  657. return body->is_axis_locked(p_axis);
  658. }
  659. void JoltPhysicsServer3D::body_add_collision_exception(RID p_body, RID p_excepted_body) {
  660. JoltBody3D *body = body_owner.get_or_null(p_body);
  661. ERR_FAIL_NULL(body);
  662. body->add_collision_exception(p_excepted_body);
  663. }
  664. void JoltPhysicsServer3D::body_remove_collision_exception(RID p_body, RID p_excepted_body) {
  665. JoltBody3D *body = body_owner.get_or_null(p_body);
  666. ERR_FAIL_NULL(body);
  667. body->remove_collision_exception(p_excepted_body);
  668. }
  669. void JoltPhysicsServer3D::body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  670. const JoltBody3D *body = body_owner.get_or_null(p_body);
  671. ERR_FAIL_NULL(body);
  672. for (const RID &exception : body->get_collision_exceptions()) {
  673. p_exceptions->push_back(exception);
  674. }
  675. }
  676. void JoltPhysicsServer3D::body_set_max_contacts_reported(RID p_body, int p_amount) {
  677. JoltBody3D *body = body_owner.get_or_null(p_body);
  678. ERR_FAIL_NULL(body);
  679. return body->set_max_contacts_reported(p_amount);
  680. }
  681. int JoltPhysicsServer3D::body_get_max_contacts_reported(RID p_body) const {
  682. const JoltBody3D *body = body_owner.get_or_null(p_body);
  683. ERR_FAIL_NULL_V(body, 0);
  684. return body->get_max_contacts_reported();
  685. }
  686. void JoltPhysicsServer3D::body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) {
  687. WARN_PRINT("Per-body contact depth threshold is not supported. Any such value will be ignored.");
  688. }
  689. real_t JoltPhysicsServer3D::body_get_contacts_reported_depth_threshold(RID p_body) const {
  690. return 0.0;
  691. }
  692. void JoltPhysicsServer3D::body_set_omit_force_integration(RID p_body, bool p_enable) {
  693. JoltBody3D *body = body_owner.get_or_null(p_body);
  694. ERR_FAIL_NULL(body);
  695. body->set_custom_integrator(p_enable);
  696. }
  697. bool JoltPhysicsServer3D::body_is_omitting_force_integration(RID p_body) const {
  698. JoltBody3D *body = body_owner.get_or_null(p_body);
  699. ERR_FAIL_NULL_V(body, false);
  700. return body->has_custom_integrator();
  701. }
  702. void JoltPhysicsServer3D::body_set_state_sync_callback(RID p_body, const Callable &p_callable) {
  703. JoltBody3D *body = body_owner.get_or_null(p_body);
  704. ERR_FAIL_NULL(body);
  705. body->set_state_sync_callback(p_callable);
  706. }
  707. void JoltPhysicsServer3D::body_set_force_integration_callback(RID p_body, const Callable &p_callable, const Variant &p_userdata) {
  708. JoltBody3D *body = body_owner.get_or_null(p_body);
  709. ERR_FAIL_NULL(body);
  710. body->set_custom_integration_callback(p_callable, p_userdata);
  711. }
  712. void JoltPhysicsServer3D::body_set_ray_pickable(RID p_body, bool p_enable) {
  713. JoltBody3D *body = body_owner.get_or_null(p_body);
  714. ERR_FAIL_NULL(body);
  715. body->set_pickable(p_enable);
  716. }
  717. bool JoltPhysicsServer3D::body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result) {
  718. JoltBody3D *body = body_owner.get_or_null(p_body);
  719. ERR_FAIL_NULL_V(body, false);
  720. JoltSpace3D *space = body->get_space();
  721. ERR_FAIL_NULL_V(space, false);
  722. return space->get_direct_state()->body_test_motion(*body, p_parameters, r_result);
  723. }
  724. PhysicsDirectBodyState3D *JoltPhysicsServer3D::body_get_direct_state(RID p_body) {
  725. ERR_FAIL_COND_V_MSG((on_separate_thread && !doing_sync), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
  726. JoltBody3D *body = body_owner.get_or_null(p_body);
  727. if (unlikely(body == nullptr || body->get_space() == nullptr)) {
  728. return nullptr;
  729. }
  730. ERR_FAIL_COND_V_MSG(body->get_space()->is_stepping(), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
  731. return body->get_direct_state();
  732. }
  733. RID JoltPhysicsServer3D::soft_body_create() {
  734. JoltSoftBody3D *body = memnew(JoltSoftBody3D);
  735. RID rid = soft_body_owner.make_rid(body);
  736. body->set_rid(rid);
  737. return rid;
  738. }
  739. void JoltPhysicsServer3D::soft_body_update_rendering_server(RID p_body, RequiredParam<PhysicsServer3DRenderingServerHandler> rp_rendering_server_handler) {
  740. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  741. ERR_FAIL_NULL(body);
  742. EXTRACT_PARAM_OR_FAIL(p_rendering_server_handler, rp_rendering_server_handler);
  743. return body->update_rendering_server(p_rendering_server_handler);
  744. }
  745. void JoltPhysicsServer3D::soft_body_set_space(RID p_body, RID p_space) {
  746. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  747. ERR_FAIL_NULL(body);
  748. JoltSpace3D *space = nullptr;
  749. if (p_space.is_valid()) {
  750. space = space_owner.get_or_null(p_space);
  751. ERR_FAIL_NULL(space);
  752. }
  753. body->set_space(space);
  754. }
  755. RID JoltPhysicsServer3D::soft_body_get_space(RID p_body) const {
  756. const JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  757. ERR_FAIL_NULL_V(body, RID());
  758. const JoltSpace3D *space = body->get_space();
  759. if (space == nullptr) {
  760. return RID();
  761. }
  762. return space->get_rid();
  763. }
  764. void JoltPhysicsServer3D::soft_body_set_mesh(RID p_body, RID p_mesh) {
  765. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  766. ERR_FAIL_NULL(body);
  767. body->set_mesh(p_mesh);
  768. }
  769. AABB JoltPhysicsServer3D::soft_body_get_bounds(RID p_body) const {
  770. const JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  771. ERR_FAIL_NULL_V(body, AABB());
  772. return body->get_bounds();
  773. }
  774. void JoltPhysicsServer3D::soft_body_set_collision_layer(RID p_body, uint32_t p_layer) {
  775. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  776. ERR_FAIL_NULL(body);
  777. body->set_collision_layer(p_layer);
  778. }
  779. uint32_t JoltPhysicsServer3D::soft_body_get_collision_layer(RID p_body) const {
  780. const JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  781. ERR_FAIL_NULL_V(body, 0);
  782. return body->get_collision_layer();
  783. }
  784. void JoltPhysicsServer3D::soft_body_set_collision_mask(RID p_body, uint32_t p_mask) {
  785. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  786. ERR_FAIL_NULL(body);
  787. body->set_collision_mask(p_mask);
  788. }
  789. uint32_t JoltPhysicsServer3D::soft_body_get_collision_mask(RID p_body) const {
  790. const JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  791. ERR_FAIL_NULL_V(body, 0);
  792. return body->get_collision_mask();
  793. }
  794. void JoltPhysicsServer3D::soft_body_add_collision_exception(RID p_body, RID p_excepted_body) {
  795. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  796. ERR_FAIL_NULL(body);
  797. body->add_collision_exception(p_excepted_body);
  798. }
  799. void JoltPhysicsServer3D::soft_body_remove_collision_exception(RID p_body, RID p_excepted_body) {
  800. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  801. ERR_FAIL_NULL(body);
  802. body->remove_collision_exception(p_excepted_body);
  803. }
  804. void JoltPhysicsServer3D::soft_body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  805. const JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  806. ERR_FAIL_NULL(body);
  807. for (const RID &exception : body->get_collision_exceptions()) {
  808. p_exceptions->push_back(exception);
  809. }
  810. }
  811. void JoltPhysicsServer3D::soft_body_set_state(RID p_body, BodyState p_state, const Variant &p_value) {
  812. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  813. ERR_FAIL_NULL(body);
  814. body->set_state(p_state, p_value);
  815. }
  816. Variant JoltPhysicsServer3D::soft_body_get_state(RID p_body, BodyState p_state) const {
  817. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  818. ERR_FAIL_NULL_V(body, Variant());
  819. return body->get_state(p_state);
  820. }
  821. void JoltPhysicsServer3D::soft_body_set_transform(RID p_body, const Transform3D &p_transform) {
  822. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  823. ERR_FAIL_NULL(body);
  824. return body->set_transform(p_transform);
  825. }
  826. void JoltPhysicsServer3D::soft_body_set_ray_pickable(RID p_body, bool p_enable) {
  827. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  828. ERR_FAIL_NULL(body);
  829. return body->set_pickable(p_enable);
  830. }
  831. void JoltPhysicsServer3D::soft_body_set_simulation_precision(RID p_body, int p_precision) {
  832. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  833. ERR_FAIL_NULL(body);
  834. return body->set_simulation_precision(p_precision);
  835. }
  836. int JoltPhysicsServer3D::soft_body_get_simulation_precision(RID p_body) const {
  837. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  838. ERR_FAIL_NULL_V(body, 0);
  839. return body->get_simulation_precision();
  840. }
  841. void JoltPhysicsServer3D::soft_body_set_total_mass(RID p_body, real_t p_total_mass) {
  842. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  843. ERR_FAIL_NULL(body);
  844. return body->set_mass((float)p_total_mass);
  845. }
  846. real_t JoltPhysicsServer3D::soft_body_get_total_mass(RID p_body) const {
  847. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  848. ERR_FAIL_NULL_V(body, 0.0);
  849. return (real_t)body->get_mass();
  850. }
  851. void JoltPhysicsServer3D::soft_body_set_linear_stiffness(RID p_body, real_t p_coefficient) {
  852. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  853. ERR_FAIL_NULL(body);
  854. return body->set_stiffness_coefficient((float)p_coefficient);
  855. }
  856. real_t JoltPhysicsServer3D::soft_body_get_linear_stiffness(RID p_body) const {
  857. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  858. ERR_FAIL_NULL_V(body, 0.0);
  859. return (real_t)body->get_stiffness_coefficient();
  860. }
  861. void JoltPhysicsServer3D::soft_body_set_shrinking_factor(RID p_body, real_t p_shrinking_factor) {
  862. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  863. ERR_FAIL_NULL(body);
  864. return body->set_shrinking_factor((float)p_shrinking_factor);
  865. }
  866. real_t JoltPhysicsServer3D::soft_body_get_shrinking_factor(RID p_body) const {
  867. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  868. ERR_FAIL_NULL_V(body, 0.0);
  869. return (real_t)body->get_shrinking_factor();
  870. }
  871. void JoltPhysicsServer3D::soft_body_set_pressure_coefficient(RID p_body, real_t p_coefficient) {
  872. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  873. ERR_FAIL_NULL(body);
  874. return body->set_pressure((float)p_coefficient);
  875. }
  876. real_t JoltPhysicsServer3D::soft_body_get_pressure_coefficient(RID p_body) const {
  877. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  878. ERR_FAIL_NULL_V(body, 0.0);
  879. return (real_t)body->get_pressure();
  880. }
  881. void JoltPhysicsServer3D::soft_body_set_damping_coefficient(RID p_body, real_t p_coefficient) {
  882. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  883. ERR_FAIL_NULL(body);
  884. return body->set_linear_damping((float)p_coefficient);
  885. }
  886. real_t JoltPhysicsServer3D::soft_body_get_damping_coefficient(RID p_body) const {
  887. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  888. ERR_FAIL_NULL_V(body, 0.0);
  889. return (real_t)body->get_linear_damping();
  890. }
  891. void JoltPhysicsServer3D::soft_body_set_drag_coefficient(RID p_body, real_t p_coefficient) {
  892. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  893. ERR_FAIL_NULL(body);
  894. return body->set_drag((float)p_coefficient);
  895. }
  896. real_t JoltPhysicsServer3D::soft_body_get_drag_coefficient(RID p_body) const {
  897. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  898. ERR_FAIL_NULL_V(body, 0.0);
  899. return (real_t)body->get_drag();
  900. }
  901. void JoltPhysicsServer3D::soft_body_move_point(RID p_body, int p_point_index, const Vector3 &p_global_position) {
  902. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  903. ERR_FAIL_NULL(body);
  904. body->set_vertex_position(p_point_index, p_global_position);
  905. }
  906. Vector3 JoltPhysicsServer3D::soft_body_get_point_global_position(RID p_body, int p_point_index) const {
  907. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  908. ERR_FAIL_NULL_V(body, Vector3());
  909. return body->get_vertex_position(p_point_index);
  910. }
  911. void JoltPhysicsServer3D::soft_body_remove_all_pinned_points(RID p_body) {
  912. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  913. ERR_FAIL_NULL(body);
  914. body->unpin_all_vertices();
  915. }
  916. void JoltPhysicsServer3D::soft_body_pin_point(RID p_body, int p_point_index, bool p_pin) {
  917. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  918. ERR_FAIL_NULL(body);
  919. if (p_pin) {
  920. body->pin_vertex(p_point_index);
  921. } else {
  922. body->unpin_vertex(p_point_index);
  923. }
  924. }
  925. bool JoltPhysicsServer3D::soft_body_is_point_pinned(RID p_body, int p_point_index) const {
  926. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  927. ERR_FAIL_NULL_V(body, false);
  928. return body->is_vertex_pinned(p_point_index);
  929. }
  930. RID JoltPhysicsServer3D::joint_create() {
  931. JoltJoint3D *joint = memnew(JoltJoint3D);
  932. RID rid = joint_owner.make_rid(joint);
  933. joint->set_rid(rid);
  934. return rid;
  935. }
  936. void JoltPhysicsServer3D::joint_clear(RID p_joint) {
  937. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  938. ERR_FAIL_NULL(joint);
  939. if (joint->get_type() != JOINT_TYPE_MAX) {
  940. JoltJoint3D *empty_joint = memnew(JoltJoint3D);
  941. empty_joint->set_rid(joint->get_rid());
  942. memdelete(joint);
  943. joint = nullptr;
  944. joint_owner.replace(p_joint, empty_joint);
  945. }
  946. }
  947. void JoltPhysicsServer3D::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. JoltJoint3D *old_joint = joint_owner.get_or_null(p_joint);
  949. ERR_FAIL_NULL(old_joint);
  950. JoltBody3D *body_a = body_owner.get_or_null(p_body_a);
  951. ERR_FAIL_NULL(body_a);
  952. JoltBody3D *body_b = body_owner.get_or_null(p_body_b);
  953. ERR_FAIL_COND(body_a == body_b);
  954. JoltJoint3D *new_joint = memnew(JoltPinJoint3D(*old_joint, body_a, body_b, p_local_a, p_local_b));
  955. memdelete(old_joint);
  956. old_joint = nullptr;
  957. joint_owner.replace(p_joint, new_joint);
  958. }
  959. void JoltPhysicsServer3D::pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) {
  960. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  961. ERR_FAIL_NULL(joint);
  962. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
  963. JoltPinJoint3D *pin_joint = static_cast<JoltPinJoint3D *>(joint);
  964. pin_joint->set_param(p_param, (double)p_value);
  965. }
  966. real_t JoltPhysicsServer3D::pin_joint_get_param(RID p_joint, PinJointParam p_param) const {
  967. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  968. ERR_FAIL_NULL_V(joint, 0.0);
  969. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, 0.0);
  970. const JoltPinJoint3D *pin_joint = static_cast<const JoltPinJoint3D *>(joint);
  971. return (real_t)pin_joint->get_param(p_param);
  972. }
  973. void JoltPhysicsServer3D::pin_joint_set_local_a(RID p_joint, const Vector3 &p_local_a) {
  974. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  975. ERR_FAIL_NULL(joint);
  976. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
  977. JoltPinJoint3D *pin_joint = static_cast<JoltPinJoint3D *>(joint);
  978. pin_joint->set_local_a(p_local_a);
  979. }
  980. Vector3 JoltPhysicsServer3D::pin_joint_get_local_a(RID p_joint) const {
  981. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  982. ERR_FAIL_NULL_V(joint, Vector3());
  983. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, Vector3());
  984. const JoltPinJoint3D *pin_joint = static_cast<const JoltPinJoint3D *>(joint);
  985. return pin_joint->get_local_a();
  986. }
  987. void JoltPhysicsServer3D::pin_joint_set_local_b(RID p_joint, const Vector3 &p_local_b) {
  988. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  989. ERR_FAIL_NULL(joint);
  990. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
  991. JoltPinJoint3D *pin_joint = static_cast<JoltPinJoint3D *>(joint);
  992. pin_joint->set_local_b(p_local_b);
  993. }
  994. Vector3 JoltPhysicsServer3D::pin_joint_get_local_b(RID p_joint) const {
  995. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  996. ERR_FAIL_NULL_V(joint, Vector3());
  997. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, Vector3());
  998. const JoltPinJoint3D *pin_joint = static_cast<const JoltPinJoint3D *>(joint);
  999. return pin_joint->get_local_b();
  1000. }
  1001. void JoltPhysicsServer3D::joint_make_hinge(RID p_joint, RID p_body_a, const Transform3D &p_hinge_a, RID p_body_b, const Transform3D &p_hinge_b) {
  1002. JoltJoint3D *old_joint = joint_owner.get_or_null(p_joint);
  1003. ERR_FAIL_NULL(old_joint);
  1004. JoltBody3D *body_a = body_owner.get_or_null(p_body_a);
  1005. ERR_FAIL_NULL(body_a);
  1006. JoltBody3D *body_b = body_owner.get_or_null(p_body_b);
  1007. ERR_FAIL_COND(body_a == body_b);
  1008. JoltJoint3D *new_joint = memnew(JoltHingeJoint3D(*old_joint, body_a, body_b, p_hinge_a, p_hinge_b));
  1009. memdelete(old_joint);
  1010. old_joint = nullptr;
  1011. joint_owner.replace(p_joint, new_joint);
  1012. }
  1013. void JoltPhysicsServer3D::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) {
  1014. ERR_FAIL_MSG("Simple hinge joints are not supported when using Jolt Physics.");
  1015. }
  1016. void JoltPhysicsServer3D::hinge_joint_set_param(RID p_joint, HingeJointParam p_param, real_t p_value) {
  1017. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1018. ERR_FAIL_NULL(joint);
  1019. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_HINGE);
  1020. JoltHingeJoint3D *hinge_joint = static_cast<JoltHingeJoint3D *>(joint);
  1021. return hinge_joint->set_param(p_param, (double)p_value);
  1022. }
  1023. real_t JoltPhysicsServer3D::hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const {
  1024. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1025. ERR_FAIL_NULL_V(joint, 0.0);
  1026. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, 0.0);
  1027. const JoltHingeJoint3D *hinge_joint = static_cast<const JoltHingeJoint3D *>(joint);
  1028. return (real_t)hinge_joint->get_param(p_param);
  1029. }
  1030. void JoltPhysicsServer3D::hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_enabled) {
  1031. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1032. ERR_FAIL_NULL(joint);
  1033. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_HINGE);
  1034. JoltHingeJoint3D *hinge_joint = static_cast<JoltHingeJoint3D *>(joint);
  1035. return hinge_joint->set_flag(p_flag, p_enabled);
  1036. }
  1037. bool JoltPhysicsServer3D::hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const {
  1038. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1039. ERR_FAIL_NULL_V(joint, false);
  1040. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, false);
  1041. const JoltHingeJoint3D *hinge_joint = static_cast<const JoltHingeJoint3D *>(joint);
  1042. return hinge_joint->get_flag(p_flag);
  1043. }
  1044. void JoltPhysicsServer3D::joint_make_slider(RID p_joint, RID p_body_a, const Transform3D &p_local_ref_a, RID p_body_b, const Transform3D &p_local_ref_b) {
  1045. JoltJoint3D *old_joint = joint_owner.get_or_null(p_joint);
  1046. ERR_FAIL_NULL(old_joint);
  1047. JoltBody3D *body_a = body_owner.get_or_null(p_body_a);
  1048. ERR_FAIL_NULL(body_a);
  1049. JoltBody3D *body_b = body_owner.get_or_null(p_body_b);
  1050. ERR_FAIL_COND(body_a == body_b);
  1051. JoltJoint3D *new_joint = memnew(JoltSliderJoint3D(*old_joint, body_a, body_b, p_local_ref_a, p_local_ref_b));
  1052. memdelete(old_joint);
  1053. old_joint = nullptr;
  1054. joint_owner.replace(p_joint, new_joint);
  1055. }
  1056. void JoltPhysicsServer3D::slider_joint_set_param(RID p_joint, SliderJointParam p_param, real_t p_value) {
  1057. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1058. ERR_FAIL_NULL(joint);
  1059. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_SLIDER);
  1060. JoltSliderJoint3D *slider_joint = static_cast<JoltSliderJoint3D *>(joint);
  1061. return slider_joint->set_param(p_param, (real_t)p_value);
  1062. }
  1063. real_t JoltPhysicsServer3D::slider_joint_get_param(RID p_joint, SliderJointParam p_param) const {
  1064. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1065. ERR_FAIL_NULL_V(joint, 0.0);
  1066. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_SLIDER, 0.0);
  1067. const JoltSliderJoint3D *slider_joint = static_cast<const JoltSliderJoint3D *>(joint);
  1068. return slider_joint->get_param(p_param);
  1069. }
  1070. void JoltPhysicsServer3D::joint_make_cone_twist(RID p_joint, RID p_body_a, const Transform3D &p_local_ref_a, RID p_body_b, const Transform3D &p_local_ref_b) {
  1071. JoltJoint3D *old_joint = joint_owner.get_or_null(p_joint);
  1072. ERR_FAIL_NULL(old_joint);
  1073. JoltBody3D *body_a = body_owner.get_or_null(p_body_a);
  1074. ERR_FAIL_NULL(body_a);
  1075. JoltBody3D *body_b = body_owner.get_or_null(p_body_b);
  1076. ERR_FAIL_COND(body_a == body_b);
  1077. JoltJoint3D *new_joint = memnew(JoltConeTwistJoint3D(*old_joint, body_a, body_b, p_local_ref_a, p_local_ref_b));
  1078. memdelete(old_joint);
  1079. old_joint = nullptr;
  1080. joint_owner.replace(p_joint, new_joint);
  1081. }
  1082. void JoltPhysicsServer3D::cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, real_t p_value) {
  1083. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1084. ERR_FAIL_NULL(joint);
  1085. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_CONE_TWIST);
  1086. JoltConeTwistJoint3D *cone_twist_joint = static_cast<JoltConeTwistJoint3D *>(joint);
  1087. return cone_twist_joint->set_param(p_param, (double)p_value);
  1088. }
  1089. real_t JoltPhysicsServer3D::cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const {
  1090. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1091. ERR_FAIL_NULL_V(joint, 0.0);
  1092. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, 0.0);
  1093. const JoltConeTwistJoint3D *cone_twist_joint = static_cast<const JoltConeTwistJoint3D *>(joint);
  1094. return (real_t)cone_twist_joint->get_param(p_param);
  1095. }
  1096. void JoltPhysicsServer3D::joint_make_generic_6dof(RID p_joint, RID p_body_a, const Transform3D &p_local_ref_a, RID p_body_b, const Transform3D &p_local_ref_b) {
  1097. JoltJoint3D *old_joint = joint_owner.get_or_null(p_joint);
  1098. ERR_FAIL_NULL(old_joint);
  1099. JoltBody3D *body_a = body_owner.get_or_null(p_body_a);
  1100. ERR_FAIL_NULL(body_a);
  1101. JoltBody3D *body_b = body_owner.get_or_null(p_body_b);
  1102. ERR_FAIL_COND(body_a == body_b);
  1103. JoltJoint3D *new_joint = memnew(JoltGeneric6DOFJoint3D(*old_joint, body_a, body_b, p_local_ref_a, p_local_ref_b));
  1104. memdelete(old_joint);
  1105. old_joint = nullptr;
  1106. joint_owner.replace(p_joint, new_joint);
  1107. }
  1108. void JoltPhysicsServer3D::generic_6dof_joint_set_param(RID p_joint, Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisParam p_param, real_t p_value) {
  1109. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1110. ERR_FAIL_NULL(joint);
  1111. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_6DOF);
  1112. JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<JoltGeneric6DOFJoint3D *>(joint);
  1113. return g6dof_joint->set_param(p_axis, p_param, (double)p_value);
  1114. }
  1115. real_t JoltPhysicsServer3D::generic_6dof_joint_get_param(RID p_joint, Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisParam p_param) const {
  1116. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1117. ERR_FAIL_NULL_V(joint, 0.0);
  1118. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, 0.0);
  1119. const JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<const JoltGeneric6DOFJoint3D *>(joint);
  1120. return (real_t)g6dof_joint->get_param(p_axis, p_param);
  1121. }
  1122. void JoltPhysicsServer3D::generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisFlag p_flag, bool p_enable) {
  1123. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1124. ERR_FAIL_NULL(joint);
  1125. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_6DOF);
  1126. JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<JoltGeneric6DOFJoint3D *>(joint);
  1127. return g6dof_joint->set_flag(p_axis, p_flag, p_enable);
  1128. }
  1129. bool JoltPhysicsServer3D::generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisFlag p_flag) const {
  1130. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1131. ERR_FAIL_NULL_V(joint, false);
  1132. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, false);
  1133. const JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<const JoltGeneric6DOFJoint3D *>(joint);
  1134. return g6dof_joint->get_flag(p_axis, p_flag);
  1135. }
  1136. PhysicsServer3D::JointType JoltPhysicsServer3D::joint_get_type(RID p_joint) const {
  1137. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1138. ERR_FAIL_NULL_V(joint, JOINT_TYPE_PIN);
  1139. return joint->get_type();
  1140. }
  1141. void JoltPhysicsServer3D::joint_set_solver_priority(RID p_joint, int p_priority) {
  1142. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1143. ERR_FAIL_NULL(joint);
  1144. joint->set_solver_priority(p_priority);
  1145. }
  1146. int JoltPhysicsServer3D::joint_get_solver_priority(RID p_joint) const {
  1147. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1148. ERR_FAIL_NULL_V(joint, 0);
  1149. return joint->get_solver_priority();
  1150. }
  1151. void JoltPhysicsServer3D::joint_disable_collisions_between_bodies(RID p_joint, bool p_disable) {
  1152. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1153. ERR_FAIL_NULL(joint);
  1154. joint->set_collision_disabled(p_disable);
  1155. }
  1156. bool JoltPhysicsServer3D::joint_is_disabled_collisions_between_bodies(RID p_joint) const {
  1157. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1158. ERR_FAIL_NULL_V(joint, false);
  1159. return joint->is_collision_disabled();
  1160. }
  1161. void JoltPhysicsServer3D::free_rid(RID p_rid) {
  1162. if (JoltShape3D *shape = shape_owner.get_or_null(p_rid)) {
  1163. free_shape(shape);
  1164. } else if (JoltBody3D *body = body_owner.get_or_null(p_rid)) {
  1165. free_body(body);
  1166. } else if (JoltJoint3D *joint = joint_owner.get_or_null(p_rid)) {
  1167. free_joint(joint);
  1168. } else if (JoltArea3D *area = area_owner.get_or_null(p_rid)) {
  1169. free_area(area);
  1170. } else if (JoltSoftBody3D *soft_body = soft_body_owner.get_or_null(p_rid)) {
  1171. free_soft_body(soft_body);
  1172. } else if (JoltSpace3D *space = space_owner.get_or_null(p_rid)) {
  1173. free_space(space);
  1174. } else {
  1175. ERR_FAIL_MSG("Failed to free RID: The specified RID has no owner.");
  1176. }
  1177. }
  1178. void JoltPhysicsServer3D::set_active(bool p_active) {
  1179. active = p_active;
  1180. }
  1181. void JoltPhysicsServer3D::init() {
  1182. job_system = new JoltJobSystem();
  1183. }
  1184. void JoltPhysicsServer3D::finish() {
  1185. if (job_system != nullptr) {
  1186. delete job_system;
  1187. job_system = nullptr;
  1188. }
  1189. }
  1190. void JoltPhysicsServer3D::step(real_t p_step) {
  1191. if (!active) {
  1192. return;
  1193. }
  1194. for (JoltSpace3D *active_space : active_spaces) {
  1195. job_system->pre_step();
  1196. active_space->step((float)p_step);
  1197. job_system->post_step();
  1198. }
  1199. }
  1200. void JoltPhysicsServer3D::sync() {
  1201. doing_sync = true;
  1202. }
  1203. void JoltPhysicsServer3D::end_sync() {
  1204. doing_sync = false;
  1205. }
  1206. void JoltPhysicsServer3D::flush_queries() {
  1207. if (!active) {
  1208. return;
  1209. }
  1210. flushing_queries = true;
  1211. for (JoltSpace3D *space : active_spaces) {
  1212. space->call_queries();
  1213. }
  1214. flushing_queries = false;
  1215. #ifdef DEBUG_ENABLED
  1216. job_system->flush_timings();
  1217. #endif
  1218. }
  1219. bool JoltPhysicsServer3D::is_flushing_queries() const {
  1220. return flushing_queries;
  1221. }
  1222. int JoltPhysicsServer3D::get_process_info(ProcessInfo p_process_info) {
  1223. return 0;
  1224. }
  1225. void JoltPhysicsServer3D::free_space(JoltSpace3D *p_space) {
  1226. ERR_FAIL_NULL(p_space);
  1227. free_area(p_space->get_default_area());
  1228. space_set_active(p_space->get_rid(), false);
  1229. space_owner.free(p_space->get_rid());
  1230. memdelete(p_space);
  1231. }
  1232. void JoltPhysicsServer3D::free_area(JoltArea3D *p_area) {
  1233. ERR_FAIL_NULL(p_area);
  1234. p_area->set_space(nullptr);
  1235. area_owner.free(p_area->get_rid());
  1236. memdelete(p_area);
  1237. }
  1238. void JoltPhysicsServer3D::free_body(JoltBody3D *p_body) {
  1239. ERR_FAIL_NULL(p_body);
  1240. p_body->set_space(nullptr);
  1241. body_owner.free(p_body->get_rid());
  1242. memdelete(p_body);
  1243. }
  1244. void JoltPhysicsServer3D::free_soft_body(JoltSoftBody3D *p_body) {
  1245. ERR_FAIL_NULL(p_body);
  1246. p_body->set_space(nullptr);
  1247. soft_body_owner.free(p_body->get_rid());
  1248. memdelete(p_body);
  1249. }
  1250. void JoltPhysicsServer3D::free_shape(JoltShape3D *p_shape) {
  1251. ERR_FAIL_NULL(p_shape);
  1252. p_shape->remove_self();
  1253. shape_owner.free(p_shape->get_rid());
  1254. memdelete(p_shape);
  1255. }
  1256. void JoltPhysicsServer3D::free_joint(JoltJoint3D *p_joint) {
  1257. ERR_FAIL_NULL(p_joint);
  1258. joint_owner.free(p_joint->get_rid());
  1259. memdelete(p_joint);
  1260. }
  1261. #ifdef DEBUG_ENABLED
  1262. void JoltPhysicsServer3D::dump_debug_snapshots(const String &p_dir) {
  1263. for (JoltSpace3D *space : active_spaces) {
  1264. space->dump_debug_snapshot(p_dir);
  1265. }
  1266. }
  1267. void JoltPhysicsServer3D::space_dump_debug_snapshot(RID p_space, const String &p_dir) {
  1268. JoltSpace3D *space = space_owner.get_or_null(p_space);
  1269. ERR_FAIL_NULL(space);
  1270. space->dump_debug_snapshot(p_dir);
  1271. }
  1272. #endif
  1273. bool JoltPhysicsServer3D::joint_get_enabled(RID p_joint) const {
  1274. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1275. ERR_FAIL_NULL_V(joint, false);
  1276. return joint->is_enabled();
  1277. }
  1278. void JoltPhysicsServer3D::joint_set_enabled(RID p_joint, bool p_enabled) {
  1279. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1280. ERR_FAIL_NULL(joint);
  1281. joint->set_enabled(p_enabled);
  1282. }
  1283. int JoltPhysicsServer3D::joint_get_solver_velocity_iterations(RID p_joint) {
  1284. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1285. ERR_FAIL_NULL_V(joint, 0);
  1286. return joint->get_solver_velocity_iterations();
  1287. }
  1288. void JoltPhysicsServer3D::joint_set_solver_velocity_iterations(RID p_joint, int p_value) {
  1289. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1290. ERR_FAIL_NULL(joint);
  1291. return joint->set_solver_velocity_iterations(p_value);
  1292. }
  1293. int JoltPhysicsServer3D::joint_get_solver_position_iterations(RID p_joint) {
  1294. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1295. ERR_FAIL_NULL_V(joint, 0);
  1296. return joint->get_solver_position_iterations();
  1297. }
  1298. void JoltPhysicsServer3D::joint_set_solver_position_iterations(RID p_joint, int p_value) {
  1299. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1300. ERR_FAIL_NULL(joint);
  1301. return joint->set_solver_position_iterations(p_value);
  1302. }
  1303. float JoltPhysicsServer3D::pin_joint_get_applied_force(RID p_joint) {
  1304. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1305. ERR_FAIL_NULL_V(joint, 0.0f);
  1306. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, 0.0);
  1307. JoltPinJoint3D *pin_joint = static_cast<JoltPinJoint3D *>(joint);
  1308. return pin_joint->get_applied_force();
  1309. }
  1310. double JoltPhysicsServer3D::hinge_joint_get_jolt_param(RID p_joint, HingeJointParamJolt p_param) const {
  1311. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1312. ERR_FAIL_NULL_V(joint, 0.0);
  1313. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, 0.0);
  1314. JoltHingeJoint3D *hinge_joint = static_cast<JoltHingeJoint3D *>(joint);
  1315. return hinge_joint->get_jolt_param(p_param);
  1316. }
  1317. void JoltPhysicsServer3D::hinge_joint_set_jolt_param(RID p_joint, HingeJointParamJolt p_param, double p_value) {
  1318. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1319. ERR_FAIL_NULL(joint);
  1320. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_HINGE);
  1321. JoltHingeJoint3D *hinge_joint = static_cast<JoltHingeJoint3D *>(joint);
  1322. return hinge_joint->set_jolt_param(p_param, p_value);
  1323. }
  1324. bool JoltPhysicsServer3D::hinge_joint_get_jolt_flag(RID p_joint, HingeJointFlagJolt p_flag) const {
  1325. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1326. ERR_FAIL_NULL_V(joint, false);
  1327. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, false);
  1328. const JoltHingeJoint3D *hinge_joint = static_cast<const JoltHingeJoint3D *>(joint);
  1329. return hinge_joint->get_jolt_flag(p_flag);
  1330. }
  1331. void JoltPhysicsServer3D::hinge_joint_set_jolt_flag(RID p_joint, HingeJointFlagJolt p_flag, bool p_enabled) {
  1332. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1333. ERR_FAIL_NULL(joint);
  1334. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_HINGE);
  1335. JoltHingeJoint3D *hinge_joint = static_cast<JoltHingeJoint3D *>(joint);
  1336. return hinge_joint->set_jolt_flag(p_flag, p_enabled);
  1337. }
  1338. float JoltPhysicsServer3D::hinge_joint_get_applied_force(RID p_joint) {
  1339. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1340. ERR_FAIL_NULL_V(joint, 0.0f);
  1341. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, 0.0f);
  1342. JoltHingeJoint3D *hinge_joint = static_cast<JoltHingeJoint3D *>(joint);
  1343. return hinge_joint->get_applied_force();
  1344. }
  1345. float JoltPhysicsServer3D::hinge_joint_get_applied_torque(RID p_joint) {
  1346. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1347. ERR_FAIL_NULL_V(joint, 0.0f);
  1348. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, 0.0f);
  1349. JoltHingeJoint3D *hinge_joint = static_cast<JoltHingeJoint3D *>(joint);
  1350. return hinge_joint->get_applied_torque();
  1351. }
  1352. double JoltPhysicsServer3D::slider_joint_get_jolt_param(RID p_joint, SliderJointParamJolt p_param) const {
  1353. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1354. ERR_FAIL_NULL_V(joint, 0.0);
  1355. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_SLIDER, 0.0);
  1356. JoltSliderJoint3D *slider_joint = static_cast<JoltSliderJoint3D *>(joint);
  1357. return slider_joint->get_jolt_param(p_param);
  1358. }
  1359. void JoltPhysicsServer3D::slider_joint_set_jolt_param(RID p_joint, SliderJointParamJolt p_param, double p_value) {
  1360. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1361. ERR_FAIL_NULL(joint);
  1362. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_SLIDER);
  1363. JoltSliderJoint3D *slider_joint = static_cast<JoltSliderJoint3D *>(joint);
  1364. return slider_joint->set_jolt_param(p_param, p_value);
  1365. }
  1366. bool JoltPhysicsServer3D::slider_joint_get_jolt_flag(RID p_joint, SliderJointFlagJolt p_flag) const {
  1367. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1368. ERR_FAIL_NULL_V(joint, false);
  1369. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_SLIDER, false);
  1370. const JoltSliderJoint3D *slider_joint = static_cast<const JoltSliderJoint3D *>(joint);
  1371. return slider_joint->get_jolt_flag(p_flag);
  1372. }
  1373. void JoltPhysicsServer3D::slider_joint_set_jolt_flag(RID p_joint, SliderJointFlagJolt p_flag, bool p_enabled) {
  1374. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1375. ERR_FAIL_NULL(joint);
  1376. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_SLIDER);
  1377. JoltSliderJoint3D *slider_joint = static_cast<JoltSliderJoint3D *>(joint);
  1378. return slider_joint->set_jolt_flag(p_flag, p_enabled);
  1379. }
  1380. float JoltPhysicsServer3D::slider_joint_get_applied_force(RID p_joint) {
  1381. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1382. ERR_FAIL_NULL_V(joint, 0.0f);
  1383. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_SLIDER, 0.0f);
  1384. JoltSliderJoint3D *slider_joint = static_cast<JoltSliderJoint3D *>(joint);
  1385. return slider_joint->get_applied_force();
  1386. }
  1387. float JoltPhysicsServer3D::slider_joint_get_applied_torque(RID p_joint) {
  1388. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1389. ERR_FAIL_NULL_V(joint, 0.0f);
  1390. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_SLIDER, 0.0f);
  1391. JoltSliderJoint3D *slider_joint = static_cast<JoltSliderJoint3D *>(joint);
  1392. return slider_joint->get_applied_torque();
  1393. }
  1394. double JoltPhysicsServer3D::cone_twist_joint_get_jolt_param(RID p_joint, ConeTwistJointParamJolt p_param) const {
  1395. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1396. ERR_FAIL_NULL_V(joint, 0.0);
  1397. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, 0.0);
  1398. JoltConeTwistJoint3D *cone_twist_joint = static_cast<JoltConeTwistJoint3D *>(joint);
  1399. return cone_twist_joint->get_jolt_param(p_param);
  1400. }
  1401. void JoltPhysicsServer3D::cone_twist_joint_set_jolt_param(RID p_joint, ConeTwistJointParamJolt p_param, double p_value) {
  1402. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1403. ERR_FAIL_NULL(joint);
  1404. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_CONE_TWIST);
  1405. JoltConeTwistJoint3D *cone_twist_joint = static_cast<JoltConeTwistJoint3D *>(joint);
  1406. return cone_twist_joint->set_jolt_param(p_param, p_value);
  1407. }
  1408. bool JoltPhysicsServer3D::cone_twist_joint_get_jolt_flag(RID p_joint, ConeTwistJointFlagJolt p_flag) const {
  1409. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1410. ERR_FAIL_NULL_V(joint, false);
  1411. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, false);
  1412. const JoltConeTwistJoint3D *cone_twist_joint = static_cast<const JoltConeTwistJoint3D *>(joint);
  1413. return cone_twist_joint->get_jolt_flag(p_flag);
  1414. }
  1415. void JoltPhysicsServer3D::cone_twist_joint_set_jolt_flag(RID p_joint, ConeTwistJointFlagJolt p_flag, bool p_enabled) {
  1416. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1417. ERR_FAIL_NULL(joint);
  1418. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_CONE_TWIST);
  1419. JoltConeTwistJoint3D *cone_twist_joint = static_cast<JoltConeTwistJoint3D *>(joint);
  1420. return cone_twist_joint->set_jolt_flag(p_flag, p_enabled);
  1421. }
  1422. float JoltPhysicsServer3D::cone_twist_joint_get_applied_force(RID p_joint) {
  1423. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1424. ERR_FAIL_NULL_V(joint, 0.0f);
  1425. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, 0.0f);
  1426. JoltConeTwistJoint3D *cone_twist_joint = static_cast<JoltConeTwistJoint3D *>(joint);
  1427. return cone_twist_joint->get_applied_force();
  1428. }
  1429. float JoltPhysicsServer3D::cone_twist_joint_get_applied_torque(RID p_joint) {
  1430. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1431. ERR_FAIL_NULL_V(joint, 0.0f);
  1432. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, 0.0f);
  1433. JoltConeTwistJoint3D *cone_twist_joint = static_cast<JoltConeTwistJoint3D *>(joint);
  1434. return cone_twist_joint->get_applied_torque();
  1435. }
  1436. double JoltPhysicsServer3D::generic_6dof_joint_get_jolt_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParamJolt p_param) const {
  1437. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1438. ERR_FAIL_NULL_V(joint, 0.0);
  1439. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, 0.0);
  1440. JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<JoltGeneric6DOFJoint3D *>(joint);
  1441. return g6dof_joint->get_jolt_param(p_axis, p_param);
  1442. }
  1443. void JoltPhysicsServer3D::generic_6dof_joint_set_jolt_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParamJolt p_param, double p_value) {
  1444. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1445. ERR_FAIL_NULL(joint);
  1446. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_6DOF);
  1447. JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<JoltGeneric6DOFJoint3D *>(joint);
  1448. return g6dof_joint->set_jolt_param(p_axis, p_param, p_value);
  1449. }
  1450. bool JoltPhysicsServer3D::generic_6dof_joint_get_jolt_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlagJolt p_flag) const {
  1451. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1452. ERR_FAIL_NULL_V(joint, false);
  1453. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, false);
  1454. const JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<const JoltGeneric6DOFJoint3D *>(joint);
  1455. return g6dof_joint->get_jolt_flag(p_axis, p_flag);
  1456. }
  1457. void JoltPhysicsServer3D::generic_6dof_joint_set_jolt_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlagJolt p_flag, bool p_enabled) {
  1458. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1459. ERR_FAIL_NULL(joint);
  1460. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_6DOF);
  1461. JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<JoltGeneric6DOFJoint3D *>(joint);
  1462. return g6dof_joint->set_jolt_flag(p_axis, p_flag, p_enabled);
  1463. }
  1464. float JoltPhysicsServer3D::generic_6dof_joint_get_applied_force(RID p_joint) {
  1465. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1466. ERR_FAIL_NULL_V(joint, 0.0f);
  1467. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, 0.0f);
  1468. JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<JoltGeneric6DOFJoint3D *>(joint);
  1469. return g6dof_joint->get_applied_force();
  1470. }
  1471. float JoltPhysicsServer3D::generic_6dof_joint_get_applied_torque(RID p_joint) {
  1472. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1473. ERR_FAIL_NULL_V(joint, 0.0f);
  1474. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, 0.0f);
  1475. JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<JoltGeneric6DOFJoint3D *>(joint);
  1476. return g6dof_joint->get_applied_torque();
  1477. }