body_sw.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /**************************************************************************/
  2. /* body_sw.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "body_sw.h"
  31. #include "area_sw.h"
  32. #include "space_sw.h"
  33. void BodySW::_update_inertia() {
  34. if (get_space() && !inertia_update_list.in_list()) {
  35. get_space()->body_add_to_inertia_update_list(&inertia_update_list);
  36. }
  37. }
  38. void BodySW::_update_transform_dependant() {
  39. center_of_mass = get_transform().basis.xform(center_of_mass_local);
  40. principal_inertia_axes = get_transform().basis * principal_inertia_axes_local;
  41. // update inertia tensor
  42. Basis tb = principal_inertia_axes;
  43. Basis tbt = tb.transposed();
  44. Basis diag;
  45. diag.scale(_inv_inertia);
  46. _inv_inertia_tensor = tb * diag * tbt;
  47. }
  48. void BodySW::update_inertias() {
  49. // Update shapes and motions.
  50. switch (mode) {
  51. case PhysicsServer::BODY_MODE_RIGID: {
  52. // Update tensor for all shapes, not the best way but should be somehow OK. (inspired from bullet)
  53. real_t total_area = 0;
  54. for (int i = 0; i < get_shape_count(); i++) {
  55. if (is_shape_disabled(i)) {
  56. continue;
  57. }
  58. total_area += get_shape_area(i);
  59. }
  60. // We have to recompute the center of mass.
  61. center_of_mass_local.zero();
  62. if (total_area != 0.0) {
  63. for (int i = 0; i < get_shape_count(); i++) {
  64. if (is_shape_disabled(i)) {
  65. continue;
  66. }
  67. real_t area = get_shape_area(i);
  68. real_t mass = area * this->mass / total_area;
  69. // NOTE: we assume that the shape origin is also its center of mass.
  70. center_of_mass_local += mass * get_shape_transform(i).origin;
  71. }
  72. center_of_mass_local /= mass;
  73. }
  74. // Recompute the inertia tensor.
  75. Basis inertia_tensor;
  76. inertia_tensor.set_zero();
  77. bool inertia_set = false;
  78. for (int i = 0; i < get_shape_count(); i++) {
  79. if (is_shape_disabled(i)) {
  80. continue;
  81. }
  82. real_t area = get_shape_area(i);
  83. if (area == 0.0) {
  84. continue;
  85. }
  86. inertia_set = true;
  87. const ShapeSW *shape = get_shape(i);
  88. real_t mass = area * this->mass / total_area;
  89. Basis shape_inertia_tensor = shape->get_moment_of_inertia(mass).to_diagonal_matrix();
  90. Transform shape_transform = get_shape_transform(i);
  91. Basis shape_basis = shape_transform.basis.orthonormalized();
  92. // NOTE: we don't take the scale of collision shapes into account when computing the inertia tensor!
  93. shape_inertia_tensor = shape_basis * shape_inertia_tensor * shape_basis.transposed();
  94. Vector3 shape_origin = shape_transform.origin - center_of_mass_local;
  95. inertia_tensor += shape_inertia_tensor + (Basis() * shape_origin.dot(shape_origin) - shape_origin.outer(shape_origin)) * mass;
  96. }
  97. // Set the inertia to a valid value when there are no valid shapes.
  98. if (!inertia_set) {
  99. inertia_tensor.set_diagonal(Vector3(1.0, 1.0, 1.0));
  100. }
  101. // Compute the principal axes of inertia.
  102. principal_inertia_axes_local = inertia_tensor.diagonalize().transposed();
  103. _inv_inertia = inertia_tensor.get_main_diagonal().inverse();
  104. if (mass) {
  105. _inv_mass = 1.0 / mass;
  106. } else {
  107. _inv_mass = 0;
  108. }
  109. } break;
  110. case PhysicsServer::BODY_MODE_KINEMATIC:
  111. case PhysicsServer::BODY_MODE_STATIC: {
  112. _inv_inertia_tensor.set_zero();
  113. _inv_mass = 0;
  114. } break;
  115. case PhysicsServer::BODY_MODE_CHARACTER: {
  116. _inv_inertia_tensor.set_zero();
  117. _inv_mass = 1.0 / mass;
  118. } break;
  119. }
  120. //_update_shapes();
  121. _update_transform_dependant();
  122. }
  123. void BodySW::set_active(bool p_active) {
  124. if (active == p_active) {
  125. return;
  126. }
  127. active = p_active;
  128. if (!p_active) {
  129. if (get_space()) {
  130. get_space()->body_remove_from_active_list(&active_list);
  131. }
  132. } else {
  133. if (mode == PhysicsServer::BODY_MODE_STATIC) {
  134. return; //static bodies can't become active
  135. }
  136. if (get_space()) {
  137. get_space()->body_add_to_active_list(&active_list);
  138. }
  139. //still_time=0;
  140. }
  141. /*
  142. if (!space)
  143. return;
  144. for(int i=0;i<get_shape_count();i++) {
  145. Shape &s=shapes[i];
  146. if (s.bpid>0) {
  147. get_space()->get_broadphase()->set_active(s.bpid,active);
  148. }
  149. }
  150. */
  151. }
  152. void BodySW::set_param(PhysicsServer::BodyParameter p_param, real_t p_value) {
  153. switch (p_param) {
  154. case PhysicsServer::BODY_PARAM_BOUNCE: {
  155. bounce = p_value;
  156. } break;
  157. case PhysicsServer::BODY_PARAM_FRICTION: {
  158. friction = p_value;
  159. } break;
  160. case PhysicsServer::BODY_PARAM_MASS: {
  161. ERR_FAIL_COND(p_value <= 0);
  162. mass = p_value;
  163. _update_inertia();
  164. } break;
  165. case PhysicsServer::BODY_PARAM_GRAVITY_SCALE: {
  166. gravity_scale = p_value;
  167. } break;
  168. case PhysicsServer::BODY_PARAM_LINEAR_DAMP: {
  169. linear_damp = p_value;
  170. } break;
  171. case PhysicsServer::BODY_PARAM_ANGULAR_DAMP: {
  172. angular_damp = p_value;
  173. } break;
  174. default: {
  175. }
  176. }
  177. }
  178. real_t BodySW::get_param(PhysicsServer::BodyParameter p_param) const {
  179. switch (p_param) {
  180. case PhysicsServer::BODY_PARAM_BOUNCE: {
  181. return bounce;
  182. } break;
  183. case PhysicsServer::BODY_PARAM_FRICTION: {
  184. return friction;
  185. } break;
  186. case PhysicsServer::BODY_PARAM_MASS: {
  187. return mass;
  188. } break;
  189. case PhysicsServer::BODY_PARAM_GRAVITY_SCALE: {
  190. return gravity_scale;
  191. } break;
  192. case PhysicsServer::BODY_PARAM_LINEAR_DAMP: {
  193. return linear_damp;
  194. } break;
  195. case PhysicsServer::BODY_PARAM_ANGULAR_DAMP: {
  196. return angular_damp;
  197. } break;
  198. default: {
  199. }
  200. }
  201. return 0;
  202. }
  203. void BodySW::set_mode(PhysicsServer::BodyMode p_mode) {
  204. PhysicsServer::BodyMode prev = mode;
  205. mode = p_mode;
  206. switch (p_mode) {
  207. //CLEAR UP EVERYTHING IN CASE IT NOT WORKS!
  208. case PhysicsServer::BODY_MODE_STATIC:
  209. case PhysicsServer::BODY_MODE_KINEMATIC: {
  210. _set_inv_transform(get_transform().affine_inverse());
  211. _inv_mass = 0;
  212. _set_static(p_mode == PhysicsServer::BODY_MODE_STATIC);
  213. //set_active(p_mode==PhysicsServer::BODY_MODE_KINEMATIC);
  214. set_active(p_mode == PhysicsServer::BODY_MODE_KINEMATIC && contacts.size());
  215. linear_velocity = Vector3();
  216. angular_velocity = Vector3();
  217. if (mode == PhysicsServer::BODY_MODE_KINEMATIC && prev != mode) {
  218. first_time_kinematic = true;
  219. }
  220. } break;
  221. case PhysicsServer::BODY_MODE_RIGID: {
  222. _inv_mass = mass > 0 ? (1.0 / mass) : 0;
  223. _set_static(false);
  224. set_active(true);
  225. } break;
  226. case PhysicsServer::BODY_MODE_CHARACTER: {
  227. _inv_mass = mass > 0 ? (1.0 / mass) : 0;
  228. _set_static(false);
  229. set_active(true);
  230. angular_velocity = Vector3();
  231. } break;
  232. }
  233. _update_inertia();
  234. /*
  235. if (get_space())
  236. _update_queries();
  237. */
  238. }
  239. PhysicsServer::BodyMode BodySW::get_mode() const {
  240. return mode;
  241. }
  242. void BodySW::_shapes_changed() {
  243. _update_inertia();
  244. wakeup();
  245. wakeup_neighbours();
  246. }
  247. void BodySW::set_state(PhysicsServer::BodyState p_state, const Variant &p_variant) {
  248. switch (p_state) {
  249. case PhysicsServer::BODY_STATE_TRANSFORM: {
  250. if (mode == PhysicsServer::BODY_MODE_KINEMATIC) {
  251. new_transform = p_variant;
  252. //wakeup_neighbours();
  253. set_active(true);
  254. if (first_time_kinematic) {
  255. _set_transform(p_variant);
  256. _set_inv_transform(get_transform().affine_inverse());
  257. first_time_kinematic = false;
  258. }
  259. } else if (mode == PhysicsServer::BODY_MODE_STATIC) {
  260. _set_transform(p_variant);
  261. _set_inv_transform(get_transform().affine_inverse());
  262. wakeup_neighbours();
  263. } else {
  264. Transform t = p_variant;
  265. t.orthonormalize();
  266. new_transform = get_transform(); //used as old to compute motion
  267. if (new_transform == t) {
  268. break;
  269. }
  270. _set_transform(t);
  271. _set_inv_transform(get_transform().inverse());
  272. }
  273. wakeup();
  274. } break;
  275. case PhysicsServer::BODY_STATE_LINEAR_VELOCITY: {
  276. /*
  277. if (mode==PhysicsServer::BODY_MODE_STATIC)
  278. break;
  279. */
  280. linear_velocity = p_variant;
  281. wakeup();
  282. } break;
  283. case PhysicsServer::BODY_STATE_ANGULAR_VELOCITY: {
  284. /*
  285. if (mode!=PhysicsServer::BODY_MODE_RIGID)
  286. break;
  287. */
  288. angular_velocity = p_variant;
  289. wakeup();
  290. } break;
  291. case PhysicsServer::BODY_STATE_SLEEPING: {
  292. //?
  293. if (mode == PhysicsServer::BODY_MODE_STATIC || mode == PhysicsServer::BODY_MODE_KINEMATIC) {
  294. break;
  295. }
  296. bool do_sleep = p_variant;
  297. if (do_sleep) {
  298. linear_velocity = Vector3();
  299. //biased_linear_velocity=Vector3();
  300. angular_velocity = Vector3();
  301. //biased_angular_velocity=Vector3();
  302. set_active(false);
  303. } else {
  304. set_active(true);
  305. }
  306. } break;
  307. case PhysicsServer::BODY_STATE_CAN_SLEEP: {
  308. can_sleep = p_variant;
  309. if (mode == PhysicsServer::BODY_MODE_RIGID && !active && !can_sleep) {
  310. set_active(true);
  311. }
  312. } break;
  313. }
  314. }
  315. Variant BodySW::get_state(PhysicsServer::BodyState p_state) const {
  316. switch (p_state) {
  317. case PhysicsServer::BODY_STATE_TRANSFORM: {
  318. return get_transform();
  319. } break;
  320. case PhysicsServer::BODY_STATE_LINEAR_VELOCITY: {
  321. return linear_velocity;
  322. } break;
  323. case PhysicsServer::BODY_STATE_ANGULAR_VELOCITY: {
  324. return angular_velocity;
  325. } break;
  326. case PhysicsServer::BODY_STATE_SLEEPING: {
  327. return !is_active();
  328. } break;
  329. case PhysicsServer::BODY_STATE_CAN_SLEEP: {
  330. return can_sleep;
  331. } break;
  332. }
  333. return Variant();
  334. }
  335. void BodySW::set_space(SpaceSW *p_space) {
  336. if (get_space()) {
  337. if (inertia_update_list.in_list()) {
  338. get_space()->body_remove_from_inertia_update_list(&inertia_update_list);
  339. }
  340. if (active_list.in_list()) {
  341. get_space()->body_remove_from_active_list(&active_list);
  342. }
  343. if (direct_state_query_list.in_list()) {
  344. get_space()->body_remove_from_state_query_list(&direct_state_query_list);
  345. }
  346. }
  347. _set_space(p_space);
  348. if (get_space()) {
  349. _update_inertia();
  350. if (active) {
  351. get_space()->body_add_to_active_list(&active_list);
  352. }
  353. /*
  354. _update_queries();
  355. if (is_active()) {
  356. active=false;
  357. set_active(true);
  358. }
  359. */
  360. }
  361. first_integration = true;
  362. }
  363. void BodySW::_compute_area_gravity_and_dampenings(const AreaSW *p_area) {
  364. if (p_area->is_gravity_point()) {
  365. if (p_area->get_gravity_distance_scale() > 0) {
  366. Vector3 v = p_area->get_transform().xform(p_area->get_gravity_vector()) - get_transform().get_origin();
  367. gravity += v.normalized() * (p_area->get_gravity() / Math::pow(v.length() * p_area->get_gravity_distance_scale() + 1, 2));
  368. } else {
  369. gravity += (p_area->get_transform().xform(p_area->get_gravity_vector()) - get_transform().get_origin()).normalized() * p_area->get_gravity();
  370. }
  371. } else {
  372. gravity += p_area->get_gravity_vector() * p_area->get_gravity();
  373. }
  374. area_linear_damp += p_area->get_linear_damp();
  375. area_angular_damp += p_area->get_angular_damp();
  376. }
  377. void BodySW::set_axis_lock(PhysicsServer::BodyAxis p_axis, bool lock) {
  378. if (lock) {
  379. locked_axis |= p_axis;
  380. } else {
  381. locked_axis &= ~p_axis;
  382. }
  383. }
  384. bool BodySW::is_axis_locked(PhysicsServer::BodyAxis p_axis) const {
  385. return locked_axis & p_axis;
  386. }
  387. void BodySW::integrate_forces(real_t p_step) {
  388. if (mode == PhysicsServer::BODY_MODE_STATIC) {
  389. return;
  390. }
  391. AreaSW *def_area = get_space()->get_default_area();
  392. // AreaSW *damp_area = def_area;
  393. ERR_FAIL_COND(!def_area);
  394. int ac = areas.size();
  395. bool stopped = false;
  396. gravity = Vector3(0, 0, 0);
  397. area_linear_damp = 0;
  398. area_angular_damp = 0;
  399. if (ac) {
  400. areas.sort();
  401. const AreaCMP *aa = &areas[0];
  402. // damp_area = aa[ac-1].area;
  403. for (int i = ac - 1; i >= 0 && !stopped; i--) {
  404. PhysicsServer::AreaSpaceOverrideMode mode = aa[i].area->get_space_override_mode();
  405. switch (mode) {
  406. case PhysicsServer::AREA_SPACE_OVERRIDE_COMBINE:
  407. case PhysicsServer::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
  408. _compute_area_gravity_and_dampenings(aa[i].area);
  409. stopped = mode == PhysicsServer::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
  410. } break;
  411. case PhysicsServer::AREA_SPACE_OVERRIDE_REPLACE:
  412. case PhysicsServer::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
  413. gravity = Vector3(0, 0, 0);
  414. area_angular_damp = 0;
  415. area_linear_damp = 0;
  416. _compute_area_gravity_and_dampenings(aa[i].area);
  417. stopped = mode == PhysicsServer::AREA_SPACE_OVERRIDE_REPLACE;
  418. } break;
  419. default: {
  420. }
  421. }
  422. }
  423. }
  424. if (!stopped) {
  425. _compute_area_gravity_and_dampenings(def_area);
  426. }
  427. gravity *= gravity_scale;
  428. // If less than 0, override dampenings with that of the Body
  429. if (angular_damp >= 0) {
  430. area_angular_damp = angular_damp;
  431. }
  432. /*
  433. else
  434. area_angular_damp=damp_area->get_angular_damp();
  435. */
  436. if (linear_damp >= 0) {
  437. area_linear_damp = linear_damp;
  438. }
  439. /*
  440. else
  441. area_linear_damp=damp_area->get_linear_damp();
  442. */
  443. prev_linear_velocity = linear_velocity;
  444. prev_angular_velocity = angular_velocity;
  445. Vector3 motion;
  446. bool do_motion = false;
  447. if (mode == PhysicsServer::BODY_MODE_KINEMATIC) {
  448. //compute motion, angular and etc. velocities from prev transform
  449. motion = new_transform.origin - get_transform().origin;
  450. do_motion = true;
  451. linear_velocity = motion / p_step;
  452. //compute a FAKE angular velocity, not so easy
  453. Basis rot = new_transform.basis.orthonormalized() * get_transform().basis.orthonormalized().transposed();
  454. Vector3 axis;
  455. real_t angle;
  456. rot.get_axis_angle(axis, angle);
  457. axis.normalize();
  458. angular_velocity = axis * (angle / p_step);
  459. } else {
  460. if (!omit_force_integration && !first_integration) {
  461. //overridden by direct state query
  462. Vector3 force = gravity * mass;
  463. force += applied_force;
  464. Vector3 torque = applied_torque;
  465. real_t damp = 1.0 - p_step * area_linear_damp;
  466. if (damp < 0) { // reached zero in the given time
  467. damp = 0;
  468. }
  469. real_t angular_damp = 1.0 - p_step * area_angular_damp;
  470. if (angular_damp < 0) { // reached zero in the given time
  471. angular_damp = 0;
  472. }
  473. linear_velocity *= damp;
  474. angular_velocity *= angular_damp;
  475. linear_velocity += _inv_mass * force * p_step;
  476. angular_velocity += _inv_inertia_tensor.xform(torque) * p_step;
  477. }
  478. if (continuous_cd) {
  479. motion = linear_velocity * p_step;
  480. do_motion = true;
  481. }
  482. }
  483. applied_force = Vector3();
  484. applied_torque = Vector3();
  485. first_integration = false;
  486. //motion=linear_velocity*p_step;
  487. biased_angular_velocity = Vector3();
  488. biased_linear_velocity = Vector3();
  489. if (do_motion) { //shapes temporarily extend for raycast
  490. _update_shapes_with_motion(motion);
  491. }
  492. def_area = nullptr; // clear the area, so it is set in the next frame
  493. contact_count = 0;
  494. }
  495. void BodySW::integrate_velocities(real_t p_step) {
  496. if (mode == PhysicsServer::BODY_MODE_STATIC) {
  497. return;
  498. }
  499. if (fi_callback) {
  500. get_space()->body_add_to_state_query_list(&direct_state_query_list);
  501. }
  502. //apply axis lock linear
  503. for (int i = 0; i < 3; i++) {
  504. if (is_axis_locked((PhysicsServer::BodyAxis)(1 << i))) {
  505. linear_velocity[i] = 0;
  506. biased_linear_velocity[i] = 0;
  507. new_transform.origin[i] = get_transform().origin[i];
  508. }
  509. }
  510. //apply axis lock angular
  511. for (int i = 0; i < 3; i++) {
  512. if (is_axis_locked((PhysicsServer::BodyAxis)(1 << (i + 3)))) {
  513. angular_velocity[i] = 0;
  514. biased_angular_velocity[i] = 0;
  515. }
  516. }
  517. if (mode == PhysicsServer::BODY_MODE_KINEMATIC) {
  518. _set_transform(new_transform, false);
  519. _set_inv_transform(new_transform.affine_inverse());
  520. if (contacts.size() == 0 && linear_velocity == Vector3() && angular_velocity == Vector3()) {
  521. set_active(false); //stopped moving, deactivate
  522. }
  523. return;
  524. }
  525. Vector3 total_angular_velocity = angular_velocity + biased_angular_velocity;
  526. real_t ang_vel = total_angular_velocity.length();
  527. Transform transform = get_transform();
  528. if (!Math::is_zero_approx(ang_vel)) {
  529. Vector3 ang_vel_axis = total_angular_velocity / ang_vel;
  530. Basis rot(ang_vel_axis, ang_vel * p_step);
  531. Basis identity3(1, 0, 0, 0, 1, 0, 0, 0, 1);
  532. transform.origin += ((identity3 - rot) * transform.basis).xform(center_of_mass_local);
  533. transform.basis = rot * transform.basis;
  534. transform.orthonormalize();
  535. }
  536. Vector3 total_linear_velocity = linear_velocity + biased_linear_velocity;
  537. /*for(int i=0;i<3;i++) {
  538. if (axis_lock&(1<<i)) {
  539. transform.origin[i]=0.0;
  540. }
  541. }*/
  542. transform.origin += total_linear_velocity * p_step;
  543. _set_transform(transform);
  544. _set_inv_transform(get_transform().inverse());
  545. _update_transform_dependant();
  546. /*
  547. if (fi_callback) {
  548. get_space()->body_add_to_state_query_list(&direct_state_query_list);
  549. */
  550. }
  551. /*
  552. void BodySW::simulate_motion(const Transform& p_xform,real_t p_step) {
  553. Transform inv_xform = p_xform.affine_inverse();
  554. if (!get_space()) {
  555. _set_transform(p_xform);
  556. _set_inv_transform(inv_xform);
  557. return;
  558. }
  559. //compute a FAKE linear velocity - this is easy
  560. linear_velocity=(p_xform.origin - get_transform().origin)/p_step;
  561. //compute a FAKE angular velocity, not so easy
  562. Basis rot=get_transform().basis.orthonormalized().transposed() * p_xform.basis.orthonormalized();
  563. Vector3 axis;
  564. real_t angle;
  565. rot.get_axis_angle(axis,angle);
  566. axis.normalize();
  567. angular_velocity=axis.normalized() * (angle/p_step);
  568. linear_velocity = (p_xform.origin - get_transform().origin)/p_step;
  569. if (!direct_state_query_list.in_list())// - callalways, so lv and av are cleared && (state_query || direct_state_query))
  570. get_space()->body_add_to_state_query_list(&direct_state_query_list);
  571. simulated_motion=true;
  572. _set_transform(p_xform);
  573. }
  574. */
  575. void BodySW::wakeup_neighbours() {
  576. for (Map<ConstraintSW *, int>::Element *E = constraint_map.front(); E; E = E->next()) {
  577. const ConstraintSW *c = E->key();
  578. BodySW **n = c->get_body_ptr();
  579. int bc = c->get_body_count();
  580. for (int i = 0; i < bc; i++) {
  581. if (i == E->get()) {
  582. continue;
  583. }
  584. BodySW *b = n[i];
  585. if (b->mode != PhysicsServer::BODY_MODE_RIGID) {
  586. continue;
  587. }
  588. if (!b->is_active()) {
  589. b->set_active(true);
  590. }
  591. }
  592. }
  593. }
  594. void BodySW::call_queries() {
  595. if (fi_callback) {
  596. Variant v = direct_access;
  597. Object *obj = ObjectDB::get_instance(fi_callback->id);
  598. if (!obj) {
  599. set_force_integration_callback(0, StringName());
  600. } else {
  601. const Variant *vp[2] = { &v, &fi_callback->udata };
  602. Variant::CallError ce;
  603. int argc = (fi_callback->udata.get_type() == Variant::NIL) ? 1 : 2;
  604. obj->call(fi_callback->method, vp, argc, ce);
  605. }
  606. }
  607. }
  608. bool BodySW::sleep_test(real_t p_step) {
  609. if (mode == PhysicsServer::BODY_MODE_STATIC || mode == PhysicsServer::BODY_MODE_KINEMATIC) {
  610. return true; //
  611. } else if (mode == PhysicsServer::BODY_MODE_CHARACTER) {
  612. return !active; // characters don't sleep unless asked to sleep
  613. } else if (!can_sleep) {
  614. return false;
  615. }
  616. if (Math::abs(angular_velocity.length()) < get_space()->get_body_angular_velocity_sleep_threshold() && Math::abs(linear_velocity.length_squared()) < get_space()->get_body_linear_velocity_sleep_threshold() * get_space()->get_body_linear_velocity_sleep_threshold()) {
  617. still_time += p_step;
  618. return still_time > get_space()->get_body_time_to_sleep();
  619. } else {
  620. still_time = 0; //maybe this should be set to 0 on set_active?
  621. return false;
  622. }
  623. }
  624. void BodySW::set_force_integration_callback(ObjectID p_id, const StringName &p_method, const Variant &p_udata) {
  625. if (fi_callback) {
  626. memdelete(fi_callback);
  627. fi_callback = nullptr;
  628. }
  629. if (p_id != 0) {
  630. fi_callback = memnew(ForceIntegrationCallback);
  631. fi_callback->id = p_id;
  632. fi_callback->method = p_method;
  633. fi_callback->udata = p_udata;
  634. }
  635. }
  636. void BodySW::set_kinematic_margin(real_t p_margin) {
  637. kinematic_safe_margin = p_margin;
  638. }
  639. BodySW::BodySW() :
  640. CollisionObjectSW(TYPE_BODY),
  641. locked_axis(0),
  642. active_list(this),
  643. inertia_update_list(this),
  644. direct_state_query_list(this) {
  645. mode = PhysicsServer::BODY_MODE_RIGID;
  646. active = true;
  647. mass = 1;
  648. kinematic_safe_margin = 0.001;
  649. //_inv_inertia=Transform();
  650. _inv_mass = 1;
  651. bounce = 0;
  652. friction = 1;
  653. omit_force_integration = false;
  654. //applied_torque=0;
  655. island_step = 0;
  656. island_next = nullptr;
  657. island_list_next = nullptr;
  658. first_time_kinematic = false;
  659. first_integration = false;
  660. _set_static(false);
  661. contact_count = 0;
  662. gravity_scale = 1.0;
  663. linear_damp = -1;
  664. angular_damp = -1;
  665. area_angular_damp = 0;
  666. area_linear_damp = 0;
  667. still_time = 0;
  668. continuous_cd = false;
  669. can_sleep = true;
  670. fi_callback = nullptr;
  671. direct_access = memnew(PhysicsDirectBodyStateSW);
  672. direct_access->body = this;
  673. }
  674. BodySW::~BodySW() {
  675. memdelete(direct_access);
  676. if (fi_callback) {
  677. memdelete(fi_callback);
  678. }
  679. }
  680. PhysicsDirectSpaceState *PhysicsDirectBodyStateSW::get_space_state() {
  681. return body->get_space()->get_direct_state();
  682. }
  683. real_t PhysicsDirectBodyStateSW::get_step() const {
  684. return body->get_space()->get_step();
  685. }