godot_physics_server_3d.cpp 55 KB

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