body_sw.cpp 21 KB

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