jolt_physics_server_3d.cpp 62 KB

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