body_sw.cpp 21 KB

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