godot_physics_server_3d.cpp 57 KB

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