body_sw.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. }
  155. real_t BodySW::get_param(PhysicsServer::BodyParameter p_param) const {
  156. switch (p_param) {
  157. case PhysicsServer::BODY_PARAM_BOUNCE: {
  158. return bounce;
  159. } break;
  160. case PhysicsServer::BODY_PARAM_FRICTION: {
  161. return friction;
  162. } break;
  163. case PhysicsServer::BODY_PARAM_MASS: {
  164. return mass;
  165. } break;
  166. case PhysicsServer::BODY_PARAM_GRAVITY_SCALE: {
  167. return gravity_scale;
  168. } break;
  169. case PhysicsServer::BODY_PARAM_LINEAR_DAMP: {
  170. return linear_damp;
  171. } break;
  172. case PhysicsServer::BODY_PARAM_ANGULAR_DAMP: {
  173. return angular_damp;
  174. } break;
  175. default: {
  176. }
  177. }
  178. return 0;
  179. }
  180. void BodySW::set_mode(PhysicsServer::BodyMode p_mode) {
  181. PhysicsServer::BodyMode prev = mode;
  182. mode = p_mode;
  183. switch (p_mode) {
  184. //CLEAR UP EVERYTHING IN CASE IT NOT WORKS!
  185. case PhysicsServer::BODY_MODE_STATIC:
  186. case PhysicsServer::BODY_MODE_KINEMATIC: {
  187. _set_inv_transform(get_transform().affine_inverse());
  188. _inv_mass = 0;
  189. _set_static(p_mode == PhysicsServer::BODY_MODE_STATIC);
  190. //set_active(p_mode==PhysicsServer::BODY_MODE_KINEMATIC);
  191. set_active(p_mode == PhysicsServer::BODY_MODE_KINEMATIC && contacts.size());
  192. linear_velocity = Vector3();
  193. angular_velocity = Vector3();
  194. if (mode == PhysicsServer::BODY_MODE_KINEMATIC && prev != mode) {
  195. first_time_kinematic = true;
  196. }
  197. } break;
  198. case PhysicsServer::BODY_MODE_RIGID: {
  199. _inv_mass = mass > 0 ? (1.0 / mass) : 0;
  200. _set_static(false);
  201. } break;
  202. case PhysicsServer::BODY_MODE_CHARACTER: {
  203. _inv_mass = mass > 0 ? (1.0 / mass) : 0;
  204. _set_static(false);
  205. } break;
  206. }
  207. _update_inertia();
  208. /*
  209. if (get_space())
  210. _update_queries();
  211. */
  212. }
  213. PhysicsServer::BodyMode BodySW::get_mode() const {
  214. return mode;
  215. }
  216. void BodySW::_shapes_changed() {
  217. _update_inertia();
  218. }
  219. void BodySW::set_state(PhysicsServer::BodyState p_state, const Variant &p_variant) {
  220. switch (p_state) {
  221. case PhysicsServer::BODY_STATE_TRANSFORM: {
  222. if (mode == PhysicsServer::BODY_MODE_KINEMATIC) {
  223. new_transform = p_variant;
  224. //wakeup_neighbours();
  225. set_active(true);
  226. if (first_time_kinematic) {
  227. _set_transform(p_variant);
  228. _set_inv_transform(get_transform().affine_inverse());
  229. first_time_kinematic = false;
  230. }
  231. } else if (mode == PhysicsServer::BODY_MODE_STATIC) {
  232. _set_transform(p_variant);
  233. _set_inv_transform(get_transform().affine_inverse());
  234. wakeup_neighbours();
  235. } else {
  236. Transform t = p_variant;
  237. t.orthonormalize();
  238. new_transform = get_transform(); //used as old to compute motion
  239. if (new_transform == t)
  240. break;
  241. _set_transform(t);
  242. _set_inv_transform(get_transform().inverse());
  243. }
  244. wakeup();
  245. } break;
  246. case PhysicsServer::BODY_STATE_LINEAR_VELOCITY: {
  247. /*
  248. if (mode==PhysicsServer::BODY_MODE_STATIC)
  249. break;
  250. */
  251. linear_velocity = p_variant;
  252. wakeup();
  253. } break;
  254. case PhysicsServer::BODY_STATE_ANGULAR_VELOCITY: {
  255. /*
  256. if (mode!=PhysicsServer::BODY_MODE_RIGID)
  257. break;
  258. */
  259. angular_velocity = p_variant;
  260. wakeup();
  261. } break;
  262. case PhysicsServer::BODY_STATE_SLEEPING: {
  263. //?
  264. if (mode == PhysicsServer::BODY_MODE_STATIC || mode == PhysicsServer::BODY_MODE_KINEMATIC)
  265. break;
  266. bool do_sleep = p_variant;
  267. if (do_sleep) {
  268. linear_velocity = Vector3();
  269. //biased_linear_velocity=Vector3();
  270. angular_velocity = Vector3();
  271. //biased_angular_velocity=Vector3();
  272. set_active(false);
  273. } else {
  274. if (mode != PhysicsServer::BODY_MODE_STATIC)
  275. set_active(true);
  276. }
  277. } break;
  278. case PhysicsServer::BODY_STATE_CAN_SLEEP: {
  279. can_sleep = p_variant;
  280. if (mode == PhysicsServer::BODY_MODE_RIGID && !active && !can_sleep)
  281. set_active(true);
  282. } break;
  283. }
  284. }
  285. Variant BodySW::get_state(PhysicsServer::BodyState p_state) const {
  286. switch (p_state) {
  287. case PhysicsServer::BODY_STATE_TRANSFORM: {
  288. return get_transform();
  289. } break;
  290. case PhysicsServer::BODY_STATE_LINEAR_VELOCITY: {
  291. return linear_velocity;
  292. } break;
  293. case PhysicsServer::BODY_STATE_ANGULAR_VELOCITY: {
  294. return angular_velocity;
  295. } break;
  296. case PhysicsServer::BODY_STATE_SLEEPING: {
  297. return !is_active();
  298. } break;
  299. case PhysicsServer::BODY_STATE_CAN_SLEEP: {
  300. return can_sleep;
  301. } break;
  302. }
  303. return Variant();
  304. }
  305. void BodySW::set_space(SpaceSW *p_space) {
  306. if (get_space()) {
  307. if (inertia_update_list.in_list())
  308. get_space()->body_remove_from_inertia_update_list(&inertia_update_list);
  309. if (active_list.in_list())
  310. get_space()->body_remove_from_active_list(&active_list);
  311. if (direct_state_query_list.in_list())
  312. get_space()->body_remove_from_state_query_list(&direct_state_query_list);
  313. }
  314. _set_space(p_space);
  315. if (get_space()) {
  316. _update_inertia();
  317. if (active)
  318. get_space()->body_add_to_active_list(&active_list);
  319. /*
  320. _update_queries();
  321. if (is_active()) {
  322. active=false;
  323. set_active(true);
  324. }
  325. */
  326. }
  327. first_integration = true;
  328. }
  329. void BodySW::_compute_area_gravity_and_dampenings(const AreaSW *p_area) {
  330. if (p_area->is_gravity_point()) {
  331. if (p_area->get_gravity_distance_scale() > 0) {
  332. Vector3 v = p_area->get_transform().xform(p_area->get_gravity_vector()) - get_transform().get_origin();
  333. gravity += v.normalized() * (p_area->get_gravity() / Math::pow(v.length() * p_area->get_gravity_distance_scale() + 1, 2));
  334. } else {
  335. gravity += (p_area->get_transform().xform(p_area->get_gravity_vector()) - get_transform().get_origin()).normalized() * p_area->get_gravity();
  336. }
  337. } else {
  338. gravity += p_area->get_gravity_vector() * p_area->get_gravity();
  339. }
  340. area_linear_damp += p_area->get_linear_damp();
  341. area_angular_damp += p_area->get_angular_damp();
  342. }
  343. void BodySW::set_axis_lock(PhysicsServer::BodyAxis p_axis, bool lock) {
  344. if (lock) {
  345. locked_axis |= p_axis;
  346. } else {
  347. locked_axis &= ~p_axis;
  348. }
  349. }
  350. bool BodySW::is_axis_locked(PhysicsServer::BodyAxis p_axis) const {
  351. return locked_axis & p_axis;
  352. }
  353. void BodySW::integrate_forces(real_t p_step) {
  354. if (mode == PhysicsServer::BODY_MODE_STATIC)
  355. return;
  356. AreaSW *def_area = get_space()->get_default_area();
  357. // AreaSW *damp_area = def_area;
  358. ERR_FAIL_COND(!def_area);
  359. int ac = areas.size();
  360. bool stopped = false;
  361. gravity = Vector3(0, 0, 0);
  362. area_linear_damp = 0;
  363. area_angular_damp = 0;
  364. if (ac) {
  365. areas.sort();
  366. const AreaCMP *aa = &areas[0];
  367. // damp_area = aa[ac-1].area;
  368. for (int i = ac - 1; i >= 0 && !stopped; i--) {
  369. PhysicsServer::AreaSpaceOverrideMode mode = aa[i].area->get_space_override_mode();
  370. switch (mode) {
  371. case PhysicsServer::AREA_SPACE_OVERRIDE_COMBINE:
  372. case PhysicsServer::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
  373. _compute_area_gravity_and_dampenings(aa[i].area);
  374. stopped = mode == PhysicsServer::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
  375. } break;
  376. case PhysicsServer::AREA_SPACE_OVERRIDE_REPLACE:
  377. case PhysicsServer::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
  378. gravity = Vector3(0, 0, 0);
  379. area_angular_damp = 0;
  380. area_linear_damp = 0;
  381. _compute_area_gravity_and_dampenings(aa[i].area);
  382. stopped = mode == PhysicsServer::AREA_SPACE_OVERRIDE_REPLACE;
  383. } break;
  384. default: {
  385. }
  386. }
  387. }
  388. }
  389. if (!stopped) {
  390. _compute_area_gravity_and_dampenings(def_area);
  391. }
  392. gravity *= gravity_scale;
  393. // If less than 0, override dampenings with that of the Body
  394. if (angular_damp >= 0)
  395. area_angular_damp = angular_damp;
  396. /*
  397. else
  398. area_angular_damp=damp_area->get_angular_damp();
  399. */
  400. if (linear_damp >= 0)
  401. area_linear_damp = linear_damp;
  402. /*
  403. else
  404. area_linear_damp=damp_area->get_linear_damp();
  405. */
  406. Vector3 motion;
  407. bool do_motion = false;
  408. if (mode == PhysicsServer::BODY_MODE_KINEMATIC) {
  409. //compute motion, angular and etc. velocities from prev transform
  410. linear_velocity = (new_transform.origin - get_transform().origin) / p_step;
  411. //compute a FAKE angular velocity, not so easy
  412. Basis rot = new_transform.basis.orthonormalized().transposed() * get_transform().basis.orthonormalized();
  413. Vector3 axis;
  414. real_t angle;
  415. rot.get_axis_angle(axis, angle);
  416. axis.normalize();
  417. angular_velocity = axis.normalized() * (angle / p_step);
  418. motion = new_transform.origin - get_transform().origin;
  419. do_motion = true;
  420. } else {
  421. if (!omit_force_integration && !first_integration) {
  422. //overridden by direct state query
  423. Vector3 force = gravity * mass;
  424. force += applied_force;
  425. Vector3 torque = applied_torque;
  426. real_t damp = 1.0 - p_step * area_linear_damp;
  427. if (damp < 0) // reached zero in the given time
  428. damp = 0;
  429. real_t angular_damp = 1.0 - p_step * area_angular_damp;
  430. if (angular_damp < 0) // reached zero in the given time
  431. angular_damp = 0;
  432. linear_velocity *= damp;
  433. angular_velocity *= angular_damp;
  434. linear_velocity += _inv_mass * force * p_step;
  435. angular_velocity += _inv_inertia_tensor.xform(torque) * p_step;
  436. }
  437. if (continuous_cd) {
  438. motion = linear_velocity * p_step;
  439. do_motion = true;
  440. }
  441. }
  442. applied_force = Vector3();
  443. applied_torque = Vector3();
  444. first_integration = false;
  445. //motion=linear_velocity*p_step;
  446. biased_angular_velocity = Vector3();
  447. biased_linear_velocity = Vector3();
  448. if (do_motion) { //shapes temporarily extend for raycast
  449. _update_shapes_with_motion(motion);
  450. }
  451. def_area = NULL; // clear the area, so it is set in the next frame
  452. contact_count = 0;
  453. }
  454. void BodySW::integrate_velocities(real_t p_step) {
  455. if (mode == PhysicsServer::BODY_MODE_STATIC)
  456. return;
  457. if (fi_callback)
  458. get_space()->body_add_to_state_query_list(&direct_state_query_list);
  459. //apply axis lock linear
  460. for (int i = 0; i < 3; i++) {
  461. if (is_axis_locked((PhysicsServer::BodyAxis)(1 << i))) {
  462. linear_velocity[i] = 0;
  463. biased_linear_velocity[i] = 0;
  464. new_transform.origin[i] = get_transform().origin[i];
  465. }
  466. }
  467. //apply axis lock angular
  468. for (int i = 0; i < 3; i++) {
  469. if (is_axis_locked((PhysicsServer::BodyAxis)(1 << (i + 3)))) {
  470. angular_velocity[i] = 0;
  471. biased_angular_velocity[i] = 0;
  472. }
  473. }
  474. if (mode == PhysicsServer::BODY_MODE_KINEMATIC) {
  475. _set_transform(new_transform, false);
  476. _set_inv_transform(new_transform.affine_inverse());
  477. if (contacts.size() == 0 && linear_velocity == Vector3() && angular_velocity == Vector3())
  478. set_active(false); //stopped moving, deactivate
  479. return;
  480. }
  481. Vector3 total_angular_velocity = angular_velocity + biased_angular_velocity;
  482. real_t ang_vel = total_angular_velocity.length();
  483. Transform transform = get_transform();
  484. if (ang_vel != 0.0) {
  485. Vector3 ang_vel_axis = total_angular_velocity / ang_vel;
  486. Basis rot(ang_vel_axis, ang_vel * p_step);
  487. Basis identity3(1, 0, 0, 0, 1, 0, 0, 0, 1);
  488. transform.origin += ((identity3 - rot) * transform.basis).xform(center_of_mass_local);
  489. transform.basis = rot * transform.basis;
  490. transform.orthonormalize();
  491. }
  492. Vector3 total_linear_velocity = linear_velocity + biased_linear_velocity;
  493. /*for(int i=0;i<3;i++) {
  494. if (axis_lock&(1<<i)) {
  495. transform.origin[i]=0.0;
  496. }
  497. }*/
  498. transform.origin += total_linear_velocity * p_step;
  499. _set_transform(transform);
  500. _set_inv_transform(get_transform().inverse());
  501. _update_transform_dependant();
  502. /*
  503. if (fi_callback) {
  504. get_space()->body_add_to_state_query_list(&direct_state_query_list);
  505. */
  506. }
  507. /*
  508. void BodySW::simulate_motion(const Transform& p_xform,real_t p_step) {
  509. Transform inv_xform = p_xform.affine_inverse();
  510. if (!get_space()) {
  511. _set_transform(p_xform);
  512. _set_inv_transform(inv_xform);
  513. return;
  514. }
  515. //compute a FAKE linear velocity - this is easy
  516. linear_velocity=(p_xform.origin - get_transform().origin)/p_step;
  517. //compute a FAKE angular velocity, not so easy
  518. Basis rot=get_transform().basis.orthonormalized().transposed() * p_xform.basis.orthonormalized();
  519. Vector3 axis;
  520. real_t angle;
  521. rot.get_axis_angle(axis,angle);
  522. axis.normalize();
  523. angular_velocity=axis.normalized() * (angle/p_step);
  524. linear_velocity = (p_xform.origin - get_transform().origin)/p_step;
  525. if (!direct_state_query_list.in_list())// - callalways, so lv and av are cleared && (state_query || direct_state_query))
  526. get_space()->body_add_to_state_query_list(&direct_state_query_list);
  527. simulated_motion=true;
  528. _set_transform(p_xform);
  529. }
  530. */
  531. void BodySW::wakeup_neighbours() {
  532. for (Map<ConstraintSW *, int>::Element *E = constraint_map.front(); E; E = E->next()) {
  533. const ConstraintSW *c = E->key();
  534. BodySW **n = c->get_body_ptr();
  535. int bc = c->get_body_count();
  536. for (int i = 0; i < bc; i++) {
  537. if (i == E->get())
  538. continue;
  539. BodySW *b = n[i];
  540. if (b->mode != PhysicsServer::BODY_MODE_RIGID)
  541. continue;
  542. if (!b->is_active())
  543. b->set_active(true);
  544. }
  545. }
  546. }
  547. void BodySW::call_queries() {
  548. if (fi_callback) {
  549. PhysicsDirectBodyStateSW *dbs = PhysicsDirectBodyStateSW::singleton;
  550. dbs->body = this;
  551. Variant v = dbs;
  552. Object *obj = ObjectDB::get_instance(fi_callback->id);
  553. if (!obj) {
  554. set_force_integration_callback(0, StringName());
  555. } else {
  556. const Variant *vp[2] = { &v, &fi_callback->udata };
  557. Variant::CallError ce;
  558. int argc = (fi_callback->udata.get_type() == Variant::NIL) ? 1 : 2;
  559. obj->call(fi_callback->method, vp, argc, ce);
  560. }
  561. }
  562. }
  563. bool BodySW::sleep_test(real_t p_step) {
  564. if (mode == PhysicsServer::BODY_MODE_STATIC || mode == PhysicsServer::BODY_MODE_KINEMATIC)
  565. return true; //
  566. else if (mode == PhysicsServer::BODY_MODE_CHARACTER)
  567. return !active; // characters don't sleep unless asked to sleep
  568. else if (!can_sleep)
  569. return false;
  570. 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()) {
  571. still_time += p_step;
  572. return still_time > get_space()->get_body_time_to_sleep();
  573. } else {
  574. still_time = 0; //maybe this should be set to 0 on set_active?
  575. return false;
  576. }
  577. }
  578. void BodySW::set_force_integration_callback(ObjectID p_id, const StringName &p_method, const Variant &p_udata) {
  579. if (fi_callback) {
  580. memdelete(fi_callback);
  581. fi_callback = NULL;
  582. }
  583. if (p_id != 0) {
  584. fi_callback = memnew(ForceIntegrationCallback);
  585. fi_callback->id = p_id;
  586. fi_callback->method = p_method;
  587. fi_callback->udata = p_udata;
  588. }
  589. }
  590. void BodySW::set_kinematic_margin(real_t p_margin) {
  591. kinematic_safe_margin = p_margin;
  592. }
  593. BodySW::BodySW() :
  594. CollisionObjectSW(TYPE_BODY),
  595. locked_axis(0),
  596. active_list(this),
  597. inertia_update_list(this),
  598. direct_state_query_list(this) {
  599. mode = PhysicsServer::BODY_MODE_RIGID;
  600. active = true;
  601. mass = 1;
  602. kinematic_safe_margin = 0.01;
  603. //_inv_inertia=Transform();
  604. _inv_mass = 1;
  605. bounce = 0;
  606. friction = 1;
  607. omit_force_integration = false;
  608. //applied_torque=0;
  609. island_step = 0;
  610. island_next = NULL;
  611. island_list_next = NULL;
  612. first_time_kinematic = false;
  613. first_integration = false;
  614. _set_static(false);
  615. contact_count = 0;
  616. gravity_scale = 1.0;
  617. linear_damp = -1;
  618. angular_damp = -1;
  619. area_angular_damp = 0;
  620. area_linear_damp = 0;
  621. still_time = 0;
  622. continuous_cd = false;
  623. can_sleep = false;
  624. fi_callback = NULL;
  625. }
  626. BodySW::~BodySW() {
  627. if (fi_callback)
  628. memdelete(fi_callback);
  629. }
  630. PhysicsDirectBodyStateSW *PhysicsDirectBodyStateSW::singleton = NULL;
  631. PhysicsDirectSpaceState *PhysicsDirectBodyStateSW::get_space_state() {
  632. return body->get_space()->get_direct_state();
  633. }