step_3d_sw.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*************************************************************************/
  2. /* step_3d_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "step_3d_sw.h"
  31. #include "joints_3d_sw.h"
  32. #include "core/os/os.h"
  33. void Step3DSW::_populate_island(Body3DSW *p_body, Body3DSW **p_island, Constraint3DSW **p_constraint_island) {
  34. p_body->set_island_step(_step);
  35. p_body->set_island_next(*p_island);
  36. *p_island = p_body;
  37. for (Map<Constraint3DSW *, int>::Element *E = p_body->get_constraint_map().front(); E; E = E->next()) {
  38. Constraint3DSW *c = (Constraint3DSW *)E->key();
  39. if (c->get_island_step() == _step) {
  40. continue; //already processed
  41. }
  42. c->set_island_step(_step);
  43. c->set_island_next(*p_constraint_island);
  44. *p_constraint_island = c;
  45. for (int i = 0; i < c->get_body_count(); i++) {
  46. if (i == E->get()) {
  47. continue;
  48. }
  49. Body3DSW *b = c->get_body_ptr()[i];
  50. if (b->get_island_step() == _step || b->get_mode() == PhysicsServer3D::BODY_MODE_STATIC || b->get_mode() == PhysicsServer3D::BODY_MODE_KINEMATIC) {
  51. continue; //no go
  52. }
  53. _populate_island(c->get_body_ptr()[i], p_island, p_constraint_island);
  54. }
  55. }
  56. }
  57. void Step3DSW::_setup_island(Constraint3DSW *p_island, real_t p_delta) {
  58. Constraint3DSW *ci = p_island;
  59. while (ci) {
  60. ci->setup(p_delta);
  61. //todo remove from island if process fails
  62. ci = ci->get_island_next();
  63. }
  64. }
  65. void Step3DSW::_solve_island(Constraint3DSW *p_island, int p_iterations, real_t p_delta) {
  66. int at_priority = 1;
  67. while (p_island) {
  68. for (int i = 0; i < p_iterations; i++) {
  69. Constraint3DSW *ci = p_island;
  70. while (ci) {
  71. ci->solve(p_delta);
  72. ci = ci->get_island_next();
  73. }
  74. }
  75. at_priority++;
  76. {
  77. Constraint3DSW *ci = p_island;
  78. Constraint3DSW *prev = nullptr;
  79. while (ci) {
  80. if (ci->get_priority() < at_priority) {
  81. if (prev) {
  82. prev->set_island_next(ci->get_island_next()); //remove
  83. } else {
  84. p_island = ci->get_island_next();
  85. }
  86. } else {
  87. prev = ci;
  88. }
  89. ci = ci->get_island_next();
  90. }
  91. }
  92. }
  93. }
  94. void Step3DSW::_check_suspend(Body3DSW *p_island, real_t p_delta) {
  95. bool can_sleep = true;
  96. Body3DSW *b = p_island;
  97. while (b) {
  98. if (b->get_mode() == PhysicsServer3D::BODY_MODE_STATIC || b->get_mode() == PhysicsServer3D::BODY_MODE_KINEMATIC) {
  99. b = b->get_island_next();
  100. continue; //ignore for static
  101. }
  102. if (!b->sleep_test(p_delta)) {
  103. can_sleep = false;
  104. }
  105. b = b->get_island_next();
  106. }
  107. //put all to sleep or wake up everyoen
  108. b = p_island;
  109. while (b) {
  110. if (b->get_mode() == PhysicsServer3D::BODY_MODE_STATIC || b->get_mode() == PhysicsServer3D::BODY_MODE_KINEMATIC) {
  111. b = b->get_island_next();
  112. continue; //ignore for static
  113. }
  114. bool active = b->is_active();
  115. if (active == can_sleep) {
  116. b->set_active(!can_sleep);
  117. }
  118. b = b->get_island_next();
  119. }
  120. }
  121. void Step3DSW::step(Space3DSW *p_space, real_t p_delta, int p_iterations) {
  122. p_space->lock(); // can't access space during this
  123. p_space->setup(); //update inertias, etc
  124. const SelfList<Body3DSW>::List *body_list = &p_space->get_active_body_list();
  125. /* INTEGRATE FORCES */
  126. uint64_t profile_begtime = OS::get_singleton()->get_ticks_usec();
  127. uint64_t profile_endtime = 0;
  128. int active_count = 0;
  129. const SelfList<Body3DSW> *b = body_list->first();
  130. while (b) {
  131. b->self()->integrate_forces(p_delta);
  132. b = b->next();
  133. active_count++;
  134. }
  135. p_space->set_active_objects(active_count);
  136. { //profile
  137. profile_endtime = OS::get_singleton()->get_ticks_usec();
  138. p_space->set_elapsed_time(Space3DSW::ELAPSED_TIME_INTEGRATE_FORCES, profile_endtime - profile_begtime);
  139. profile_begtime = profile_endtime;
  140. }
  141. /* GENERATE CONSTRAINT ISLANDS */
  142. Body3DSW *island_list = nullptr;
  143. Constraint3DSW *constraint_island_list = nullptr;
  144. b = body_list->first();
  145. int island_count = 0;
  146. while (b) {
  147. Body3DSW *body = b->self();
  148. if (body->get_island_step() != _step) {
  149. Body3DSW *island = nullptr;
  150. Constraint3DSW *constraint_island = nullptr;
  151. _populate_island(body, &island, &constraint_island);
  152. island->set_island_list_next(island_list);
  153. island_list = island;
  154. if (constraint_island) {
  155. constraint_island->set_island_list_next(constraint_island_list);
  156. constraint_island_list = constraint_island;
  157. island_count++;
  158. }
  159. }
  160. b = b->next();
  161. }
  162. p_space->set_island_count(island_count);
  163. const SelfList<Area3DSW>::List &aml = p_space->get_moved_area_list();
  164. while (aml.first()) {
  165. for (const Set<Constraint3DSW *>::Element *E = aml.first()->self()->get_constraints().front(); E; E = E->next()) {
  166. Constraint3DSW *c = E->get();
  167. if (c->get_island_step() == _step) {
  168. continue;
  169. }
  170. c->set_island_step(_step);
  171. c->set_island_next(nullptr);
  172. c->set_island_list_next(constraint_island_list);
  173. constraint_island_list = c;
  174. }
  175. p_space->area_remove_from_moved_list((SelfList<Area3DSW> *)aml.first()); //faster to remove here
  176. }
  177. { //profile
  178. profile_endtime = OS::get_singleton()->get_ticks_usec();
  179. p_space->set_elapsed_time(Space3DSW::ELAPSED_TIME_GENERATE_ISLANDS, profile_endtime - profile_begtime);
  180. profile_begtime = profile_endtime;
  181. }
  182. /* SETUP CONSTRAINT ISLANDS */
  183. {
  184. Constraint3DSW *ci = constraint_island_list;
  185. while (ci) {
  186. _setup_island(ci, p_delta);
  187. ci = ci->get_island_list_next();
  188. }
  189. }
  190. { //profile
  191. profile_endtime = OS::get_singleton()->get_ticks_usec();
  192. p_space->set_elapsed_time(Space3DSW::ELAPSED_TIME_SETUP_CONSTRAINTS, profile_endtime - profile_begtime);
  193. profile_begtime = profile_endtime;
  194. }
  195. /* SOLVE CONSTRAINT ISLANDS */
  196. {
  197. Constraint3DSW *ci = constraint_island_list;
  198. while (ci) {
  199. //iterating each island separatedly improves cache efficiency
  200. _solve_island(ci, p_iterations, p_delta);
  201. ci = ci->get_island_list_next();
  202. }
  203. }
  204. { //profile
  205. profile_endtime = OS::get_singleton()->get_ticks_usec();
  206. p_space->set_elapsed_time(Space3DSW::ELAPSED_TIME_SOLVE_CONSTRAINTS, profile_endtime - profile_begtime);
  207. profile_begtime = profile_endtime;
  208. }
  209. /* INTEGRATE VELOCITIES */
  210. b = body_list->first();
  211. while (b) {
  212. const SelfList<Body3DSW> *n = b->next();
  213. b->self()->integrate_velocities(p_delta);
  214. b = n;
  215. }
  216. /* SLEEP / WAKE UP ISLANDS */
  217. {
  218. Body3DSW *bi = island_list;
  219. while (bi) {
  220. _check_suspend(bi, p_delta);
  221. bi = bi->get_island_list_next();
  222. }
  223. }
  224. { //profile
  225. profile_endtime = OS::get_singleton()->get_ticks_usec();
  226. p_space->set_elapsed_time(Space3DSW::ELAPSED_TIME_INTEGRATE_VELOCITIES, profile_endtime - profile_begtime);
  227. profile_begtime = profile_endtime;
  228. }
  229. p_space->update();
  230. p_space->unlock();
  231. _step++;
  232. }
  233. Step3DSW::Step3DSW() {
  234. _step = 1;
  235. }