jolt_physics_server_3d.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019
  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. 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, PhysicsServer3DRenderingServerHandler *p_rendering_server_handler) {
  740. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  741. ERR_FAIL_NULL(body);
  742. return body->update_rendering_server(p_rendering_server_handler);
  743. }
  744. void JoltPhysicsServer3D::soft_body_set_space(RID p_body, RID p_space) {
  745. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  746. ERR_FAIL_NULL(body);
  747. JoltSpace3D *space = nullptr;
  748. if (p_space.is_valid()) {
  749. space = space_owner.get_or_null(p_space);
  750. ERR_FAIL_NULL(space);
  751. }
  752. body->set_space(space);
  753. }
  754. RID JoltPhysicsServer3D::soft_body_get_space(RID p_body) const {
  755. const JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  756. ERR_FAIL_NULL_V(body, RID());
  757. const JoltSpace3D *space = body->get_space();
  758. if (space == nullptr) {
  759. return RID();
  760. }
  761. return space->get_rid();
  762. }
  763. void JoltPhysicsServer3D::soft_body_set_mesh(RID p_body, RID p_mesh) {
  764. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  765. ERR_FAIL_NULL(body);
  766. body->set_mesh(p_mesh);
  767. }
  768. AABB JoltPhysicsServer3D::soft_body_get_bounds(RID p_body) const {
  769. const JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  770. ERR_FAIL_NULL_V(body, AABB());
  771. return body->get_bounds();
  772. }
  773. void JoltPhysicsServer3D::soft_body_set_collision_layer(RID p_body, uint32_t p_layer) {
  774. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  775. ERR_FAIL_NULL(body);
  776. body->set_collision_layer(p_layer);
  777. }
  778. uint32_t JoltPhysicsServer3D::soft_body_get_collision_layer(RID p_body) const {
  779. const JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  780. ERR_FAIL_NULL_V(body, 0);
  781. return body->get_collision_layer();
  782. }
  783. void JoltPhysicsServer3D::soft_body_set_collision_mask(RID p_body, uint32_t p_mask) {
  784. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  785. ERR_FAIL_NULL(body);
  786. body->set_collision_mask(p_mask);
  787. }
  788. uint32_t JoltPhysicsServer3D::soft_body_get_collision_mask(RID p_body) const {
  789. const JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  790. ERR_FAIL_NULL_V(body, 0);
  791. return body->get_collision_mask();
  792. }
  793. void JoltPhysicsServer3D::soft_body_add_collision_exception(RID p_body, RID p_excepted_body) {
  794. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  795. ERR_FAIL_NULL(body);
  796. body->add_collision_exception(p_excepted_body);
  797. }
  798. void JoltPhysicsServer3D::soft_body_remove_collision_exception(RID p_body, RID p_excepted_body) {
  799. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  800. ERR_FAIL_NULL(body);
  801. body->remove_collision_exception(p_excepted_body);
  802. }
  803. void JoltPhysicsServer3D::soft_body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  804. const JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  805. ERR_FAIL_NULL(body);
  806. for (const RID &exception : body->get_collision_exceptions()) {
  807. p_exceptions->push_back(exception);
  808. }
  809. }
  810. void JoltPhysicsServer3D::soft_body_set_state(RID p_body, BodyState p_state, const Variant &p_value) {
  811. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  812. ERR_FAIL_NULL(body);
  813. body->set_state(p_state, p_value);
  814. }
  815. Variant JoltPhysicsServer3D::soft_body_get_state(RID p_body, BodyState p_state) const {
  816. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  817. ERR_FAIL_NULL_V(body, Variant());
  818. return body->get_state(p_state);
  819. }
  820. void JoltPhysicsServer3D::soft_body_set_transform(RID p_body, const Transform3D &p_transform) {
  821. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  822. ERR_FAIL_NULL(body);
  823. return body->set_transform(p_transform);
  824. }
  825. void JoltPhysicsServer3D::soft_body_set_ray_pickable(RID p_body, bool p_enable) {
  826. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  827. ERR_FAIL_NULL(body);
  828. return body->set_pickable(p_enable);
  829. }
  830. void JoltPhysicsServer3D::soft_body_set_simulation_precision(RID p_body, int p_precision) {
  831. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  832. ERR_FAIL_NULL(body);
  833. return body->set_simulation_precision(p_precision);
  834. }
  835. int JoltPhysicsServer3D::soft_body_get_simulation_precision(RID p_body) const {
  836. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  837. ERR_FAIL_NULL_V(body, 0);
  838. return body->get_simulation_precision();
  839. }
  840. void JoltPhysicsServer3D::soft_body_set_total_mass(RID p_body, real_t p_total_mass) {
  841. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  842. ERR_FAIL_NULL(body);
  843. return body->set_mass((float)p_total_mass);
  844. }
  845. real_t JoltPhysicsServer3D::soft_body_get_total_mass(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_mass();
  849. }
  850. void JoltPhysicsServer3D::soft_body_set_linear_stiffness(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_stiffness_coefficient((float)p_coefficient);
  854. }
  855. real_t JoltPhysicsServer3D::soft_body_get_linear_stiffness(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_stiffness_coefficient();
  859. }
  860. void JoltPhysicsServer3D::soft_body_set_shrinking_factor(RID p_body, real_t p_shrinking_factor) {
  861. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  862. ERR_FAIL_NULL(body);
  863. return body->set_shrinking_factor((float)p_shrinking_factor);
  864. }
  865. real_t JoltPhysicsServer3D::soft_body_get_shrinking_factor(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_shrinking_factor();
  869. }
  870. void JoltPhysicsServer3D::soft_body_set_pressure_coefficient(RID p_body, real_t p_coefficient) {
  871. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  872. ERR_FAIL_NULL(body);
  873. return body->set_pressure((float)p_coefficient);
  874. }
  875. real_t JoltPhysicsServer3D::soft_body_get_pressure_coefficient(RID p_body) const {
  876. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  877. ERR_FAIL_NULL_V(body, 0.0);
  878. return (real_t)body->get_pressure();
  879. }
  880. void JoltPhysicsServer3D::soft_body_set_damping_coefficient(RID p_body, real_t p_coefficient) {
  881. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  882. ERR_FAIL_NULL(body);
  883. return body->set_linear_damping((float)p_coefficient);
  884. }
  885. real_t JoltPhysicsServer3D::soft_body_get_damping_coefficient(RID p_body) const {
  886. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  887. ERR_FAIL_NULL_V(body, 0.0);
  888. return (real_t)body->get_linear_damping();
  889. }
  890. void JoltPhysicsServer3D::soft_body_set_drag_coefficient(RID p_body, real_t p_coefficient) {
  891. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  892. ERR_FAIL_NULL(body);
  893. return body->set_drag((float)p_coefficient);
  894. }
  895. real_t JoltPhysicsServer3D::soft_body_get_drag_coefficient(RID p_body) const {
  896. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  897. ERR_FAIL_NULL_V(body, 0.0);
  898. return (real_t)body->get_drag();
  899. }
  900. void JoltPhysicsServer3D::soft_body_move_point(RID p_body, int p_point_index, const Vector3 &p_global_position) {
  901. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  902. ERR_FAIL_NULL(body);
  903. body->set_vertex_position(p_point_index, p_global_position);
  904. }
  905. Vector3 JoltPhysicsServer3D::soft_body_get_point_global_position(RID p_body, int p_point_index) const {
  906. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  907. ERR_FAIL_NULL_V(body, Vector3());
  908. return body->get_vertex_position(p_point_index);
  909. }
  910. void JoltPhysicsServer3D::soft_body_remove_all_pinned_points(RID p_body) {
  911. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  912. ERR_FAIL_NULL(body);
  913. body->unpin_all_vertices();
  914. }
  915. void JoltPhysicsServer3D::soft_body_pin_point(RID p_body, int p_point_index, bool p_pin) {
  916. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  917. ERR_FAIL_NULL(body);
  918. if (p_pin) {
  919. body->pin_vertex(p_point_index);
  920. } else {
  921. body->unpin_vertex(p_point_index);
  922. }
  923. }
  924. bool JoltPhysicsServer3D::soft_body_is_point_pinned(RID p_body, int p_point_index) const {
  925. JoltSoftBody3D *body = soft_body_owner.get_or_null(p_body);
  926. ERR_FAIL_NULL_V(body, false);
  927. return body->is_vertex_pinned(p_point_index);
  928. }
  929. RID JoltPhysicsServer3D::joint_create() {
  930. JoltJoint3D *joint = memnew(JoltJoint3D);
  931. RID rid = joint_owner.make_rid(joint);
  932. joint->set_rid(rid);
  933. return rid;
  934. }
  935. void JoltPhysicsServer3D::joint_clear(RID p_joint) {
  936. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  937. ERR_FAIL_NULL(joint);
  938. if (joint->get_type() != JOINT_TYPE_MAX) {
  939. JoltJoint3D *empty_joint = memnew(JoltJoint3D);
  940. empty_joint->set_rid(joint->get_rid());
  941. memdelete(joint);
  942. joint = nullptr;
  943. joint_owner.replace(p_joint, empty_joint);
  944. }
  945. }
  946. 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) {
  947. JoltJoint3D *old_joint = joint_owner.get_or_null(p_joint);
  948. ERR_FAIL_NULL(old_joint);
  949. JoltBody3D *body_a = body_owner.get_or_null(p_body_a);
  950. ERR_FAIL_NULL(body_a);
  951. JoltBody3D *body_b = body_owner.get_or_null(p_body_b);
  952. ERR_FAIL_COND(body_a == body_b);
  953. JoltJoint3D *new_joint = memnew(JoltPinJoint3D(*old_joint, body_a, body_b, p_local_a, p_local_b));
  954. memdelete(old_joint);
  955. old_joint = nullptr;
  956. joint_owner.replace(p_joint, new_joint);
  957. }
  958. void JoltPhysicsServer3D::pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) {
  959. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  960. ERR_FAIL_NULL(joint);
  961. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
  962. JoltPinJoint3D *pin_joint = static_cast<JoltPinJoint3D *>(joint);
  963. pin_joint->set_param(p_param, (double)p_value);
  964. }
  965. real_t JoltPhysicsServer3D::pin_joint_get_param(RID p_joint, PinJointParam p_param) const {
  966. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  967. ERR_FAIL_NULL_V(joint, 0.0);
  968. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, 0.0);
  969. const JoltPinJoint3D *pin_joint = static_cast<const JoltPinJoint3D *>(joint);
  970. return (real_t)pin_joint->get_param(p_param);
  971. }
  972. void JoltPhysicsServer3D::pin_joint_set_local_a(RID p_joint, const Vector3 &p_local_a) {
  973. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  974. ERR_FAIL_NULL(joint);
  975. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
  976. JoltPinJoint3D *pin_joint = static_cast<JoltPinJoint3D *>(joint);
  977. pin_joint->set_local_a(p_local_a);
  978. }
  979. Vector3 JoltPhysicsServer3D::pin_joint_get_local_a(RID p_joint) const {
  980. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  981. ERR_FAIL_NULL_V(joint, Vector3());
  982. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, Vector3());
  983. const JoltPinJoint3D *pin_joint = static_cast<const JoltPinJoint3D *>(joint);
  984. return pin_joint->get_local_a();
  985. }
  986. void JoltPhysicsServer3D::pin_joint_set_local_b(RID p_joint, const Vector3 &p_local_b) {
  987. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  988. ERR_FAIL_NULL(joint);
  989. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
  990. JoltPinJoint3D *pin_joint = static_cast<JoltPinJoint3D *>(joint);
  991. pin_joint->set_local_b(p_local_b);
  992. }
  993. Vector3 JoltPhysicsServer3D::pin_joint_get_local_b(RID p_joint) const {
  994. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  995. ERR_FAIL_NULL_V(joint, Vector3());
  996. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, Vector3());
  997. const JoltPinJoint3D *pin_joint = static_cast<const JoltPinJoint3D *>(joint);
  998. return pin_joint->get_local_b();
  999. }
  1000. 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) {
  1001. JoltJoint3D *old_joint = joint_owner.get_or_null(p_joint);
  1002. ERR_FAIL_NULL(old_joint);
  1003. JoltBody3D *body_a = body_owner.get_or_null(p_body_a);
  1004. ERR_FAIL_NULL(body_a);
  1005. JoltBody3D *body_b = body_owner.get_or_null(p_body_b);
  1006. ERR_FAIL_COND(body_a == body_b);
  1007. JoltJoint3D *new_joint = memnew(JoltHingeJoint3D(*old_joint, body_a, body_b, p_hinge_a, p_hinge_b));
  1008. memdelete(old_joint);
  1009. old_joint = nullptr;
  1010. joint_owner.replace(p_joint, new_joint);
  1011. }
  1012. 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) {
  1013. ERR_FAIL_MSG("Simple hinge joints are not supported when using Jolt Physics.");
  1014. }
  1015. void JoltPhysicsServer3D::hinge_joint_set_param(RID p_joint, HingeJointParam p_param, real_t p_value) {
  1016. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1017. ERR_FAIL_NULL(joint);
  1018. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_HINGE);
  1019. JoltHingeJoint3D *hinge_joint = static_cast<JoltHingeJoint3D *>(joint);
  1020. return hinge_joint->set_param(p_param, (double)p_value);
  1021. }
  1022. real_t JoltPhysicsServer3D::hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const {
  1023. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1024. ERR_FAIL_NULL_V(joint, 0.0);
  1025. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, 0.0);
  1026. const JoltHingeJoint3D *hinge_joint = static_cast<const JoltHingeJoint3D *>(joint);
  1027. return (real_t)hinge_joint->get_param(p_param);
  1028. }
  1029. void JoltPhysicsServer3D::hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_enabled) {
  1030. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1031. ERR_FAIL_NULL(joint);
  1032. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_HINGE);
  1033. JoltHingeJoint3D *hinge_joint = static_cast<JoltHingeJoint3D *>(joint);
  1034. return hinge_joint->set_flag(p_flag, p_enabled);
  1035. }
  1036. bool JoltPhysicsServer3D::hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const {
  1037. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1038. ERR_FAIL_NULL_V(joint, false);
  1039. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, false);
  1040. const JoltHingeJoint3D *hinge_joint = static_cast<const JoltHingeJoint3D *>(joint);
  1041. return hinge_joint->get_flag(p_flag);
  1042. }
  1043. 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) {
  1044. JoltJoint3D *old_joint = joint_owner.get_or_null(p_joint);
  1045. ERR_FAIL_NULL(old_joint);
  1046. JoltBody3D *body_a = body_owner.get_or_null(p_body_a);
  1047. ERR_FAIL_NULL(body_a);
  1048. JoltBody3D *body_b = body_owner.get_or_null(p_body_b);
  1049. ERR_FAIL_COND(body_a == body_b);
  1050. JoltJoint3D *new_joint = memnew(JoltSliderJoint3D(*old_joint, body_a, body_b, p_local_ref_a, p_local_ref_b));
  1051. memdelete(old_joint);
  1052. old_joint = nullptr;
  1053. joint_owner.replace(p_joint, new_joint);
  1054. }
  1055. void JoltPhysicsServer3D::slider_joint_set_param(RID p_joint, SliderJointParam p_param, real_t p_value) {
  1056. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1057. ERR_FAIL_NULL(joint);
  1058. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_SLIDER);
  1059. JoltSliderJoint3D *slider_joint = static_cast<JoltSliderJoint3D *>(joint);
  1060. return slider_joint->set_param(p_param, (real_t)p_value);
  1061. }
  1062. real_t JoltPhysicsServer3D::slider_joint_get_param(RID p_joint, SliderJointParam p_param) const {
  1063. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1064. ERR_FAIL_NULL_V(joint, 0.0);
  1065. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_SLIDER, 0.0);
  1066. const JoltSliderJoint3D *slider_joint = static_cast<const JoltSliderJoint3D *>(joint);
  1067. return slider_joint->get_param(p_param);
  1068. }
  1069. 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) {
  1070. JoltJoint3D *old_joint = joint_owner.get_or_null(p_joint);
  1071. ERR_FAIL_NULL(old_joint);
  1072. JoltBody3D *body_a = body_owner.get_or_null(p_body_a);
  1073. ERR_FAIL_NULL(body_a);
  1074. JoltBody3D *body_b = body_owner.get_or_null(p_body_b);
  1075. ERR_FAIL_COND(body_a == body_b);
  1076. JoltJoint3D *new_joint = memnew(JoltConeTwistJoint3D(*old_joint, body_a, body_b, p_local_ref_a, p_local_ref_b));
  1077. memdelete(old_joint);
  1078. old_joint = nullptr;
  1079. joint_owner.replace(p_joint, new_joint);
  1080. }
  1081. void JoltPhysicsServer3D::cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, real_t p_value) {
  1082. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1083. ERR_FAIL_NULL(joint);
  1084. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_CONE_TWIST);
  1085. JoltConeTwistJoint3D *cone_twist_joint = static_cast<JoltConeTwistJoint3D *>(joint);
  1086. return cone_twist_joint->set_param(p_param, (double)p_value);
  1087. }
  1088. real_t JoltPhysicsServer3D::cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const {
  1089. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1090. ERR_FAIL_NULL_V(joint, 0.0);
  1091. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, 0.0);
  1092. const JoltConeTwistJoint3D *cone_twist_joint = static_cast<const JoltConeTwistJoint3D *>(joint);
  1093. return (real_t)cone_twist_joint->get_param(p_param);
  1094. }
  1095. 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) {
  1096. JoltJoint3D *old_joint = joint_owner.get_or_null(p_joint);
  1097. ERR_FAIL_NULL(old_joint);
  1098. JoltBody3D *body_a = body_owner.get_or_null(p_body_a);
  1099. ERR_FAIL_NULL(body_a);
  1100. JoltBody3D *body_b = body_owner.get_or_null(p_body_b);
  1101. ERR_FAIL_COND(body_a == body_b);
  1102. JoltJoint3D *new_joint = memnew(JoltGeneric6DOFJoint3D(*old_joint, body_a, body_b, p_local_ref_a, p_local_ref_b));
  1103. memdelete(old_joint);
  1104. old_joint = nullptr;
  1105. joint_owner.replace(p_joint, new_joint);
  1106. }
  1107. void JoltPhysicsServer3D::generic_6dof_joint_set_param(RID p_joint, Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisParam p_param, real_t p_value) {
  1108. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1109. ERR_FAIL_NULL(joint);
  1110. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_6DOF);
  1111. JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<JoltGeneric6DOFJoint3D *>(joint);
  1112. return g6dof_joint->set_param(p_axis, p_param, (double)p_value);
  1113. }
  1114. real_t JoltPhysicsServer3D::generic_6dof_joint_get_param(RID p_joint, Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisParam p_param) const {
  1115. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1116. ERR_FAIL_NULL_V(joint, 0.0);
  1117. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, 0.0);
  1118. const JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<const JoltGeneric6DOFJoint3D *>(joint);
  1119. return (real_t)g6dof_joint->get_param(p_axis, p_param);
  1120. }
  1121. void JoltPhysicsServer3D::generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisFlag p_flag, bool p_enable) {
  1122. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1123. ERR_FAIL_NULL(joint);
  1124. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_6DOF);
  1125. JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<JoltGeneric6DOFJoint3D *>(joint);
  1126. return g6dof_joint->set_flag(p_axis, p_flag, p_enable);
  1127. }
  1128. bool JoltPhysicsServer3D::generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisFlag p_flag) const {
  1129. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1130. ERR_FAIL_NULL_V(joint, false);
  1131. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, false);
  1132. const JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<const JoltGeneric6DOFJoint3D *>(joint);
  1133. return g6dof_joint->get_flag(p_axis, p_flag);
  1134. }
  1135. PhysicsServer3D::JointType JoltPhysicsServer3D::joint_get_type(RID p_joint) const {
  1136. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1137. ERR_FAIL_NULL_V(joint, JOINT_TYPE_PIN);
  1138. return joint->get_type();
  1139. }
  1140. void JoltPhysicsServer3D::joint_set_solver_priority(RID p_joint, int p_priority) {
  1141. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1142. ERR_FAIL_NULL(joint);
  1143. joint->set_solver_priority(p_priority);
  1144. }
  1145. int JoltPhysicsServer3D::joint_get_solver_priority(RID p_joint) const {
  1146. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1147. ERR_FAIL_NULL_V(joint, 0);
  1148. return joint->get_solver_priority();
  1149. }
  1150. void JoltPhysicsServer3D::joint_disable_collisions_between_bodies(RID p_joint, bool p_disable) {
  1151. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1152. ERR_FAIL_NULL(joint);
  1153. joint->set_collision_disabled(p_disable);
  1154. }
  1155. bool JoltPhysicsServer3D::joint_is_disabled_collisions_between_bodies(RID p_joint) const {
  1156. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1157. ERR_FAIL_NULL_V(joint, false);
  1158. return joint->is_collision_disabled();
  1159. }
  1160. void JoltPhysicsServer3D::free(RID p_rid) {
  1161. if (JoltShape3D *shape = shape_owner.get_or_null(p_rid)) {
  1162. free_shape(shape);
  1163. } else if (JoltBody3D *body = body_owner.get_or_null(p_rid)) {
  1164. free_body(body);
  1165. } else if (JoltJoint3D *joint = joint_owner.get_or_null(p_rid)) {
  1166. free_joint(joint);
  1167. } else if (JoltArea3D *area = area_owner.get_or_null(p_rid)) {
  1168. free_area(area);
  1169. } else if (JoltSoftBody3D *soft_body = soft_body_owner.get_or_null(p_rid)) {
  1170. free_soft_body(soft_body);
  1171. } else if (JoltSpace3D *space = space_owner.get_or_null(p_rid)) {
  1172. free_space(space);
  1173. } else {
  1174. ERR_FAIL_MSG("Failed to free RID: The specified RID has no owner.");
  1175. }
  1176. }
  1177. void JoltPhysicsServer3D::set_active(bool p_active) {
  1178. active = p_active;
  1179. }
  1180. void JoltPhysicsServer3D::init() {
  1181. job_system = new JoltJobSystem();
  1182. }
  1183. void JoltPhysicsServer3D::finish() {
  1184. if (job_system != nullptr) {
  1185. delete job_system;
  1186. job_system = nullptr;
  1187. }
  1188. }
  1189. void JoltPhysicsServer3D::step(real_t p_step) {
  1190. if (!active) {
  1191. return;
  1192. }
  1193. for (JoltSpace3D *active_space : active_spaces) {
  1194. job_system->pre_step();
  1195. active_space->step((float)p_step);
  1196. job_system->post_step();
  1197. }
  1198. }
  1199. void JoltPhysicsServer3D::sync() {
  1200. doing_sync = true;
  1201. }
  1202. void JoltPhysicsServer3D::end_sync() {
  1203. doing_sync = false;
  1204. }
  1205. void JoltPhysicsServer3D::flush_queries() {
  1206. if (!active) {
  1207. return;
  1208. }
  1209. flushing_queries = true;
  1210. for (JoltSpace3D *space : active_spaces) {
  1211. space->call_queries();
  1212. }
  1213. flushing_queries = false;
  1214. #ifdef DEBUG_ENABLED
  1215. job_system->flush_timings();
  1216. #endif
  1217. }
  1218. bool JoltPhysicsServer3D::is_flushing_queries() const {
  1219. return flushing_queries;
  1220. }
  1221. int JoltPhysicsServer3D::get_process_info(ProcessInfo p_process_info) {
  1222. return 0;
  1223. }
  1224. void JoltPhysicsServer3D::free_space(JoltSpace3D *p_space) {
  1225. ERR_FAIL_NULL(p_space);
  1226. free_area(p_space->get_default_area());
  1227. space_set_active(p_space->get_rid(), false);
  1228. space_owner.free(p_space->get_rid());
  1229. memdelete(p_space);
  1230. }
  1231. void JoltPhysicsServer3D::free_area(JoltArea3D *p_area) {
  1232. ERR_FAIL_NULL(p_area);
  1233. p_area->set_space(nullptr);
  1234. area_owner.free(p_area->get_rid());
  1235. memdelete(p_area);
  1236. }
  1237. void JoltPhysicsServer3D::free_body(JoltBody3D *p_body) {
  1238. ERR_FAIL_NULL(p_body);
  1239. p_body->set_space(nullptr);
  1240. body_owner.free(p_body->get_rid());
  1241. memdelete(p_body);
  1242. }
  1243. void JoltPhysicsServer3D::free_soft_body(JoltSoftBody3D *p_body) {
  1244. ERR_FAIL_NULL(p_body);
  1245. p_body->set_space(nullptr);
  1246. soft_body_owner.free(p_body->get_rid());
  1247. memdelete(p_body);
  1248. }
  1249. void JoltPhysicsServer3D::free_shape(JoltShape3D *p_shape) {
  1250. ERR_FAIL_NULL(p_shape);
  1251. p_shape->remove_self();
  1252. shape_owner.free(p_shape->get_rid());
  1253. memdelete(p_shape);
  1254. }
  1255. void JoltPhysicsServer3D::free_joint(JoltJoint3D *p_joint) {
  1256. ERR_FAIL_NULL(p_joint);
  1257. joint_owner.free(p_joint->get_rid());
  1258. memdelete(p_joint);
  1259. }
  1260. #ifdef DEBUG_ENABLED
  1261. void JoltPhysicsServer3D::dump_debug_snapshots(const String &p_dir) {
  1262. for (JoltSpace3D *space : active_spaces) {
  1263. space->dump_debug_snapshot(p_dir);
  1264. }
  1265. }
  1266. void JoltPhysicsServer3D::space_dump_debug_snapshot(RID p_space, const String &p_dir) {
  1267. JoltSpace3D *space = space_owner.get_or_null(p_space);
  1268. ERR_FAIL_NULL(space);
  1269. space->dump_debug_snapshot(p_dir);
  1270. }
  1271. #endif
  1272. bool JoltPhysicsServer3D::joint_get_enabled(RID p_joint) const {
  1273. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1274. ERR_FAIL_NULL_V(joint, false);
  1275. return joint->is_enabled();
  1276. }
  1277. void JoltPhysicsServer3D::joint_set_enabled(RID p_joint, bool p_enabled) {
  1278. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1279. ERR_FAIL_NULL(joint);
  1280. joint->set_enabled(p_enabled);
  1281. }
  1282. int JoltPhysicsServer3D::joint_get_solver_velocity_iterations(RID p_joint) {
  1283. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1284. ERR_FAIL_NULL_V(joint, 0);
  1285. return joint->get_solver_velocity_iterations();
  1286. }
  1287. void JoltPhysicsServer3D::joint_set_solver_velocity_iterations(RID p_joint, int p_value) {
  1288. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1289. ERR_FAIL_NULL(joint);
  1290. return joint->set_solver_velocity_iterations(p_value);
  1291. }
  1292. int JoltPhysicsServer3D::joint_get_solver_position_iterations(RID p_joint) {
  1293. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1294. ERR_FAIL_NULL_V(joint, 0);
  1295. return joint->get_solver_position_iterations();
  1296. }
  1297. void JoltPhysicsServer3D::joint_set_solver_position_iterations(RID p_joint, int p_value) {
  1298. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1299. ERR_FAIL_NULL(joint);
  1300. return joint->set_solver_position_iterations(p_value);
  1301. }
  1302. float JoltPhysicsServer3D::pin_joint_get_applied_force(RID p_joint) {
  1303. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1304. ERR_FAIL_NULL_V(joint, 0.0f);
  1305. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, 0.0);
  1306. JoltPinJoint3D *pin_joint = static_cast<JoltPinJoint3D *>(joint);
  1307. return pin_joint->get_applied_force();
  1308. }
  1309. double JoltPhysicsServer3D::hinge_joint_get_jolt_param(RID p_joint, HingeJointParamJolt p_param) const {
  1310. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1311. ERR_FAIL_NULL_V(joint, 0.0);
  1312. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, 0.0);
  1313. JoltHingeJoint3D *hinge_joint = static_cast<JoltHingeJoint3D *>(joint);
  1314. return hinge_joint->get_jolt_param(p_param);
  1315. }
  1316. void JoltPhysicsServer3D::hinge_joint_set_jolt_param(RID p_joint, HingeJointParamJolt p_param, double p_value) {
  1317. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1318. ERR_FAIL_NULL(joint);
  1319. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_HINGE);
  1320. JoltHingeJoint3D *hinge_joint = static_cast<JoltHingeJoint3D *>(joint);
  1321. return hinge_joint->set_jolt_param(p_param, p_value);
  1322. }
  1323. bool JoltPhysicsServer3D::hinge_joint_get_jolt_flag(RID p_joint, HingeJointFlagJolt p_flag) const {
  1324. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1325. ERR_FAIL_NULL_V(joint, false);
  1326. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, false);
  1327. const JoltHingeJoint3D *hinge_joint = static_cast<const JoltHingeJoint3D *>(joint);
  1328. return hinge_joint->get_jolt_flag(p_flag);
  1329. }
  1330. void JoltPhysicsServer3D::hinge_joint_set_jolt_flag(RID p_joint, HingeJointFlagJolt p_flag, bool p_enabled) {
  1331. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1332. ERR_FAIL_NULL(joint);
  1333. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_HINGE);
  1334. JoltHingeJoint3D *hinge_joint = static_cast<JoltHingeJoint3D *>(joint);
  1335. return hinge_joint->set_jolt_flag(p_flag, p_enabled);
  1336. }
  1337. float JoltPhysicsServer3D::hinge_joint_get_applied_force(RID p_joint) {
  1338. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1339. ERR_FAIL_NULL_V(joint, 0.0f);
  1340. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, 0.0f);
  1341. JoltHingeJoint3D *hinge_joint = static_cast<JoltHingeJoint3D *>(joint);
  1342. return hinge_joint->get_applied_force();
  1343. }
  1344. float JoltPhysicsServer3D::hinge_joint_get_applied_torque(RID p_joint) {
  1345. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1346. ERR_FAIL_NULL_V(joint, 0.0f);
  1347. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, 0.0f);
  1348. JoltHingeJoint3D *hinge_joint = static_cast<JoltHingeJoint3D *>(joint);
  1349. return hinge_joint->get_applied_torque();
  1350. }
  1351. double JoltPhysicsServer3D::slider_joint_get_jolt_param(RID p_joint, SliderJointParamJolt p_param) const {
  1352. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1353. ERR_FAIL_NULL_V(joint, 0.0);
  1354. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_SLIDER, 0.0);
  1355. JoltSliderJoint3D *slider_joint = static_cast<JoltSliderJoint3D *>(joint);
  1356. return slider_joint->get_jolt_param(p_param);
  1357. }
  1358. void JoltPhysicsServer3D::slider_joint_set_jolt_param(RID p_joint, SliderJointParamJolt p_param, double p_value) {
  1359. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1360. ERR_FAIL_NULL(joint);
  1361. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_SLIDER);
  1362. JoltSliderJoint3D *slider_joint = static_cast<JoltSliderJoint3D *>(joint);
  1363. return slider_joint->set_jolt_param(p_param, p_value);
  1364. }
  1365. bool JoltPhysicsServer3D::slider_joint_get_jolt_flag(RID p_joint, SliderJointFlagJolt p_flag) const {
  1366. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1367. ERR_FAIL_NULL_V(joint, false);
  1368. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_SLIDER, false);
  1369. const JoltSliderJoint3D *slider_joint = static_cast<const JoltSliderJoint3D *>(joint);
  1370. return slider_joint->get_jolt_flag(p_flag);
  1371. }
  1372. void JoltPhysicsServer3D::slider_joint_set_jolt_flag(RID p_joint, SliderJointFlagJolt p_flag, bool p_enabled) {
  1373. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1374. ERR_FAIL_NULL(joint);
  1375. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_SLIDER);
  1376. JoltSliderJoint3D *slider_joint = static_cast<JoltSliderJoint3D *>(joint);
  1377. return slider_joint->set_jolt_flag(p_flag, p_enabled);
  1378. }
  1379. float JoltPhysicsServer3D::slider_joint_get_applied_force(RID p_joint) {
  1380. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1381. ERR_FAIL_NULL_V(joint, 0.0f);
  1382. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_SLIDER, 0.0f);
  1383. JoltSliderJoint3D *slider_joint = static_cast<JoltSliderJoint3D *>(joint);
  1384. return slider_joint->get_applied_force();
  1385. }
  1386. float JoltPhysicsServer3D::slider_joint_get_applied_torque(RID p_joint) {
  1387. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1388. ERR_FAIL_NULL_V(joint, 0.0f);
  1389. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_SLIDER, 0.0f);
  1390. JoltSliderJoint3D *slider_joint = static_cast<JoltSliderJoint3D *>(joint);
  1391. return slider_joint->get_applied_torque();
  1392. }
  1393. double JoltPhysicsServer3D::cone_twist_joint_get_jolt_param(RID p_joint, ConeTwistJointParamJolt p_param) const {
  1394. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1395. ERR_FAIL_NULL_V(joint, 0.0);
  1396. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, 0.0);
  1397. JoltConeTwistJoint3D *cone_twist_joint = static_cast<JoltConeTwistJoint3D *>(joint);
  1398. return cone_twist_joint->get_jolt_param(p_param);
  1399. }
  1400. void JoltPhysicsServer3D::cone_twist_joint_set_jolt_param(RID p_joint, ConeTwistJointParamJolt p_param, double p_value) {
  1401. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1402. ERR_FAIL_NULL(joint);
  1403. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_CONE_TWIST);
  1404. JoltConeTwistJoint3D *cone_twist_joint = static_cast<JoltConeTwistJoint3D *>(joint);
  1405. return cone_twist_joint->set_jolt_param(p_param, p_value);
  1406. }
  1407. bool JoltPhysicsServer3D::cone_twist_joint_get_jolt_flag(RID p_joint, ConeTwistJointFlagJolt p_flag) const {
  1408. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1409. ERR_FAIL_NULL_V(joint, false);
  1410. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, false);
  1411. const JoltConeTwistJoint3D *cone_twist_joint = static_cast<const JoltConeTwistJoint3D *>(joint);
  1412. return cone_twist_joint->get_jolt_flag(p_flag);
  1413. }
  1414. void JoltPhysicsServer3D::cone_twist_joint_set_jolt_flag(RID p_joint, ConeTwistJointFlagJolt p_flag, bool p_enabled) {
  1415. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1416. ERR_FAIL_NULL(joint);
  1417. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_CONE_TWIST);
  1418. JoltConeTwistJoint3D *cone_twist_joint = static_cast<JoltConeTwistJoint3D *>(joint);
  1419. return cone_twist_joint->set_jolt_flag(p_flag, p_enabled);
  1420. }
  1421. float JoltPhysicsServer3D::cone_twist_joint_get_applied_force(RID p_joint) {
  1422. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1423. ERR_FAIL_NULL_V(joint, 0.0f);
  1424. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, 0.0f);
  1425. JoltConeTwistJoint3D *cone_twist_joint = static_cast<JoltConeTwistJoint3D *>(joint);
  1426. return cone_twist_joint->get_applied_force();
  1427. }
  1428. float JoltPhysicsServer3D::cone_twist_joint_get_applied_torque(RID p_joint) {
  1429. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1430. ERR_FAIL_NULL_V(joint, 0.0f);
  1431. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, 0.0f);
  1432. JoltConeTwistJoint3D *cone_twist_joint = static_cast<JoltConeTwistJoint3D *>(joint);
  1433. return cone_twist_joint->get_applied_torque();
  1434. }
  1435. double JoltPhysicsServer3D::generic_6dof_joint_get_jolt_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParamJolt p_param) const {
  1436. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1437. ERR_FAIL_NULL_V(joint, 0.0);
  1438. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, 0.0);
  1439. JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<JoltGeneric6DOFJoint3D *>(joint);
  1440. return g6dof_joint->get_jolt_param(p_axis, p_param);
  1441. }
  1442. void JoltPhysicsServer3D::generic_6dof_joint_set_jolt_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParamJolt p_param, double p_value) {
  1443. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1444. ERR_FAIL_NULL(joint);
  1445. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_6DOF);
  1446. JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<JoltGeneric6DOFJoint3D *>(joint);
  1447. return g6dof_joint->set_jolt_param(p_axis, p_param, p_value);
  1448. }
  1449. bool JoltPhysicsServer3D::generic_6dof_joint_get_jolt_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlagJolt p_flag) const {
  1450. const JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1451. ERR_FAIL_NULL_V(joint, false);
  1452. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, false);
  1453. const JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<const JoltGeneric6DOFJoint3D *>(joint);
  1454. return g6dof_joint->get_jolt_flag(p_axis, p_flag);
  1455. }
  1456. void JoltPhysicsServer3D::generic_6dof_joint_set_jolt_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlagJolt p_flag, bool p_enabled) {
  1457. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1458. ERR_FAIL_NULL(joint);
  1459. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_6DOF);
  1460. JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<JoltGeneric6DOFJoint3D *>(joint);
  1461. return g6dof_joint->set_jolt_flag(p_axis, p_flag, p_enabled);
  1462. }
  1463. float JoltPhysicsServer3D::generic_6dof_joint_get_applied_force(RID p_joint) {
  1464. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1465. ERR_FAIL_NULL_V(joint, 0.0f);
  1466. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, 0.0f);
  1467. JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<JoltGeneric6DOFJoint3D *>(joint);
  1468. return g6dof_joint->get_applied_force();
  1469. }
  1470. float JoltPhysicsServer3D::generic_6dof_joint_get_applied_torque(RID p_joint) {
  1471. JoltJoint3D *joint = joint_owner.get_or_null(p_joint);
  1472. ERR_FAIL_NULL_V(joint, 0.0f);
  1473. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, 0.0f);
  1474. JoltGeneric6DOFJoint3D *g6dof_joint = static_cast<JoltGeneric6DOFJoint3D *>(joint);
  1475. return g6dof_joint->get_applied_torque();
  1476. }