physics_server_3d_sw.cpp 46 KB

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