physics_server_sw.cpp 43 KB

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