physics_server_sw.cpp 43 KB

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