physics_server_sw.cpp 43 KB

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