physics_server_sw.cpp 42 KB

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