physics_server_sw.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_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 = 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 = 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_as_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_as_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. body->wakeup();
  468. }
  469. uint32_t PhysicsServerSW::body_get_collision_layer(RID p_body) const {
  470. const BodySW *body = body_owner.get(p_body);
  471. ERR_FAIL_COND_V(!body, 0);
  472. return body->get_collision_layer();
  473. }
  474. void PhysicsServerSW::body_set_collision_mask(RID p_body, uint32_t p_mask) {
  475. BodySW *body = body_owner.get(p_body);
  476. ERR_FAIL_COND(!body);
  477. body->set_collision_mask(p_mask);
  478. body->wakeup();
  479. }
  480. uint32_t PhysicsServerSW::body_get_collision_mask(RID p_body) const {
  481. const BodySW *body = body_owner.get(p_body);
  482. ERR_FAIL_COND_V(!body, 0);
  483. return body->get_collision_mask();
  484. }
  485. void PhysicsServerSW::body_attach_object_instance_id(RID p_body, uint32_t p_id) {
  486. BodySW *body = body_owner.get(p_body);
  487. ERR_FAIL_COND(!body);
  488. body->set_instance_id(p_id);
  489. };
  490. uint32_t PhysicsServerSW::body_get_object_instance_id(RID p_body) const {
  491. BodySW *body = body_owner.get(p_body);
  492. ERR_FAIL_COND_V(!body, 0);
  493. return body->get_instance_id();
  494. };
  495. void PhysicsServerSW::body_set_user_flags(RID p_body, uint32_t p_flags) {
  496. BodySW *body = body_owner.get(p_body);
  497. ERR_FAIL_COND(!body);
  498. };
  499. uint32_t PhysicsServerSW::body_get_user_flags(RID p_body) const {
  500. BodySW *body = body_owner.get(p_body);
  501. ERR_FAIL_COND_V(!body, 0);
  502. return 0;
  503. };
  504. void PhysicsServerSW::body_set_param(RID p_body, BodyParameter p_param, real_t p_value) {
  505. BodySW *body = body_owner.get(p_body);
  506. ERR_FAIL_COND(!body);
  507. body->set_param(p_param, p_value);
  508. };
  509. real_t PhysicsServerSW::body_get_param(RID p_body, BodyParameter p_param) const {
  510. BodySW *body = body_owner.get(p_body);
  511. ERR_FAIL_COND_V(!body, 0);
  512. return body->get_param(p_param);
  513. };
  514. void PhysicsServerSW::body_set_kinematic_safe_margin(RID p_body, real_t p_margin) {
  515. BodySW *body = body_owner.get(p_body);
  516. ERR_FAIL_COND(!body);
  517. body->set_kinematic_margin(p_margin);
  518. }
  519. real_t PhysicsServerSW::body_get_kinematic_safe_margin(RID p_body) const {
  520. BodySW *body = body_owner.get(p_body);
  521. ERR_FAIL_COND_V(!body, 0);
  522. return body->get_kinematic_margin();
  523. }
  524. void PhysicsServerSW::body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
  525. BodySW *body = body_owner.get(p_body);
  526. ERR_FAIL_COND(!body);
  527. body->set_state(p_state, p_variant);
  528. };
  529. Variant PhysicsServerSW::body_get_state(RID p_body, BodyState p_state) const {
  530. BodySW *body = body_owner.get(p_body);
  531. ERR_FAIL_COND_V(!body, Variant());
  532. return body->get_state(p_state);
  533. };
  534. void PhysicsServerSW::body_set_applied_force(RID p_body, const Vector3 &p_force) {
  535. BodySW *body = body_owner.get(p_body);
  536. ERR_FAIL_COND(!body);
  537. body->set_applied_force(p_force);
  538. body->wakeup();
  539. };
  540. Vector3 PhysicsServerSW::body_get_applied_force(RID p_body) const {
  541. BodySW *body = body_owner.get(p_body);
  542. ERR_FAIL_COND_V(!body, Vector3());
  543. return body->get_applied_force();
  544. };
  545. void PhysicsServerSW::body_set_applied_torque(RID p_body, const Vector3 &p_torque) {
  546. BodySW *body = body_owner.get(p_body);
  547. ERR_FAIL_COND(!body);
  548. body->set_applied_torque(p_torque);
  549. body->wakeup();
  550. };
  551. Vector3 PhysicsServerSW::body_get_applied_torque(RID p_body) const {
  552. BodySW *body = body_owner.get(p_body);
  553. ERR_FAIL_COND_V(!body, Vector3());
  554. return body->get_applied_torque();
  555. };
  556. void PhysicsServerSW::body_add_central_force(RID p_body, const Vector3 &p_force) {
  557. BodySW *body = body_owner.get(p_body);
  558. ERR_FAIL_COND(!body);
  559. body->add_central_force(p_force);
  560. body->wakeup();
  561. }
  562. void PhysicsServerSW::body_add_force(RID p_body, const Vector3 &p_force, const Vector3 &p_pos) {
  563. BodySW *body = body_owner.get(p_body);
  564. ERR_FAIL_COND(!body);
  565. body->add_force(p_force, p_pos);
  566. body->wakeup();
  567. };
  568. void PhysicsServerSW::body_add_torque(RID p_body, const Vector3 &p_torque) {
  569. BodySW *body = body_owner.get(p_body);
  570. ERR_FAIL_COND(!body);
  571. body->add_torque(p_torque);
  572. body->wakeup();
  573. };
  574. void PhysicsServerSW::body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) {
  575. BodySW *body = body_owner.get(p_body);
  576. ERR_FAIL_COND(!body);
  577. _update_shapes();
  578. body->apply_central_impulse(p_impulse);
  579. body->wakeup();
  580. }
  581. void PhysicsServerSW::body_apply_impulse(RID p_body, const Vector3 &p_pos, const Vector3 &p_impulse) {
  582. BodySW *body = body_owner.get(p_body);
  583. ERR_FAIL_COND(!body);
  584. _update_shapes();
  585. body->apply_impulse(p_pos, p_impulse);
  586. body->wakeup();
  587. };
  588. void PhysicsServerSW::body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse) {
  589. BodySW *body = body_owner.get(p_body);
  590. ERR_FAIL_COND(!body);
  591. _update_shapes();
  592. body->apply_torque_impulse(p_impulse);
  593. body->wakeup();
  594. };
  595. void PhysicsServerSW::body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity) {
  596. BodySW *body = body_owner.get(p_body);
  597. ERR_FAIL_COND(!body);
  598. _update_shapes();
  599. Vector3 v = body->get_linear_velocity();
  600. Vector3 axis = p_axis_velocity.normalized();
  601. v -= axis * axis.dot(v);
  602. v += p_axis_velocity;
  603. body->set_linear_velocity(v);
  604. body->wakeup();
  605. };
  606. void PhysicsServerSW::body_set_axis_lock(RID p_body, BodyAxis p_axis, bool p_lock) {
  607. BodySW *body = body_owner.get(p_body);
  608. ERR_FAIL_COND(!body);
  609. body->set_axis_lock(p_axis, p_lock);
  610. body->wakeup();
  611. }
  612. bool PhysicsServerSW::body_is_axis_locked(RID p_body, BodyAxis p_axis) const {
  613. const BodySW *body = body_owner.get(p_body);
  614. ERR_FAIL_COND_V(!body, 0);
  615. return body->is_axis_locked(p_axis);
  616. }
  617. void PhysicsServerSW::body_add_collision_exception(RID p_body, RID p_body_b) {
  618. BodySW *body = body_owner.get(p_body);
  619. ERR_FAIL_COND(!body);
  620. body->add_exception(p_body_b);
  621. body->wakeup();
  622. };
  623. void PhysicsServerSW::body_remove_collision_exception(RID p_body, RID p_body_b) {
  624. BodySW *body = body_owner.get(p_body);
  625. ERR_FAIL_COND(!body);
  626. body->remove_exception(p_body_b);
  627. body->wakeup();
  628. };
  629. void PhysicsServerSW::body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  630. BodySW *body = body_owner.get(p_body);
  631. ERR_FAIL_COND(!body);
  632. for (int i = 0; i < body->get_exceptions().size(); i++) {
  633. p_exceptions->push_back(body->get_exceptions()[i]);
  634. }
  635. };
  636. void PhysicsServerSW::body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) {
  637. BodySW *body = body_owner.get(p_body);
  638. ERR_FAIL_COND(!body);
  639. };
  640. real_t PhysicsServerSW::body_get_contacts_reported_depth_threshold(RID p_body) const {
  641. BodySW *body = body_owner.get(p_body);
  642. ERR_FAIL_COND_V(!body, 0);
  643. return 0;
  644. };
  645. void PhysicsServerSW::body_set_omit_force_integration(RID p_body, bool p_omit) {
  646. BodySW *body = body_owner.get(p_body);
  647. ERR_FAIL_COND(!body);
  648. body->set_omit_force_integration(p_omit);
  649. };
  650. bool PhysicsServerSW::body_is_omitting_force_integration(RID p_body) const {
  651. BodySW *body = body_owner.get(p_body);
  652. ERR_FAIL_COND_V(!body, false);
  653. return body->get_omit_force_integration();
  654. };
  655. void PhysicsServerSW::body_set_max_contacts_reported(RID p_body, int p_contacts) {
  656. BodySW *body = body_owner.get(p_body);
  657. ERR_FAIL_COND(!body);
  658. body->set_max_contacts_reported(p_contacts);
  659. }
  660. int PhysicsServerSW::body_get_max_contacts_reported(RID p_body) const {
  661. BodySW *body = body_owner.get(p_body);
  662. ERR_FAIL_COND_V(!body, -1);
  663. return body->get_max_contacts_reported();
  664. }
  665. void PhysicsServerSW::body_set_force_integration_callback(RID p_body, Object *p_receiver, const StringName &p_method, const Variant &p_udata) {
  666. BodySW *body = body_owner.get(p_body);
  667. ERR_FAIL_COND(!body);
  668. body->set_force_integration_callback(p_receiver ? p_receiver->get_instance_id() : ObjectID(0), p_method, p_udata);
  669. }
  670. void PhysicsServerSW::body_set_ray_pickable(RID p_body, bool p_enable) {
  671. BodySW *body = body_owner.get(p_body);
  672. ERR_FAIL_COND(!body);
  673. body->set_ray_pickable(p_enable);
  674. }
  675. bool PhysicsServerSW::body_is_ray_pickable(RID p_body) const {
  676. BodySW *body = body_owner.get(p_body);
  677. ERR_FAIL_COND_V(!body, false);
  678. return body->is_ray_pickable();
  679. }
  680. 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) {
  681. BodySW *body = body_owner.get(p_body);
  682. ERR_FAIL_COND_V(!body, false);
  683. ERR_FAIL_COND_V(!body->get_space(), false);
  684. ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
  685. _update_shapes();
  686. 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);
  687. }
  688. 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) {
  689. BodySW *body = body_owner.get(p_body);
  690. ERR_FAIL_COND_V(!body, false);
  691. ERR_FAIL_COND_V(!body->get_space(), false);
  692. ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
  693. _update_shapes();
  694. return body->get_space()->test_body_ray_separation(body, p_transform, p_infinite_inertia, r_recover_motion, r_results, p_result_max, p_margin);
  695. }
  696. PhysicsDirectBodyState *PhysicsServerSW::body_get_direct_state(RID p_body) {
  697. BodySW *body = body_owner.get(p_body);
  698. ERR_FAIL_COND_V(!body, nullptr);
  699. ERR_FAIL_COND_V_MSG(body->get_space()->is_locked(), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
  700. direct_state->body = body;
  701. return direct_state;
  702. }
  703. /* JOINT API */
  704. RID PhysicsServerSW::joint_create_pin(RID p_body_A, const Vector3 &p_local_A, RID p_body_B, const Vector3 &p_local_B) {
  705. BodySW *body_A = body_owner.get(p_body_A);
  706. ERR_FAIL_COND_V(!body_A, RID());
  707. if (!p_body_B.is_valid()) {
  708. ERR_FAIL_COND_V(!body_A->get_space(), RID());
  709. p_body_B = body_A->get_space()->get_static_global_body();
  710. }
  711. BodySW *body_B = body_owner.get(p_body_B);
  712. ERR_FAIL_COND_V(!body_B, RID());
  713. ERR_FAIL_COND_V(body_A == body_B, RID());
  714. JointSW *joint = memnew(PinJointSW(body_A, p_local_A, body_B, p_local_B));
  715. RID rid = joint_owner.make_rid(joint);
  716. joint->set_self(rid);
  717. return rid;
  718. }
  719. void PhysicsServerSW::pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) {
  720. JointSW *joint = joint_owner.get(p_joint);
  721. ERR_FAIL_COND(!joint);
  722. ERR_FAIL_COND(joint->get_type() != JOINT_PIN);
  723. PinJointSW *pin_joint = static_cast<PinJointSW *>(joint);
  724. pin_joint->set_param(p_param, p_value);
  725. }
  726. real_t PhysicsServerSW::pin_joint_get_param(RID p_joint, PinJointParam p_param) const {
  727. JointSW *joint = joint_owner.get(p_joint);
  728. ERR_FAIL_COND_V(!joint, 0);
  729. ERR_FAIL_COND_V(joint->get_type() != JOINT_PIN, 0);
  730. PinJointSW *pin_joint = static_cast<PinJointSW *>(joint);
  731. return pin_joint->get_param(p_param);
  732. }
  733. void PhysicsServerSW::pin_joint_set_local_a(RID p_joint, const Vector3 &p_A) {
  734. JointSW *joint = joint_owner.get(p_joint);
  735. ERR_FAIL_COND(!joint);
  736. ERR_FAIL_COND(joint->get_type() != JOINT_PIN);
  737. PinJointSW *pin_joint = static_cast<PinJointSW *>(joint);
  738. pin_joint->set_pos_a(p_A);
  739. }
  740. Vector3 PhysicsServerSW::pin_joint_get_local_a(RID p_joint) const {
  741. JointSW *joint = joint_owner.get(p_joint);
  742. ERR_FAIL_COND_V(!joint, Vector3());
  743. ERR_FAIL_COND_V(joint->get_type() != JOINT_PIN, Vector3());
  744. PinJointSW *pin_joint = static_cast<PinJointSW *>(joint);
  745. return pin_joint->get_position_a();
  746. }
  747. void PhysicsServerSW::pin_joint_set_local_b(RID p_joint, const Vector3 &p_B) {
  748. JointSW *joint = joint_owner.get(p_joint);
  749. ERR_FAIL_COND(!joint);
  750. ERR_FAIL_COND(joint->get_type() != JOINT_PIN);
  751. PinJointSW *pin_joint = static_cast<PinJointSW *>(joint);
  752. pin_joint->set_pos_b(p_B);
  753. }
  754. Vector3 PhysicsServerSW::pin_joint_get_local_b(RID p_joint) const {
  755. JointSW *joint = joint_owner.get(p_joint);
  756. ERR_FAIL_COND_V(!joint, Vector3());
  757. ERR_FAIL_COND_V(joint->get_type() != JOINT_PIN, Vector3());
  758. PinJointSW *pin_joint = static_cast<PinJointSW *>(joint);
  759. return pin_joint->get_position_b();
  760. }
  761. RID PhysicsServerSW::joint_create_hinge(RID p_body_A, const Transform &p_frame_A, RID p_body_B, const Transform &p_frame_B) {
  762. BodySW *body_A = body_owner.get(p_body_A);
  763. ERR_FAIL_COND_V(!body_A, RID());
  764. if (!p_body_B.is_valid()) {
  765. ERR_FAIL_COND_V(!body_A->get_space(), RID());
  766. p_body_B = body_A->get_space()->get_static_global_body();
  767. }
  768. BodySW *body_B = body_owner.get(p_body_B);
  769. ERR_FAIL_COND_V(!body_B, RID());
  770. ERR_FAIL_COND_V(body_A == body_B, RID());
  771. JointSW *joint = memnew(HingeJointSW(body_A, body_B, p_frame_A, p_frame_B));
  772. RID rid = joint_owner.make_rid(joint);
  773. joint->set_self(rid);
  774. return rid;
  775. }
  776. 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) {
  777. BodySW *body_A = body_owner.get(p_body_A);
  778. ERR_FAIL_COND_V(!body_A, RID());
  779. if (!p_body_B.is_valid()) {
  780. ERR_FAIL_COND_V(!body_A->get_space(), RID());
  781. p_body_B = body_A->get_space()->get_static_global_body();
  782. }
  783. BodySW *body_B = body_owner.get(p_body_B);
  784. ERR_FAIL_COND_V(!body_B, RID());
  785. ERR_FAIL_COND_V(body_A == body_B, RID());
  786. JointSW *joint = memnew(HingeJointSW(body_A, body_B, p_pivot_A, p_pivot_B, p_axis_A, p_axis_B));
  787. RID rid = joint_owner.make_rid(joint);
  788. joint->set_self(rid);
  789. return rid;
  790. }
  791. void PhysicsServerSW::hinge_joint_set_param(RID p_joint, HingeJointParam p_param, real_t p_value) {
  792. JointSW *joint = joint_owner.get(p_joint);
  793. ERR_FAIL_COND(!joint);
  794. ERR_FAIL_COND(joint->get_type() != JOINT_HINGE);
  795. HingeJointSW *hinge_joint = static_cast<HingeJointSW *>(joint);
  796. hinge_joint->set_param(p_param, p_value);
  797. }
  798. real_t PhysicsServerSW::hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const {
  799. JointSW *joint = joint_owner.get(p_joint);
  800. ERR_FAIL_COND_V(!joint, 0);
  801. ERR_FAIL_COND_V(joint->get_type() != JOINT_HINGE, 0);
  802. HingeJointSW *hinge_joint = static_cast<HingeJointSW *>(joint);
  803. return hinge_joint->get_param(p_param);
  804. }
  805. void PhysicsServerSW::hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_value) {
  806. JointSW *joint = joint_owner.get(p_joint);
  807. ERR_FAIL_COND(!joint);
  808. ERR_FAIL_COND(joint->get_type() != JOINT_HINGE);
  809. HingeJointSW *hinge_joint = static_cast<HingeJointSW *>(joint);
  810. hinge_joint->set_flag(p_flag, p_value);
  811. }
  812. bool PhysicsServerSW::hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const {
  813. JointSW *joint = joint_owner.get(p_joint);
  814. ERR_FAIL_COND_V(!joint, false);
  815. ERR_FAIL_COND_V(joint->get_type() != JOINT_HINGE, false);
  816. HingeJointSW *hinge_joint = static_cast<HingeJointSW *>(joint);
  817. return hinge_joint->get_flag(p_flag);
  818. }
  819. void PhysicsServerSW::joint_set_solver_priority(RID p_joint, int p_priority) {
  820. JointSW *joint = joint_owner.get(p_joint);
  821. ERR_FAIL_COND(!joint);
  822. joint->set_priority(p_priority);
  823. }
  824. int PhysicsServerSW::joint_get_solver_priority(RID p_joint) const {
  825. JointSW *joint = joint_owner.get(p_joint);
  826. ERR_FAIL_COND_V(!joint, 0);
  827. return joint->get_priority();
  828. }
  829. void PhysicsServerSW::joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable) {
  830. JointSW *joint = joint_owner.get(p_joint);
  831. ERR_FAIL_COND(!joint);
  832. joint->disable_collisions_between_bodies(p_disable);
  833. if (2 == joint->get_body_count()) {
  834. BodySW *body_a = *joint->get_body_ptr();
  835. BodySW *body_b = *(joint->get_body_ptr() + 1);
  836. if (p_disable) {
  837. body_add_collision_exception(body_a->get_self(), body_b->get_self());
  838. body_add_collision_exception(body_b->get_self(), body_a->get_self());
  839. } else {
  840. body_remove_collision_exception(body_a->get_self(), body_b->get_self());
  841. body_remove_collision_exception(body_b->get_self(), body_a->get_self());
  842. }
  843. }
  844. }
  845. bool PhysicsServerSW::joint_is_disabled_collisions_between_bodies(RID p_joint) const {
  846. JointSW *joint = joint_owner.get(p_joint);
  847. ERR_FAIL_COND_V(!joint, true);
  848. return joint->is_disabled_collisions_between_bodies();
  849. }
  850. PhysicsServerSW::JointType PhysicsServerSW::joint_get_type(RID p_joint) const {
  851. JointSW *joint = joint_owner.get(p_joint);
  852. ERR_FAIL_COND_V(!joint, JOINT_PIN);
  853. return joint->get_type();
  854. }
  855. 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) {
  856. BodySW *body_A = body_owner.get(p_body_A);
  857. ERR_FAIL_COND_V(!body_A, RID());
  858. if (!p_body_B.is_valid()) {
  859. ERR_FAIL_COND_V(!body_A->get_space(), RID());
  860. p_body_B = body_A->get_space()->get_static_global_body();
  861. }
  862. BodySW *body_B = body_owner.get(p_body_B);
  863. ERR_FAIL_COND_V(!body_B, RID());
  864. ERR_FAIL_COND_V(body_A == body_B, RID());
  865. JointSW *joint = memnew(SliderJointSW(body_A, body_B, p_local_frame_A, p_local_frame_B));
  866. RID rid = joint_owner.make_rid(joint);
  867. joint->set_self(rid);
  868. return rid;
  869. }
  870. void PhysicsServerSW::slider_joint_set_param(RID p_joint, SliderJointParam p_param, real_t p_value) {
  871. JointSW *joint = joint_owner.get(p_joint);
  872. ERR_FAIL_COND(!joint);
  873. ERR_FAIL_COND(joint->get_type() != JOINT_SLIDER);
  874. SliderJointSW *slider_joint = static_cast<SliderJointSW *>(joint);
  875. slider_joint->set_param(p_param, p_value);
  876. }
  877. real_t PhysicsServerSW::slider_joint_get_param(RID p_joint, SliderJointParam p_param) const {
  878. JointSW *joint = joint_owner.get(p_joint);
  879. ERR_FAIL_COND_V(!joint, 0);
  880. ERR_FAIL_COND_V(joint->get_type() != JOINT_CONE_TWIST, 0);
  881. SliderJointSW *slider_joint = static_cast<SliderJointSW *>(joint);
  882. return slider_joint->get_param(p_param);
  883. }
  884. 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) {
  885. BodySW *body_A = body_owner.get(p_body_A);
  886. ERR_FAIL_COND_V(!body_A, RID());
  887. if (!p_body_B.is_valid()) {
  888. ERR_FAIL_COND_V(!body_A->get_space(), RID());
  889. p_body_B = body_A->get_space()->get_static_global_body();
  890. }
  891. BodySW *body_B = body_owner.get(p_body_B);
  892. ERR_FAIL_COND_V(!body_B, RID());
  893. ERR_FAIL_COND_V(body_A == body_B, RID());
  894. JointSW *joint = memnew(ConeTwistJointSW(body_A, body_B, p_local_frame_A, p_local_frame_B));
  895. RID rid = joint_owner.make_rid(joint);
  896. joint->set_self(rid);
  897. return rid;
  898. }
  899. void PhysicsServerSW::cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, real_t p_value) {
  900. JointSW *joint = joint_owner.get(p_joint);
  901. ERR_FAIL_COND(!joint);
  902. ERR_FAIL_COND(joint->get_type() != JOINT_CONE_TWIST);
  903. ConeTwistJointSW *cone_twist_joint = static_cast<ConeTwistJointSW *>(joint);
  904. cone_twist_joint->set_param(p_param, p_value);
  905. }
  906. real_t PhysicsServerSW::cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const {
  907. JointSW *joint = joint_owner.get(p_joint);
  908. ERR_FAIL_COND_V(!joint, 0);
  909. ERR_FAIL_COND_V(joint->get_type() != JOINT_CONE_TWIST, 0);
  910. ConeTwistJointSW *cone_twist_joint = static_cast<ConeTwistJointSW *>(joint);
  911. return cone_twist_joint->get_param(p_param);
  912. }
  913. 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) {
  914. BodySW *body_A = body_owner.get(p_body_A);
  915. ERR_FAIL_COND_V(!body_A, RID());
  916. if (!p_body_B.is_valid()) {
  917. ERR_FAIL_COND_V(!body_A->get_space(), RID());
  918. p_body_B = body_A->get_space()->get_static_global_body();
  919. }
  920. BodySW *body_B = body_owner.get(p_body_B);
  921. ERR_FAIL_COND_V(!body_B, RID());
  922. ERR_FAIL_COND_V(body_A == body_B, RID());
  923. JointSW *joint = memnew(Generic6DOFJointSW(body_A, body_B, p_local_frame_A, p_local_frame_B, true));
  924. RID rid = joint_owner.make_rid(joint);
  925. joint->set_self(rid);
  926. return rid;
  927. }
  928. void PhysicsServerSW::generic_6dof_joint_set_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param, real_t p_value) {
  929. JointSW *joint = joint_owner.get(p_joint);
  930. ERR_FAIL_COND(!joint);
  931. ERR_FAIL_COND(joint->get_type() != JOINT_6DOF);
  932. Generic6DOFJointSW *generic_6dof_joint = static_cast<Generic6DOFJointSW *>(joint);
  933. generic_6dof_joint->set_param(p_axis, p_param, p_value);
  934. }
  935. real_t PhysicsServerSW::generic_6dof_joint_get_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param) {
  936. JointSW *joint = joint_owner.get(p_joint);
  937. ERR_FAIL_COND_V(!joint, 0);
  938. ERR_FAIL_COND_V(joint->get_type() != JOINT_6DOF, 0);
  939. Generic6DOFJointSW *generic_6dof_joint = static_cast<Generic6DOFJointSW *>(joint);
  940. return generic_6dof_joint->get_param(p_axis, p_param);
  941. }
  942. void PhysicsServerSW::generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag, bool p_enable) {
  943. JointSW *joint = joint_owner.get(p_joint);
  944. ERR_FAIL_COND(!joint);
  945. ERR_FAIL_COND(joint->get_type() != JOINT_6DOF);
  946. Generic6DOFJointSW *generic_6dof_joint = static_cast<Generic6DOFJointSW *>(joint);
  947. generic_6dof_joint->set_flag(p_axis, p_flag, p_enable);
  948. }
  949. bool PhysicsServerSW::generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag) {
  950. JointSW *joint = joint_owner.get(p_joint);
  951. ERR_FAIL_COND_V(!joint, false);
  952. ERR_FAIL_COND_V(joint->get_type() != JOINT_6DOF, false);
  953. Generic6DOFJointSW *generic_6dof_joint = static_cast<Generic6DOFJointSW *>(joint);
  954. return generic_6dof_joint->get_flag(p_axis, p_flag);
  955. }
  956. void PhysicsServerSW::free(RID p_rid) {
  957. _update_shapes(); //just in case
  958. if (shape_owner.owns(p_rid)) {
  959. ShapeSW *shape = shape_owner.get(p_rid);
  960. while (shape->get_owners().size()) {
  961. ShapeOwnerSW *so = shape->get_owners().front()->key();
  962. so->remove_shape(shape);
  963. }
  964. shape_owner.free(p_rid);
  965. memdelete(shape);
  966. } else if (body_owner.owns(p_rid)) {
  967. BodySW *body = body_owner.get(p_rid);
  968. /*
  969. if (body->get_state_query())
  970. _clear_query(body->get_state_query());
  971. if (body->get_direct_state_query())
  972. _clear_query(body->get_direct_state_query());
  973. */
  974. body->set_space(nullptr);
  975. while (body->get_shape_count()) {
  976. body->remove_shape(0);
  977. }
  978. body_owner.free(p_rid);
  979. memdelete(body);
  980. } else if (area_owner.owns(p_rid)) {
  981. AreaSW *area = area_owner.get(p_rid);
  982. /*
  983. if (area->get_monitor_query())
  984. _clear_query(area->get_monitor_query());
  985. */
  986. area->set_space(nullptr);
  987. while (area->get_shape_count()) {
  988. area->remove_shape(0);
  989. }
  990. area_owner.free(p_rid);
  991. memdelete(area);
  992. } else if (space_owner.owns(p_rid)) {
  993. SpaceSW *space = space_owner.get(p_rid);
  994. while (space->get_objects().size()) {
  995. CollisionObjectSW *co = (CollisionObjectSW *)space->get_objects().front()->get();
  996. co->set_space(nullptr);
  997. }
  998. active_spaces.erase(space);
  999. free(space->get_default_area()->get_self());
  1000. free(space->get_static_global_body());
  1001. space_owner.free(p_rid);
  1002. memdelete(space);
  1003. } else if (joint_owner.owns(p_rid)) {
  1004. JointSW *joint = joint_owner.get(p_rid);
  1005. for (int i = 0; i < joint->get_body_count(); i++) {
  1006. joint->get_body_ptr()[i]->remove_constraint(joint);
  1007. }
  1008. joint_owner.free(p_rid);
  1009. memdelete(joint);
  1010. } else {
  1011. ERR_FAIL_MSG("Invalid ID.");
  1012. }
  1013. };
  1014. void PhysicsServerSW::set_active(bool p_active) {
  1015. active = p_active;
  1016. };
  1017. void PhysicsServerSW::init() {
  1018. last_step = 0.001;
  1019. iterations = 8; // 8?
  1020. stepper = memnew(StepSW);
  1021. direct_state = memnew(PhysicsDirectBodyStateSW);
  1022. };
  1023. void PhysicsServerSW::step(real_t p_step) {
  1024. #ifndef _3D_DISABLED
  1025. if (!active) {
  1026. return;
  1027. }
  1028. _update_shapes();
  1029. last_step = p_step;
  1030. PhysicsDirectBodyStateSW::singleton->step = p_step;
  1031. island_count = 0;
  1032. active_objects = 0;
  1033. collision_pairs = 0;
  1034. for (Set<const SpaceSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  1035. stepper->step((SpaceSW *)E->get(), p_step, iterations);
  1036. island_count += E->get()->get_island_count();
  1037. active_objects += E->get()->get_active_objects();
  1038. collision_pairs += E->get()->get_collision_pairs();
  1039. }
  1040. #endif
  1041. }
  1042. void PhysicsServerSW::flush_queries() {
  1043. #ifndef _3D_DISABLED
  1044. if (!active) {
  1045. return;
  1046. }
  1047. flushing_queries = true;
  1048. uint64_t time_beg = OS::get_singleton()->get_ticks_usec();
  1049. for (Set<const SpaceSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  1050. SpaceSW *space = (SpaceSW *)E->get();
  1051. space->call_queries();
  1052. }
  1053. flushing_queries = false;
  1054. if (ScriptDebugger::get_singleton() && ScriptDebugger::get_singleton()->is_profiling()) {
  1055. uint64_t total_time[SpaceSW::ELAPSED_TIME_MAX];
  1056. static const char *time_name[SpaceSW::ELAPSED_TIME_MAX] = {
  1057. "integrate_forces",
  1058. "generate_islands",
  1059. "setup_constraints",
  1060. "solve_constraints",
  1061. "integrate_velocities"
  1062. };
  1063. for (int i = 0; i < SpaceSW::ELAPSED_TIME_MAX; i++) {
  1064. total_time[i] = 0;
  1065. }
  1066. for (Set<const SpaceSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  1067. for (int i = 0; i < SpaceSW::ELAPSED_TIME_MAX; i++) {
  1068. total_time[i] += E->get()->get_elapsed_time(SpaceSW::ElapsedTime(i));
  1069. }
  1070. }
  1071. Array values;
  1072. values.resize(SpaceSW::ELAPSED_TIME_MAX * 2);
  1073. for (int i = 0; i < SpaceSW::ELAPSED_TIME_MAX; i++) {
  1074. values[i * 2 + 0] = time_name[i];
  1075. values[i * 2 + 1] = USEC_TO_SEC(total_time[i]);
  1076. }
  1077. values.push_back("flush_queries");
  1078. values.push_back(USEC_TO_SEC(OS::get_singleton()->get_ticks_usec() - time_beg));
  1079. ScriptDebugger::get_singleton()->add_profiling_frame_data("physics", values);
  1080. }
  1081. #endif
  1082. };
  1083. void PhysicsServerSW::finish() {
  1084. memdelete(stepper);
  1085. memdelete(direct_state);
  1086. };
  1087. int PhysicsServerSW::get_process_info(ProcessInfo p_info) {
  1088. switch (p_info) {
  1089. case INFO_ACTIVE_OBJECTS: {
  1090. return active_objects;
  1091. } break;
  1092. case INFO_COLLISION_PAIRS: {
  1093. return collision_pairs;
  1094. } break;
  1095. case INFO_ISLAND_COUNT: {
  1096. return island_count;
  1097. } break;
  1098. }
  1099. return 0;
  1100. }
  1101. void PhysicsServerSW::_update_shapes() {
  1102. while (pending_shape_update_list.first()) {
  1103. pending_shape_update_list.first()->self()->_shape_changed();
  1104. pending_shape_update_list.remove(pending_shape_update_list.first());
  1105. }
  1106. }
  1107. void PhysicsServerSW::_shape_col_cbk(const Vector3 &p_point_A, const Vector3 &p_point_B, void *p_userdata) {
  1108. CollCbkData *cbk = (CollCbkData *)p_userdata;
  1109. if (cbk->max == 0) {
  1110. return;
  1111. }
  1112. if (cbk->amount == cbk->max) {
  1113. //find least deep
  1114. real_t min_depth = 1e20;
  1115. int min_depth_idx = 0;
  1116. for (int i = 0; i < cbk->amount; i++) {
  1117. real_t d = cbk->ptr[i * 2 + 0].distance_squared_to(cbk->ptr[i * 2 + 1]);
  1118. if (d < min_depth) {
  1119. min_depth = d;
  1120. min_depth_idx = i;
  1121. }
  1122. }
  1123. real_t d = p_point_A.distance_squared_to(p_point_B);
  1124. if (d < min_depth) {
  1125. return;
  1126. }
  1127. cbk->ptr[min_depth_idx * 2 + 0] = p_point_A;
  1128. cbk->ptr[min_depth_idx * 2 + 1] = p_point_B;
  1129. } else {
  1130. cbk->ptr[cbk->amount * 2 + 0] = p_point_A;
  1131. cbk->ptr[cbk->amount * 2 + 1] = p_point_B;
  1132. cbk->amount++;
  1133. }
  1134. }
  1135. PhysicsServerSW *PhysicsServerSW::singleton = nullptr;
  1136. PhysicsServerSW::PhysicsServerSW() {
  1137. singleton = this;
  1138. bool use_bvh_or_octree = GLOBAL_GET("physics/3d/godot_physics/use_bvh");
  1139. if (use_bvh_or_octree) {
  1140. BroadPhaseSW::create_func = BroadPhaseBVH::_create;
  1141. } else {
  1142. BroadPhaseSW::create_func = BroadPhaseOctree::_create;
  1143. }
  1144. island_count = 0;
  1145. active_objects = 0;
  1146. collision_pairs = 0;
  1147. active = true;
  1148. flushing_queries = false;
  1149. };
  1150. PhysicsServerSW::~PhysicsServerSW(){
  1151. };