physics_server_sw.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. /**************************************************************************/
  2. /* physics_server_sw.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 "physics_server_sw.h"
  31. #include "broad_phase_basic.h"
  32. #include "broad_phase_bvh.h"
  33. #include "broad_phase_octree.h"
  34. #include "core/os/os.h"
  35. #include "core/project_settings.h"
  36. #include "core/script_language.h"
  37. #include "joints/cone_twist_joint_sw.h"
  38. #include "joints/generic_6dof_joint_sw.h"
  39. #include "joints/hinge_joint_sw.h"
  40. #include "joints/pin_joint_sw.h"
  41. #include "joints/slider_joint_sw.h"
  42. #define FLUSH_QUERY_CHECK(m_object) \
  43. 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.");
  44. RID PhysicsServerSW::shape_create(ShapeType p_shape) {
  45. ShapeSW *shape = nullptr;
  46. switch (p_shape) {
  47. case SHAPE_PLANE: {
  48. shape = memnew(PlaneShapeSW);
  49. } break;
  50. case SHAPE_RAY: {
  51. shape = memnew(RayShapeSW);
  52. } break;
  53. case SHAPE_SPHERE: {
  54. shape = memnew(SphereShapeSW);
  55. } break;
  56. case SHAPE_BOX: {
  57. shape = memnew(BoxShapeSW);
  58. } break;
  59. case SHAPE_CAPSULE: {
  60. shape = memnew(CapsuleShapeSW);
  61. } break;
  62. case SHAPE_CYLINDER: {
  63. shape = memnew(CylinderShapeSW);
  64. } break;
  65. case SHAPE_CONVEX_POLYGON: {
  66. shape = memnew(ConvexPolygonShapeSW);
  67. } break;
  68. case SHAPE_CONCAVE_POLYGON: {
  69. shape = memnew(ConcavePolygonShapeSW);
  70. } break;
  71. case SHAPE_HEIGHTMAP: {
  72. shape = memnew(HeightMapShapeSW);
  73. } break;
  74. case SHAPE_CUSTOM: {
  75. ERR_FAIL_V(RID());
  76. } break;
  77. }
  78. RID id = shape_owner.make_rid(shape);
  79. shape->set_self(id);
  80. return id;
  81. };
  82. void PhysicsServerSW::shape_set_data(RID p_shape, const Variant &p_data) {
  83. ShapeSW *shape = shape_owner.get(p_shape);
  84. ERR_FAIL_COND(!shape);
  85. shape->set_data(p_data);
  86. };
  87. void PhysicsServerSW::shape_set_custom_solver_bias(RID p_shape, real_t p_bias) {
  88. ShapeSW *shape = shape_owner.get(p_shape);
  89. ERR_FAIL_COND(!shape);
  90. shape->set_custom_bias(p_bias);
  91. }
  92. PhysicsServer::ShapeType PhysicsServerSW::shape_get_type(RID p_shape) const {
  93. const ShapeSW *shape = shape_owner.get(p_shape);
  94. ERR_FAIL_COND_V(!shape, SHAPE_CUSTOM);
  95. return shape->get_type();
  96. };
  97. Variant PhysicsServerSW::shape_get_data(RID p_shape) const {
  98. const ShapeSW *shape = shape_owner.get(p_shape);
  99. ERR_FAIL_COND_V(!shape, Variant());
  100. ERR_FAIL_COND_V(!shape->is_configured(), Variant());
  101. return shape->get_data();
  102. };
  103. void PhysicsServerSW::shape_set_margin(RID p_shape, real_t p_margin) {
  104. }
  105. real_t PhysicsServerSW::shape_get_margin(RID p_shape) const {
  106. return 0.0;
  107. }
  108. real_t PhysicsServerSW::shape_get_custom_solver_bias(RID p_shape) const {
  109. const ShapeSW *shape = shape_owner.get(p_shape);
  110. ERR_FAIL_COND_V(!shape, 0);
  111. return shape->get_custom_bias();
  112. }
  113. RID PhysicsServerSW::space_create() {
  114. SpaceSW *space = memnew(SpaceSW);
  115. RID id = space_owner.make_rid(space);
  116. space->set_self(id);
  117. RID area_id = RID_PRIME(area_create());
  118. AreaSW *area = area_owner.get(area_id);
  119. ERR_FAIL_COND_V(!area, RID());
  120. space->set_default_area(area);
  121. area->set_space(space);
  122. area->set_priority(-1);
  123. RID sgb = RID_PRIME(body_create());
  124. body_set_space(sgb, id);
  125. body_set_mode(sgb, BODY_MODE_STATIC);
  126. space->set_static_global_body(sgb);
  127. return id;
  128. };
  129. void PhysicsServerSW::space_set_active(RID p_space, bool p_active) {
  130. SpaceSW *space = space_owner.get(p_space);
  131. ERR_FAIL_COND(!space);
  132. if (p_active) {
  133. active_spaces.insert(space);
  134. } else {
  135. active_spaces.erase(space);
  136. }
  137. }
  138. bool PhysicsServerSW::space_is_active(RID p_space) const {
  139. const SpaceSW *space = space_owner.get(p_space);
  140. ERR_FAIL_COND_V(!space, false);
  141. return active_spaces.has(space);
  142. }
  143. void PhysicsServerSW::space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) {
  144. SpaceSW *space = space_owner.get(p_space);
  145. ERR_FAIL_COND(!space);
  146. space->set_param(p_param, p_value);
  147. }
  148. real_t PhysicsServerSW::space_get_param(RID p_space, SpaceParameter p_param) const {
  149. const SpaceSW *space = space_owner.get(p_space);
  150. ERR_FAIL_COND_V(!space, 0);
  151. return space->get_param(p_param);
  152. }
  153. PhysicsDirectSpaceState *PhysicsServerSW::space_get_direct_state(RID p_space) {
  154. SpaceSW *space = space_owner.get(p_space);
  155. ERR_FAIL_COND_V(!space, nullptr);
  156. ERR_FAIL_COND_V_MSG(space->is_locked(), nullptr, "Space state is inaccessible right now, wait for iteration or physics process notification.");
  157. return space->get_direct_state();
  158. }
  159. void PhysicsServerSW::space_set_debug_contacts(RID p_space, int p_max_contacts) {
  160. SpaceSW *space = space_owner.get(p_space);
  161. ERR_FAIL_COND(!space);
  162. space->set_debug_contacts(p_max_contacts);
  163. }
  164. Vector<Vector3> PhysicsServerSW::space_get_contacts(RID p_space) const {
  165. SpaceSW *space = space_owner.get(p_space);
  166. ERR_FAIL_COND_V(!space, Vector<Vector3>());
  167. return space->get_debug_contacts();
  168. }
  169. int PhysicsServerSW::space_get_contact_count(RID p_space) const {
  170. SpaceSW *space = space_owner.get(p_space);
  171. ERR_FAIL_COND_V(!space, 0);
  172. return space->get_debug_contact_count();
  173. }
  174. RID PhysicsServerSW::area_create() {
  175. AreaSW *area = memnew(AreaSW);
  176. RID rid = area_owner.make_rid(area);
  177. area->set_self(rid);
  178. return rid;
  179. };
  180. void PhysicsServerSW::area_set_space(RID p_area, RID p_space) {
  181. AreaSW *area = area_owner.get(p_area);
  182. ERR_FAIL_COND(!area);
  183. SpaceSW *space = nullptr;
  184. if (p_space.is_valid()) {
  185. space = space_owner.get(p_space);
  186. ERR_FAIL_COND(!space);
  187. }
  188. if (area->get_space() == space) {
  189. return; //pointless
  190. }
  191. area->clear_constraints();
  192. area->set_space(space);
  193. };
  194. RID PhysicsServerSW::area_get_space(RID p_area) const {
  195. AreaSW *area = area_owner.get(p_area);
  196. ERR_FAIL_COND_V(!area, RID());
  197. SpaceSW *space = area->get_space();
  198. if (!space) {
  199. return RID();
  200. }
  201. return space->get_self();
  202. };
  203. void PhysicsServerSW::area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode) {
  204. AreaSW *area = area_owner.get(p_area);
  205. ERR_FAIL_COND(!area);
  206. area->set_space_override_mode(p_mode);
  207. }
  208. PhysicsServer::AreaSpaceOverrideMode PhysicsServerSW::area_get_space_override_mode(RID p_area) const {
  209. const AreaSW *area = area_owner.get(p_area);
  210. ERR_FAIL_COND_V(!area, AREA_SPACE_OVERRIDE_DISABLED);
  211. return area->get_space_override_mode();
  212. }
  213. void PhysicsServerSW::area_add_shape(RID p_area, RID p_shape, const Transform &p_transform, bool p_disabled) {
  214. AreaSW *area = area_owner.get(p_area);
  215. ERR_FAIL_COND(!area);
  216. ShapeSW *shape = shape_owner.get(p_shape);
  217. ERR_FAIL_COND(!shape);
  218. area->add_shape(shape, p_transform, p_disabled);
  219. }
  220. void PhysicsServerSW::area_set_shape(RID p_area, int p_shape_idx, RID p_shape) {
  221. AreaSW *area = area_owner.get(p_area);
  222. ERR_FAIL_COND(!area);
  223. ShapeSW *shape = shape_owner.get(p_shape);
  224. ERR_FAIL_COND(!shape);
  225. ERR_FAIL_COND(!shape->is_configured());
  226. area->set_shape(p_shape_idx, shape);
  227. }
  228. void PhysicsServerSW::area_set_shape_transform(RID p_area, int p_shape_idx, const Transform &p_transform) {
  229. AreaSW *area = area_owner.get(p_area);
  230. ERR_FAIL_COND(!area);
  231. area->set_shape_transform(p_shape_idx, p_transform);
  232. }
  233. int PhysicsServerSW::area_get_shape_count(RID p_area) const {
  234. AreaSW *area = area_owner.get(p_area);
  235. ERR_FAIL_COND_V(!area, -1);
  236. return area->get_shape_count();
  237. }
  238. RID PhysicsServerSW::area_get_shape(RID p_area, int p_shape_idx) const {
  239. AreaSW *area = area_owner.get(p_area);
  240. ERR_FAIL_COND_V(!area, RID());
  241. ShapeSW *shape = area->get_shape(p_shape_idx);
  242. ERR_FAIL_COND_V(!shape, RID());
  243. return shape->get_self();
  244. }
  245. Transform PhysicsServerSW::area_get_shape_transform(RID p_area, int p_shape_idx) const {
  246. AreaSW *area = area_owner.get(p_area);
  247. ERR_FAIL_COND_V(!area, Transform());
  248. return area->get_shape_transform(p_shape_idx);
  249. }
  250. void PhysicsServerSW::area_remove_shape(RID p_area, int p_shape_idx) {
  251. AreaSW *area = area_owner.get(p_area);
  252. ERR_FAIL_COND(!area);
  253. area->remove_shape(p_shape_idx);
  254. }
  255. void PhysicsServerSW::area_clear_shapes(RID p_area) {
  256. AreaSW *area = area_owner.get(p_area);
  257. ERR_FAIL_COND(!area);
  258. while (area->get_shape_count()) {
  259. area->remove_shape(0);
  260. }
  261. }
  262. void PhysicsServerSW::area_set_shape_disabled(RID p_area, int p_shape_idx, bool p_disabled) {
  263. AreaSW *area = area_owner.get(p_area);
  264. ERR_FAIL_COND(!area);
  265. ERR_FAIL_INDEX(p_shape_idx, area->get_shape_count());
  266. FLUSH_QUERY_CHECK(area);
  267. area->set_shape_disabled(p_shape_idx, p_disabled);
  268. }
  269. void PhysicsServerSW::area_attach_object_instance_id(RID p_area, ObjectID p_id) {
  270. if (space_owner.owns(p_area)) {
  271. SpaceSW *space = space_owner.get(p_area);
  272. p_area = space->get_default_area()->get_self();
  273. }
  274. AreaSW *area = area_owner.get(p_area);
  275. ERR_FAIL_COND(!area);
  276. area->set_instance_id(p_id);
  277. }
  278. ObjectID PhysicsServerSW::area_get_object_instance_id(RID p_area) const {
  279. if (space_owner.owns(p_area)) {
  280. SpaceSW *space = space_owner.get(p_area);
  281. p_area = space->get_default_area()->get_self();
  282. }
  283. AreaSW *area = area_owner.get(p_area);
  284. ERR_FAIL_COND_V(!area, 0);
  285. return area->get_instance_id();
  286. }
  287. void PhysicsServerSW::area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) {
  288. if (space_owner.owns(p_area)) {
  289. SpaceSW *space = space_owner.get(p_area);
  290. p_area = space->get_default_area()->get_self();
  291. }
  292. AreaSW *area = area_owner.get(p_area);
  293. ERR_FAIL_COND(!area);
  294. area->set_param(p_param, p_value);
  295. };
  296. void PhysicsServerSW::area_set_transform(RID p_area, const Transform &p_transform) {
  297. AreaSW *area = area_owner.get(p_area);
  298. ERR_FAIL_COND(!area);
  299. area->set_transform(p_transform);
  300. };
  301. Variant PhysicsServerSW::area_get_param(RID p_area, AreaParameter p_param) const {
  302. if (space_owner.owns(p_area)) {
  303. SpaceSW *space = space_owner.get(p_area);
  304. p_area = space->get_default_area()->get_self();
  305. }
  306. AreaSW *area = area_owner.get(p_area);
  307. ERR_FAIL_COND_V(!area, Variant());
  308. return area->get_param(p_param);
  309. };
  310. Transform PhysicsServerSW::area_get_transform(RID p_area) const {
  311. AreaSW *area = area_owner.get(p_area);
  312. ERR_FAIL_COND_V(!area, Transform());
  313. return area->get_transform();
  314. };
  315. void PhysicsServerSW::area_set_collision_layer(RID p_area, uint32_t p_layer) {
  316. AreaSW *area = area_owner.get(p_area);
  317. ERR_FAIL_COND(!area);
  318. area->set_collision_layer(p_layer);
  319. }
  320. void PhysicsServerSW::area_set_collision_mask(RID p_area, uint32_t p_mask) {
  321. AreaSW *area = area_owner.get(p_area);
  322. ERR_FAIL_COND(!area);
  323. area->set_collision_mask(p_mask);
  324. }
  325. void PhysicsServerSW::area_set_monitorable(RID p_area, bool p_monitorable) {
  326. AreaSW *area = area_owner.get(p_area);
  327. ERR_FAIL_COND(!area);
  328. FLUSH_QUERY_CHECK(area);
  329. area->set_monitorable(p_monitorable);
  330. }
  331. void PhysicsServerSW::area_set_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) {
  332. AreaSW *area = area_owner.get(p_area);
  333. ERR_FAIL_COND(!area);
  334. area->set_monitor_callback(p_receiver ? p_receiver->get_instance_id() : 0, p_method);
  335. }
  336. void PhysicsServerSW::area_set_ray_pickable(RID p_area, bool p_enable) {
  337. AreaSW *area = area_owner.get(p_area);
  338. ERR_FAIL_COND(!area);
  339. area->set_ray_pickable(p_enable);
  340. }
  341. bool PhysicsServerSW::area_is_ray_pickable(RID p_area) const {
  342. AreaSW *area = area_owner.get(p_area);
  343. ERR_FAIL_COND_V(!area, false);
  344. return area->is_ray_pickable();
  345. }
  346. void PhysicsServerSW::area_set_area_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) {
  347. AreaSW *area = area_owner.get(p_area);
  348. ERR_FAIL_COND(!area);
  349. area->set_area_monitor_callback(p_receiver ? p_receiver->get_instance_id() : 0, p_method);
  350. }
  351. /* BODY API */
  352. RID PhysicsServerSW::body_create(BodyMode p_mode, bool p_init_sleeping) {
  353. BodySW *body = memnew(BodySW);
  354. if (p_mode != BODY_MODE_RIGID) {
  355. body->set_mode(p_mode);
  356. }
  357. if (p_init_sleeping) {
  358. body->set_state(BODY_STATE_SLEEPING, p_init_sleeping);
  359. }
  360. RID rid = body_owner.make_rid(body);
  361. body->set_self(rid);
  362. return rid;
  363. };
  364. void PhysicsServerSW::body_set_space(RID p_body, RID p_space) {
  365. BodySW *body = body_owner.get(p_body);
  366. ERR_FAIL_COND(!body);
  367. SpaceSW *space = nullptr;
  368. if (p_space.is_valid()) {
  369. space = space_owner.get(p_space);
  370. ERR_FAIL_COND(!space);
  371. }
  372. if (body->get_space() == space) {
  373. return; //pointless
  374. }
  375. body->clear_constraint_map();
  376. body->set_space(space);
  377. };
  378. RID PhysicsServerSW::body_get_space(RID p_body) const {
  379. BodySW *body = body_owner.get(p_body);
  380. ERR_FAIL_COND_V(!body, RID());
  381. SpaceSW *space = body->get_space();
  382. if (!space) {
  383. return RID();
  384. }
  385. return space->get_self();
  386. };
  387. void PhysicsServerSW::body_set_mode(RID p_body, BodyMode p_mode) {
  388. BodySW *body = body_owner.get(p_body);
  389. ERR_FAIL_COND(!body);
  390. body->set_mode(p_mode);
  391. };
  392. PhysicsServer::BodyMode PhysicsServerSW::body_get_mode(RID p_body) const {
  393. BodySW *body = body_owner.get(p_body);
  394. ERR_FAIL_COND_V(!body, BODY_MODE_STATIC);
  395. return body->get_mode();
  396. };
  397. void PhysicsServerSW::body_add_shape(RID p_body, RID p_shape, const Transform &p_transform, bool p_disabled) {
  398. BodySW *body = body_owner.get(p_body);
  399. ERR_FAIL_COND(!body);
  400. ShapeSW *shape = shape_owner.get(p_shape);
  401. ERR_FAIL_COND(!shape);
  402. body->add_shape(shape, p_transform, p_disabled);
  403. }
  404. void PhysicsServerSW::body_set_shape(RID p_body, int p_shape_idx, RID p_shape) {
  405. BodySW *body = body_owner.get(p_body);
  406. ERR_FAIL_COND(!body);
  407. ShapeSW *shape = shape_owner.get(p_shape);
  408. ERR_FAIL_COND(!shape);
  409. ERR_FAIL_COND(!shape->is_configured());
  410. body->set_shape(p_shape_idx, shape);
  411. }
  412. void PhysicsServerSW::body_set_shape_transform(RID p_body, int p_shape_idx, const Transform &p_transform) {
  413. BodySW *body = body_owner.get(p_body);
  414. ERR_FAIL_COND(!body);
  415. body->set_shape_transform(p_shape_idx, p_transform);
  416. }
  417. int PhysicsServerSW::body_get_shape_count(RID p_body) const {
  418. BodySW *body = body_owner.get(p_body);
  419. ERR_FAIL_COND_V(!body, -1);
  420. return body->get_shape_count();
  421. }
  422. RID PhysicsServerSW::body_get_shape(RID p_body, int p_shape_idx) const {
  423. BodySW *body = body_owner.get(p_body);
  424. ERR_FAIL_COND_V(!body, RID());
  425. ShapeSW *shape = body->get_shape(p_shape_idx);
  426. ERR_FAIL_COND_V(!shape, RID());
  427. return shape->get_self();
  428. }
  429. void PhysicsServerSW::body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled) {
  430. BodySW *body = body_owner.get(p_body);
  431. ERR_FAIL_COND(!body);
  432. ERR_FAIL_INDEX(p_shape_idx, body->get_shape_count());
  433. FLUSH_QUERY_CHECK(body);
  434. body->set_shape_disabled(p_shape_idx, p_disabled);
  435. }
  436. Transform PhysicsServerSW::body_get_shape_transform(RID p_body, int p_shape_idx) const {
  437. BodySW *body = body_owner.get(p_body);
  438. ERR_FAIL_COND_V(!body, Transform());
  439. return body->get_shape_transform(p_shape_idx);
  440. }
  441. void PhysicsServerSW::body_remove_shape(RID p_body, int p_shape_idx) {
  442. BodySW *body = body_owner.get(p_body);
  443. ERR_FAIL_COND(!body);
  444. body->remove_shape(p_shape_idx);
  445. }
  446. void PhysicsServerSW::body_clear_shapes(RID p_body) {
  447. BodySW *body = body_owner.get(p_body);
  448. ERR_FAIL_COND(!body);
  449. while (body->get_shape_count()) {
  450. body->remove_shape(0);
  451. }
  452. }
  453. void PhysicsServerSW::body_set_enable_continuous_collision_detection(RID p_body, bool p_enable) {
  454. BodySW *body = body_owner.get(p_body);
  455. ERR_FAIL_COND(!body);
  456. body->set_continuous_collision_detection(p_enable);
  457. }
  458. bool PhysicsServerSW::body_is_continuous_collision_detection_enabled(RID p_body) const {
  459. BodySW *body = body_owner.get(p_body);
  460. ERR_FAIL_COND_V(!body, false);
  461. return body->is_continuous_collision_detection_enabled();
  462. }
  463. void PhysicsServerSW::body_set_collision_layer(RID p_body, uint32_t p_layer) {
  464. BodySW *body = body_owner.get(p_body);
  465. ERR_FAIL_COND(!body);
  466. body->set_collision_layer(p_layer);
  467. }
  468. uint32_t PhysicsServerSW::body_get_collision_layer(RID p_body) const {
  469. const BodySW *body = body_owner.get(p_body);
  470. ERR_FAIL_COND_V(!body, 0);
  471. return body->get_collision_layer();
  472. }
  473. void PhysicsServerSW::body_set_collision_mask(RID p_body, uint32_t p_mask) {
  474. BodySW *body = body_owner.get(p_body);
  475. ERR_FAIL_COND(!body);
  476. body->set_collision_mask(p_mask);
  477. }
  478. uint32_t PhysicsServerSW::body_get_collision_mask(RID p_body) const {
  479. const BodySW *body = body_owner.get(p_body);
  480. ERR_FAIL_COND_V(!body, 0);
  481. return body->get_collision_mask();
  482. }
  483. void PhysicsServerSW::body_attach_object_instance_id(RID p_body, uint32_t p_id) {
  484. BodySW *body = body_owner.get(p_body);
  485. ERR_FAIL_COND(!body);
  486. body->set_instance_id(p_id);
  487. };
  488. uint32_t PhysicsServerSW::body_get_object_instance_id(RID p_body) const {
  489. BodySW *body = body_owner.get(p_body);
  490. ERR_FAIL_COND_V(!body, 0);
  491. return body->get_instance_id();
  492. };
  493. void PhysicsServerSW::body_set_user_flags(RID p_body, uint32_t p_flags) {
  494. BodySW *body = body_owner.get(p_body);
  495. ERR_FAIL_COND(!body);
  496. };
  497. uint32_t PhysicsServerSW::body_get_user_flags(RID p_body) const {
  498. BodySW *body = body_owner.get(p_body);
  499. ERR_FAIL_COND_V(!body, 0);
  500. return 0;
  501. };
  502. void PhysicsServerSW::body_set_param(RID p_body, BodyParameter p_param, real_t p_value) {
  503. BodySW *body = body_owner.get(p_body);
  504. ERR_FAIL_COND(!body);
  505. body->set_param(p_param, p_value);
  506. };
  507. real_t PhysicsServerSW::body_get_param(RID p_body, BodyParameter p_param) const {
  508. BodySW *body = body_owner.get(p_body);
  509. ERR_FAIL_COND_V(!body, 0);
  510. return body->get_param(p_param);
  511. };
  512. void PhysicsServerSW::body_set_kinematic_safe_margin(RID p_body, real_t p_margin) {
  513. BodySW *body = body_owner.get(p_body);
  514. ERR_FAIL_COND(!body);
  515. body->set_kinematic_margin(p_margin);
  516. }
  517. real_t PhysicsServerSW::body_get_kinematic_safe_margin(RID p_body) const {
  518. BodySW *body = body_owner.get(p_body);
  519. ERR_FAIL_COND_V(!body, 0);
  520. return body->get_kinematic_margin();
  521. }
  522. void PhysicsServerSW::body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
  523. BodySW *body = body_owner.get(p_body);
  524. ERR_FAIL_COND(!body);
  525. body->set_state(p_state, p_variant);
  526. };
  527. Variant PhysicsServerSW::body_get_state(RID p_body, BodyState p_state) const {
  528. BodySW *body = body_owner.get(p_body);
  529. ERR_FAIL_COND_V(!body, Variant());
  530. return body->get_state(p_state);
  531. };
  532. void PhysicsServerSW::body_set_applied_force(RID p_body, const Vector3 &p_force) {
  533. BodySW *body = body_owner.get(p_body);
  534. ERR_FAIL_COND(!body);
  535. body->set_applied_force(p_force);
  536. body->wakeup();
  537. };
  538. Vector3 PhysicsServerSW::body_get_applied_force(RID p_body) const {
  539. BodySW *body = body_owner.get(p_body);
  540. ERR_FAIL_COND_V(!body, Vector3());
  541. return body->get_applied_force();
  542. };
  543. void PhysicsServerSW::body_set_applied_torque(RID p_body, const Vector3 &p_torque) {
  544. BodySW *body = body_owner.get(p_body);
  545. ERR_FAIL_COND(!body);
  546. body->set_applied_torque(p_torque);
  547. body->wakeup();
  548. };
  549. Vector3 PhysicsServerSW::body_get_applied_torque(RID p_body) const {
  550. BodySW *body = body_owner.get(p_body);
  551. ERR_FAIL_COND_V(!body, Vector3());
  552. return body->get_applied_torque();
  553. };
  554. void PhysicsServerSW::body_add_central_force(RID p_body, const Vector3 &p_force) {
  555. BodySW *body = body_owner.get(p_body);
  556. ERR_FAIL_COND(!body);
  557. body->add_central_force(p_force);
  558. body->wakeup();
  559. }
  560. void PhysicsServerSW::body_add_force(RID p_body, const Vector3 &p_force, const Vector3 &p_pos) {
  561. BodySW *body = body_owner.get(p_body);
  562. ERR_FAIL_COND(!body);
  563. body->add_force(p_force, p_pos);
  564. body->wakeup();
  565. };
  566. void PhysicsServerSW::body_add_torque(RID p_body, const Vector3 &p_torque) {
  567. BodySW *body = body_owner.get(p_body);
  568. ERR_FAIL_COND(!body);
  569. body->add_torque(p_torque);
  570. body->wakeup();
  571. };
  572. void PhysicsServerSW::body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) {
  573. BodySW *body = body_owner.get(p_body);
  574. ERR_FAIL_COND(!body);
  575. _update_shapes();
  576. body->apply_central_impulse(p_impulse);
  577. body->wakeup();
  578. }
  579. void PhysicsServerSW::body_apply_impulse(RID p_body, const Vector3 &p_pos, const Vector3 &p_impulse) {
  580. BodySW *body = body_owner.get(p_body);
  581. ERR_FAIL_COND(!body);
  582. _update_shapes();
  583. body->apply_impulse(p_pos, p_impulse);
  584. body->wakeup();
  585. };
  586. void PhysicsServerSW::body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse) {
  587. BodySW *body = body_owner.get(p_body);
  588. ERR_FAIL_COND(!body);
  589. _update_shapes();
  590. body->apply_torque_impulse(p_impulse);
  591. body->wakeup();
  592. };
  593. void PhysicsServerSW::body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity) {
  594. BodySW *body = body_owner.get(p_body);
  595. ERR_FAIL_COND(!body);
  596. _update_shapes();
  597. Vector3 v = body->get_linear_velocity();
  598. Vector3 axis = p_axis_velocity.normalized();
  599. v -= axis * axis.dot(v);
  600. v += p_axis_velocity;
  601. body->set_linear_velocity(v);
  602. body->wakeup();
  603. };
  604. void PhysicsServerSW::body_set_axis_lock(RID p_body, BodyAxis p_axis, bool p_lock) {
  605. BodySW *body = body_owner.get(p_body);
  606. ERR_FAIL_COND(!body);
  607. body->set_axis_lock(p_axis, p_lock);
  608. body->wakeup();
  609. }
  610. bool PhysicsServerSW::body_is_axis_locked(RID p_body, BodyAxis p_axis) const {
  611. const BodySW *body = body_owner.get(p_body);
  612. ERR_FAIL_COND_V(!body, 0);
  613. return body->is_axis_locked(p_axis);
  614. }
  615. void PhysicsServerSW::body_add_collision_exception(RID p_body, RID p_body_b) {
  616. BodySW *body = body_owner.get(p_body);
  617. ERR_FAIL_COND(!body);
  618. body->add_exception(p_body_b);
  619. body->wakeup();
  620. };
  621. void PhysicsServerSW::body_remove_collision_exception(RID p_body, RID p_body_b) {
  622. BodySW *body = body_owner.get(p_body);
  623. ERR_FAIL_COND(!body);
  624. body->remove_exception(p_body_b);
  625. body->wakeup();
  626. };
  627. void PhysicsServerSW::body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  628. BodySW *body = body_owner.get(p_body);
  629. ERR_FAIL_COND(!body);
  630. for (int i = 0; i < body->get_exceptions().size(); i++) {
  631. p_exceptions->push_back(body->get_exceptions()[i]);
  632. }
  633. };
  634. void PhysicsServerSW::body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) {
  635. BodySW *body = body_owner.get(p_body);
  636. ERR_FAIL_COND(!body);
  637. };
  638. real_t PhysicsServerSW::body_get_contacts_reported_depth_threshold(RID p_body) const {
  639. BodySW *body = body_owner.get(p_body);
  640. ERR_FAIL_COND_V(!body, 0);
  641. return 0;
  642. };
  643. void PhysicsServerSW::body_set_omit_force_integration(RID p_body, bool p_omit) {
  644. BodySW *body = body_owner.get(p_body);
  645. ERR_FAIL_COND(!body);
  646. body->set_omit_force_integration(p_omit);
  647. };
  648. bool PhysicsServerSW::body_is_omitting_force_integration(RID p_body) const {
  649. BodySW *body = body_owner.get(p_body);
  650. ERR_FAIL_COND_V(!body, false);
  651. return body->get_omit_force_integration();
  652. };
  653. void PhysicsServerSW::body_set_max_contacts_reported(RID p_body, int p_contacts) {
  654. BodySW *body = body_owner.get(p_body);
  655. ERR_FAIL_COND(!body);
  656. body->set_max_contacts_reported(p_contacts);
  657. }
  658. int PhysicsServerSW::body_get_max_contacts_reported(RID p_body) const {
  659. BodySW *body = body_owner.get(p_body);
  660. ERR_FAIL_COND_V(!body, -1);
  661. return body->get_max_contacts_reported();
  662. }
  663. void PhysicsServerSW::body_set_force_integration_callback(RID p_body, Object *p_receiver, const StringName &p_method, const Variant &p_udata) {
  664. BodySW *body = body_owner.get(p_body);
  665. ERR_FAIL_COND(!body);
  666. body->set_force_integration_callback(p_receiver ? p_receiver->get_instance_id() : ObjectID(0), p_method, p_udata);
  667. }
  668. void PhysicsServerSW::body_set_ray_pickable(RID p_body, bool p_enable) {
  669. BodySW *body = body_owner.get(p_body);
  670. ERR_FAIL_COND(!body);
  671. body->set_ray_pickable(p_enable);
  672. }
  673. bool PhysicsServerSW::body_is_ray_pickable(RID p_body) const {
  674. BodySW *body = body_owner.get(p_body);
  675. ERR_FAIL_COND_V(!body, false);
  676. return body->is_ray_pickable();
  677. }
  678. bool PhysicsServerSW::body_test_motion(RID p_body, const Transform &p_from, const Vector3 &p_motion, bool p_infinite_inertia, MotionResult *r_result, bool p_exclude_raycast_shapes, const Set<RID> &p_exclude) {
  679. BodySW *body = body_owner.get(p_body);
  680. ERR_FAIL_COND_V(!body, false);
  681. ERR_FAIL_COND_V(!body->get_space(), false);
  682. ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
  683. _update_shapes();
  684. return body->get_space()->test_body_motion(body, p_from, p_motion, p_infinite_inertia, body->get_kinematic_margin(), r_result, p_exclude_raycast_shapes, p_exclude);
  685. }
  686. int PhysicsServerSW::body_test_ray_separation(RID p_body, const Transform &p_transform, bool p_infinite_inertia, Vector3 &r_recover_motion, SeparationResult *r_results, int p_result_max, float p_margin) {
  687. BodySW *body = body_owner.get(p_body);
  688. ERR_FAIL_COND_V(!body, false);
  689. ERR_FAIL_COND_V(!body->get_space(), false);
  690. ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
  691. _update_shapes();
  692. return body->get_space()->test_body_ray_separation(body, p_transform, p_infinite_inertia, r_recover_motion, r_results, p_result_max, p_margin);
  693. }
  694. PhysicsDirectBodyState *PhysicsServerSW::body_get_direct_state(RID p_body) {
  695. if (!body_owner.owns(p_body)) {
  696. return nullptr;
  697. }
  698. BodySW *body = body_owner.get(p_body);
  699. ERR_FAIL_COND_V_MSG(!body, nullptr, "Body with RID " + itos(p_body.get_id()) + " not owned by this server.");
  700. if (!body->get_space()) {
  701. return nullptr;
  702. }
  703. ERR_FAIL_COND_V_MSG(body->get_space()->is_locked(), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
  704. return body->get_direct_state();
  705. }
  706. /* JOINT API */
  707. RID PhysicsServerSW::joint_create_pin(RID p_body_A, const Vector3 &p_local_A, RID p_body_B, const Vector3 &p_local_B) {
  708. BodySW *body_A = body_owner.get(p_body_A);
  709. ERR_FAIL_COND_V(!body_A, RID());
  710. if (!p_body_B.is_valid()) {
  711. ERR_FAIL_COND_V(!body_A->get_space(), RID());
  712. p_body_B = body_A->get_space()->get_static_global_body();
  713. }
  714. BodySW *body_B = body_owner.get(p_body_B);
  715. ERR_FAIL_COND_V(!body_B, RID());
  716. ERR_FAIL_COND_V(body_A == body_B, RID());
  717. JointSW *joint = memnew(PinJointSW(body_A, p_local_A, body_B, p_local_B));
  718. RID rid = joint_owner.make_rid(joint);
  719. joint->set_self(rid);
  720. return rid;
  721. }
  722. void PhysicsServerSW::pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) {
  723. JointSW *joint = joint_owner.get(p_joint);
  724. ERR_FAIL_COND(!joint);
  725. ERR_FAIL_COND(joint->get_type() != JOINT_PIN);
  726. PinJointSW *pin_joint = static_cast<PinJointSW *>(joint);
  727. pin_joint->set_param(p_param, p_value);
  728. }
  729. real_t PhysicsServerSW::pin_joint_get_param(RID p_joint, PinJointParam p_param) const {
  730. JointSW *joint = joint_owner.get(p_joint);
  731. ERR_FAIL_COND_V(!joint, 0);
  732. ERR_FAIL_COND_V(joint->get_type() != JOINT_PIN, 0);
  733. PinJointSW *pin_joint = static_cast<PinJointSW *>(joint);
  734. return pin_joint->get_param(p_param);
  735. }
  736. void PhysicsServerSW::pin_joint_set_local_a(RID p_joint, const Vector3 &p_A) {
  737. JointSW *joint = joint_owner.get(p_joint);
  738. ERR_FAIL_COND(!joint);
  739. ERR_FAIL_COND(joint->get_type() != JOINT_PIN);
  740. PinJointSW *pin_joint = static_cast<PinJointSW *>(joint);
  741. pin_joint->set_pos_a(p_A);
  742. }
  743. Vector3 PhysicsServerSW::pin_joint_get_local_a(RID p_joint) const {
  744. JointSW *joint = joint_owner.get(p_joint);
  745. ERR_FAIL_COND_V(!joint, Vector3());
  746. ERR_FAIL_COND_V(joint->get_type() != JOINT_PIN, Vector3());
  747. PinJointSW *pin_joint = static_cast<PinJointSW *>(joint);
  748. return pin_joint->get_position_a();
  749. }
  750. void PhysicsServerSW::pin_joint_set_local_b(RID p_joint, const Vector3 &p_B) {
  751. JointSW *joint = joint_owner.get(p_joint);
  752. ERR_FAIL_COND(!joint);
  753. ERR_FAIL_COND(joint->get_type() != JOINT_PIN);
  754. PinJointSW *pin_joint = static_cast<PinJointSW *>(joint);
  755. pin_joint->set_pos_b(p_B);
  756. }
  757. Vector3 PhysicsServerSW::pin_joint_get_local_b(RID p_joint) const {
  758. JointSW *joint = joint_owner.get(p_joint);
  759. ERR_FAIL_COND_V(!joint, Vector3());
  760. ERR_FAIL_COND_V(joint->get_type() != JOINT_PIN, Vector3());
  761. PinJointSW *pin_joint = static_cast<PinJointSW *>(joint);
  762. return pin_joint->get_position_b();
  763. }
  764. RID PhysicsServerSW::joint_create_hinge(RID p_body_A, const Transform &p_frame_A, RID p_body_B, const Transform &p_frame_B) {
  765. BodySW *body_A = body_owner.get(p_body_A);
  766. ERR_FAIL_COND_V(!body_A, RID());
  767. if (!p_body_B.is_valid()) {
  768. ERR_FAIL_COND_V(!body_A->get_space(), RID());
  769. p_body_B = body_A->get_space()->get_static_global_body();
  770. }
  771. BodySW *body_B = body_owner.get(p_body_B);
  772. ERR_FAIL_COND_V(!body_B, RID());
  773. ERR_FAIL_COND_V(body_A == body_B, RID());
  774. JointSW *joint = memnew(HingeJointSW(body_A, body_B, p_frame_A, p_frame_B));
  775. RID rid = joint_owner.make_rid(joint);
  776. joint->set_self(rid);
  777. return rid;
  778. }
  779. RID PhysicsServerSW::joint_create_hinge_simple(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) {
  780. BodySW *body_A = body_owner.get(p_body_A);
  781. ERR_FAIL_COND_V(!body_A, RID());
  782. if (!p_body_B.is_valid()) {
  783. ERR_FAIL_COND_V(!body_A->get_space(), RID());
  784. p_body_B = body_A->get_space()->get_static_global_body();
  785. }
  786. BodySW *body_B = body_owner.get(p_body_B);
  787. ERR_FAIL_COND_V(!body_B, RID());
  788. ERR_FAIL_COND_V(body_A == body_B, RID());
  789. JointSW *joint = memnew(HingeJointSW(body_A, body_B, p_pivot_A, p_pivot_B, p_axis_A, p_axis_B));
  790. RID rid = joint_owner.make_rid(joint);
  791. joint->set_self(rid);
  792. return rid;
  793. }
  794. void PhysicsServerSW::hinge_joint_set_param(RID p_joint, HingeJointParam p_param, real_t p_value) {
  795. JointSW *joint = joint_owner.get(p_joint);
  796. ERR_FAIL_COND(!joint);
  797. ERR_FAIL_COND(joint->get_type() != JOINT_HINGE);
  798. HingeJointSW *hinge_joint = static_cast<HingeJointSW *>(joint);
  799. hinge_joint->set_param(p_param, p_value);
  800. }
  801. real_t PhysicsServerSW::hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const {
  802. JointSW *joint = joint_owner.get(p_joint);
  803. ERR_FAIL_COND_V(!joint, 0);
  804. ERR_FAIL_COND_V(joint->get_type() != JOINT_HINGE, 0);
  805. HingeJointSW *hinge_joint = static_cast<HingeJointSW *>(joint);
  806. return hinge_joint->get_param(p_param);
  807. }
  808. void PhysicsServerSW::hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_value) {
  809. JointSW *joint = joint_owner.get(p_joint);
  810. ERR_FAIL_COND(!joint);
  811. ERR_FAIL_COND(joint->get_type() != JOINT_HINGE);
  812. HingeJointSW *hinge_joint = static_cast<HingeJointSW *>(joint);
  813. hinge_joint->set_flag(p_flag, p_value);
  814. }
  815. bool PhysicsServerSW::hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const {
  816. JointSW *joint = joint_owner.get(p_joint);
  817. ERR_FAIL_COND_V(!joint, false);
  818. ERR_FAIL_COND_V(joint->get_type() != JOINT_HINGE, false);
  819. HingeJointSW *hinge_joint = static_cast<HingeJointSW *>(joint);
  820. return hinge_joint->get_flag(p_flag);
  821. }
  822. void PhysicsServerSW::joint_set_solver_priority(RID p_joint, int p_priority) {
  823. JointSW *joint = joint_owner.get(p_joint);
  824. ERR_FAIL_COND(!joint);
  825. joint->set_priority(p_priority);
  826. }
  827. int PhysicsServerSW::joint_get_solver_priority(RID p_joint) const {
  828. JointSW *joint = joint_owner.get(p_joint);
  829. ERR_FAIL_COND_V(!joint, 0);
  830. return joint->get_priority();
  831. }
  832. void PhysicsServerSW::joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable) {
  833. JointSW *joint = joint_owner.get(p_joint);
  834. ERR_FAIL_COND(!joint);
  835. joint->disable_collisions_between_bodies(p_disable);
  836. if (2 == joint->get_body_count()) {
  837. BodySW *body_a = *joint->get_body_ptr();
  838. BodySW *body_b = *(joint->get_body_ptr() + 1);
  839. if (p_disable) {
  840. body_add_collision_exception(body_a->get_self(), body_b->get_self());
  841. body_add_collision_exception(body_b->get_self(), body_a->get_self());
  842. } else {
  843. body_remove_collision_exception(body_a->get_self(), body_b->get_self());
  844. body_remove_collision_exception(body_b->get_self(), body_a->get_self());
  845. }
  846. }
  847. }
  848. bool PhysicsServerSW::joint_is_disabled_collisions_between_bodies(RID p_joint) const {
  849. JointSW *joint = joint_owner.get(p_joint);
  850. ERR_FAIL_COND_V(!joint, true);
  851. return joint->is_disabled_collisions_between_bodies();
  852. }
  853. PhysicsServerSW::JointType PhysicsServerSW::joint_get_type(RID p_joint) const {
  854. JointSW *joint = joint_owner.get(p_joint);
  855. ERR_FAIL_COND_V(!joint, JOINT_PIN);
  856. return joint->get_type();
  857. }
  858. RID PhysicsServerSW::joint_create_slider(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) {
  859. BodySW *body_A = body_owner.get(p_body_A);
  860. ERR_FAIL_COND_V(!body_A, RID());
  861. if (!p_body_B.is_valid()) {
  862. ERR_FAIL_COND_V(!body_A->get_space(), RID());
  863. p_body_B = body_A->get_space()->get_static_global_body();
  864. }
  865. BodySW *body_B = body_owner.get(p_body_B);
  866. ERR_FAIL_COND_V(!body_B, RID());
  867. ERR_FAIL_COND_V(body_A == body_B, RID());
  868. JointSW *joint = memnew(SliderJointSW(body_A, body_B, p_local_frame_A, p_local_frame_B));
  869. RID rid = joint_owner.make_rid(joint);
  870. joint->set_self(rid);
  871. return rid;
  872. }
  873. void PhysicsServerSW::slider_joint_set_param(RID p_joint, SliderJointParam p_param, real_t p_value) {
  874. JointSW *joint = joint_owner.get(p_joint);
  875. ERR_FAIL_COND(!joint);
  876. ERR_FAIL_COND(joint->get_type() != JOINT_SLIDER);
  877. SliderJointSW *slider_joint = static_cast<SliderJointSW *>(joint);
  878. slider_joint->set_param(p_param, p_value);
  879. }
  880. real_t PhysicsServerSW::slider_joint_get_param(RID p_joint, SliderJointParam p_param) const {
  881. JointSW *joint = joint_owner.get(p_joint);
  882. ERR_FAIL_COND_V(!joint, 0);
  883. ERR_FAIL_COND_V(joint->get_type() != JOINT_CONE_TWIST, 0);
  884. SliderJointSW *slider_joint = static_cast<SliderJointSW *>(joint);
  885. return slider_joint->get_param(p_param);
  886. }
  887. RID PhysicsServerSW::joint_create_cone_twist(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) {
  888. BodySW *body_A = body_owner.get(p_body_A);
  889. ERR_FAIL_COND_V(!body_A, RID());
  890. if (!p_body_B.is_valid()) {
  891. ERR_FAIL_COND_V(!body_A->get_space(), RID());
  892. p_body_B = body_A->get_space()->get_static_global_body();
  893. }
  894. BodySW *body_B = body_owner.get(p_body_B);
  895. ERR_FAIL_COND_V(!body_B, RID());
  896. ERR_FAIL_COND_V(body_A == body_B, RID());
  897. JointSW *joint = memnew(ConeTwistJointSW(body_A, body_B, p_local_frame_A, p_local_frame_B));
  898. RID rid = joint_owner.make_rid(joint);
  899. joint->set_self(rid);
  900. return rid;
  901. }
  902. void PhysicsServerSW::cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, real_t p_value) {
  903. JointSW *joint = joint_owner.get(p_joint);
  904. ERR_FAIL_COND(!joint);
  905. ERR_FAIL_COND(joint->get_type() != JOINT_CONE_TWIST);
  906. ConeTwistJointSW *cone_twist_joint = static_cast<ConeTwistJointSW *>(joint);
  907. cone_twist_joint->set_param(p_param, p_value);
  908. }
  909. real_t PhysicsServerSW::cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const {
  910. JointSW *joint = joint_owner.get(p_joint);
  911. ERR_FAIL_COND_V(!joint, 0);
  912. ERR_FAIL_COND_V(joint->get_type() != JOINT_CONE_TWIST, 0);
  913. ConeTwistJointSW *cone_twist_joint = static_cast<ConeTwistJointSW *>(joint);
  914. return cone_twist_joint->get_param(p_param);
  915. }
  916. RID PhysicsServerSW::joint_create_generic_6dof(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) {
  917. BodySW *body_A = body_owner.get(p_body_A);
  918. ERR_FAIL_COND_V(!body_A, RID());
  919. if (!p_body_B.is_valid()) {
  920. ERR_FAIL_COND_V(!body_A->get_space(), RID());
  921. p_body_B = body_A->get_space()->get_static_global_body();
  922. }
  923. BodySW *body_B = body_owner.get(p_body_B);
  924. ERR_FAIL_COND_V(!body_B, RID());
  925. ERR_FAIL_COND_V(body_A == body_B, RID());
  926. JointSW *joint = memnew(Generic6DOFJointSW(body_A, body_B, p_local_frame_A, p_local_frame_B, true));
  927. RID rid = joint_owner.make_rid(joint);
  928. joint->set_self(rid);
  929. return rid;
  930. }
  931. void PhysicsServerSW::generic_6dof_joint_set_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param, real_t p_value) {
  932. JointSW *joint = joint_owner.get(p_joint);
  933. ERR_FAIL_COND(!joint);
  934. ERR_FAIL_COND(joint->get_type() != JOINT_6DOF);
  935. Generic6DOFJointSW *generic_6dof_joint = static_cast<Generic6DOFJointSW *>(joint);
  936. generic_6dof_joint->set_param(p_axis, p_param, p_value);
  937. }
  938. real_t PhysicsServerSW::generic_6dof_joint_get_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param) {
  939. JointSW *joint = joint_owner.get(p_joint);
  940. ERR_FAIL_COND_V(!joint, 0);
  941. ERR_FAIL_COND_V(joint->get_type() != JOINT_6DOF, 0);
  942. Generic6DOFJointSW *generic_6dof_joint = static_cast<Generic6DOFJointSW *>(joint);
  943. return generic_6dof_joint->get_param(p_axis, p_param);
  944. }
  945. void PhysicsServerSW::generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag, bool p_enable) {
  946. JointSW *joint = joint_owner.get(p_joint);
  947. ERR_FAIL_COND(!joint);
  948. ERR_FAIL_COND(joint->get_type() != JOINT_6DOF);
  949. Generic6DOFJointSW *generic_6dof_joint = static_cast<Generic6DOFJointSW *>(joint);
  950. generic_6dof_joint->set_flag(p_axis, p_flag, p_enable);
  951. }
  952. bool PhysicsServerSW::generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag) {
  953. JointSW *joint = joint_owner.get(p_joint);
  954. ERR_FAIL_COND_V(!joint, false);
  955. ERR_FAIL_COND_V(joint->get_type() != JOINT_6DOF, false);
  956. Generic6DOFJointSW *generic_6dof_joint = static_cast<Generic6DOFJointSW *>(joint);
  957. return generic_6dof_joint->get_flag(p_axis, p_flag);
  958. }
  959. void PhysicsServerSW::free(RID p_rid) {
  960. _update_shapes(); //just in case
  961. if (shape_owner.owns(p_rid)) {
  962. ShapeSW *shape = shape_owner.get(p_rid);
  963. while (shape->get_owners().size()) {
  964. ShapeOwnerSW *so = shape->get_owners().front()->key();
  965. so->remove_shape(shape);
  966. }
  967. shape_owner.free(p_rid);
  968. memdelete(shape);
  969. } else if (body_owner.owns(p_rid)) {
  970. BodySW *body = body_owner.get(p_rid);
  971. /*
  972. if (body->get_state_query())
  973. _clear_query(body->get_state_query());
  974. if (body->get_direct_state_query())
  975. _clear_query(body->get_direct_state_query());
  976. */
  977. body->set_space(nullptr);
  978. while (body->get_shape_count()) {
  979. body->remove_shape(0);
  980. }
  981. body_owner.free(p_rid);
  982. memdelete(body);
  983. } else if (area_owner.owns(p_rid)) {
  984. AreaSW *area = area_owner.get(p_rid);
  985. /*
  986. if (area->get_monitor_query())
  987. _clear_query(area->get_monitor_query());
  988. */
  989. area->set_space(nullptr);
  990. while (area->get_shape_count()) {
  991. area->remove_shape(0);
  992. }
  993. area_owner.free(p_rid);
  994. memdelete(area);
  995. } else if (space_owner.owns(p_rid)) {
  996. SpaceSW *space = space_owner.get(p_rid);
  997. while (space->get_objects().size()) {
  998. CollisionObjectSW *co = (CollisionObjectSW *)space->get_objects().front()->get();
  999. co->set_space(nullptr);
  1000. }
  1001. active_spaces.erase(space);
  1002. free(space->get_default_area()->get_self());
  1003. free(space->get_static_global_body());
  1004. space_owner.free(p_rid);
  1005. memdelete(space);
  1006. } else if (joint_owner.owns(p_rid)) {
  1007. JointSW *joint = joint_owner.get(p_rid);
  1008. for (int i = 0; i < joint->get_body_count(); i++) {
  1009. joint->get_body_ptr()[i]->remove_constraint(joint);
  1010. }
  1011. joint_owner.free(p_rid);
  1012. memdelete(joint);
  1013. } else {
  1014. ERR_FAIL_MSG("Invalid ID.");
  1015. }
  1016. };
  1017. void PhysicsServerSW::set_active(bool p_active) {
  1018. active = p_active;
  1019. };
  1020. void PhysicsServerSW::set_collision_iterations(int p_iterations) {
  1021. iterations = p_iterations;
  1022. };
  1023. void PhysicsServerSW::init() {
  1024. iterations = 8; // 8?
  1025. stepper = memnew(StepSW);
  1026. };
  1027. void PhysicsServerSW::step(real_t p_step) {
  1028. #ifndef _3D_DISABLED
  1029. if (!active) {
  1030. return;
  1031. }
  1032. _update_shapes();
  1033. island_count = 0;
  1034. active_objects = 0;
  1035. collision_pairs = 0;
  1036. for (Set<const SpaceSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  1037. stepper->step((SpaceSW *)E->get(), p_step, iterations);
  1038. island_count += E->get()->get_island_count();
  1039. active_objects += E->get()->get_active_objects();
  1040. collision_pairs += E->get()->get_collision_pairs();
  1041. }
  1042. #endif
  1043. }
  1044. void PhysicsServerSW::flush_queries() {
  1045. #ifndef _3D_DISABLED
  1046. if (!active) {
  1047. return;
  1048. }
  1049. flushing_queries = true;
  1050. uint64_t time_beg = OS::get_singleton()->get_ticks_usec();
  1051. for (Set<const SpaceSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  1052. SpaceSW *space = (SpaceSW *)E->get();
  1053. space->call_queries();
  1054. }
  1055. flushing_queries = false;
  1056. if (ScriptDebugger::get_singleton() && ScriptDebugger::get_singleton()->is_profiling()) {
  1057. uint64_t total_time[SpaceSW::ELAPSED_TIME_MAX];
  1058. static const char *time_name[SpaceSW::ELAPSED_TIME_MAX] = {
  1059. "integrate_forces",
  1060. "generate_islands",
  1061. "setup_constraints",
  1062. "solve_constraints",
  1063. "integrate_velocities"
  1064. };
  1065. for (int i = 0; i < SpaceSW::ELAPSED_TIME_MAX; i++) {
  1066. total_time[i] = 0;
  1067. }
  1068. for (Set<const SpaceSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  1069. for (int i = 0; i < SpaceSW::ELAPSED_TIME_MAX; i++) {
  1070. total_time[i] += E->get()->get_elapsed_time(SpaceSW::ElapsedTime(i));
  1071. }
  1072. }
  1073. Array values;
  1074. values.resize(SpaceSW::ELAPSED_TIME_MAX * 2);
  1075. for (int i = 0; i < SpaceSW::ELAPSED_TIME_MAX; i++) {
  1076. values[i * 2 + 0] = time_name[i];
  1077. values[i * 2 + 1] = USEC_TO_SEC(total_time[i]);
  1078. }
  1079. values.push_back("flush_queries");
  1080. values.push_back(USEC_TO_SEC(OS::get_singleton()->get_ticks_usec() - time_beg));
  1081. ScriptDebugger::get_singleton()->add_profiling_frame_data("physics_3d", values);
  1082. }
  1083. #endif
  1084. };
  1085. void PhysicsServerSW::finish() {
  1086. memdelete(stepper);
  1087. };
  1088. int PhysicsServerSW::get_process_info(ProcessInfo p_info) {
  1089. switch (p_info) {
  1090. case INFO_ACTIVE_OBJECTS: {
  1091. return active_objects;
  1092. } break;
  1093. case INFO_COLLISION_PAIRS: {
  1094. return collision_pairs;
  1095. } break;
  1096. case INFO_ISLAND_COUNT: {
  1097. return island_count;
  1098. } break;
  1099. }
  1100. return 0;
  1101. }
  1102. void PhysicsServerSW::_update_shapes() {
  1103. while (pending_shape_update_list.first()) {
  1104. pending_shape_update_list.first()->self()->_shape_changed();
  1105. pending_shape_update_list.remove(pending_shape_update_list.first());
  1106. }
  1107. }
  1108. void PhysicsServerSW::_shape_col_cbk(const Vector3 &p_point_A, const Vector3 &p_point_B, void *p_userdata) {
  1109. CollCbkData *cbk = (CollCbkData *)p_userdata;
  1110. if (cbk->max == 0) {
  1111. return;
  1112. }
  1113. if (cbk->amount == cbk->max) {
  1114. //find least deep
  1115. real_t min_depth = 1e20;
  1116. int min_depth_idx = 0;
  1117. for (int i = 0; i < cbk->amount; i++) {
  1118. real_t d = cbk->ptr[i * 2 + 0].distance_squared_to(cbk->ptr[i * 2 + 1]);
  1119. if (d < min_depth) {
  1120. min_depth = d;
  1121. min_depth_idx = i;
  1122. }
  1123. }
  1124. real_t d = p_point_A.distance_squared_to(p_point_B);
  1125. if (d < min_depth) {
  1126. return;
  1127. }
  1128. cbk->ptr[min_depth_idx * 2 + 0] = p_point_A;
  1129. cbk->ptr[min_depth_idx * 2 + 1] = p_point_B;
  1130. } else {
  1131. cbk->ptr[cbk->amount * 2 + 0] = p_point_A;
  1132. cbk->ptr[cbk->amount * 2 + 1] = p_point_B;
  1133. cbk->amount++;
  1134. }
  1135. }
  1136. PhysicsServerSW *PhysicsServerSW::singleton = nullptr;
  1137. PhysicsServerSW::PhysicsServerSW() {
  1138. singleton = this;
  1139. bool use_bvh_or_octree = GLOBAL_GET("physics/3d/godot_physics/use_bvh");
  1140. if (use_bvh_or_octree) {
  1141. BroadPhaseSW::create_func = BroadPhaseBVH::_create;
  1142. } else {
  1143. BroadPhaseSW::create_func = BroadPhaseOctree::_create;
  1144. }
  1145. island_count = 0;
  1146. active_objects = 0;
  1147. collision_pairs = 0;
  1148. active = true;
  1149. flushing_queries = false;
  1150. };
  1151. PhysicsServerSW::~PhysicsServerSW(){
  1152. };