step_2d_sw.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*************************************************************************/
  2. /* step_2d_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_2d_sw.h"
  31. #include "core/os/os.h"
  32. void Step2DSW::_populate_island(Body2DSW *p_body, Body2DSW **p_island, Constraint2DSW **p_constraint_island) {
  33. p_body->set_island_step(_step);
  34. p_body->set_island_next(*p_island);
  35. *p_island = p_body;
  36. for (Map<Constraint2DSW *, int>::Element *E = p_body->get_constraint_map().front(); E; E = E->next()) {
  37. Constraint2DSW *c = (Constraint2DSW *)E->key();
  38. if (c->get_island_step() == _step) {
  39. continue; //already processed
  40. }
  41. c->set_island_step(_step);
  42. c->set_island_next(*p_constraint_island);
  43. *p_constraint_island = c;
  44. for (int i = 0; i < c->get_body_count(); i++) {
  45. if (i == E->get()) {
  46. continue;
  47. }
  48. Body2DSW *b = c->get_body_ptr()[i];
  49. if (b->get_island_step() == _step || b->get_mode() == Physics2DServer::BODY_MODE_STATIC || b->get_mode() == Physics2DServer::BODY_MODE_KINEMATIC) {
  50. continue; //no go
  51. }
  52. _populate_island(c->get_body_ptr()[i], p_island, p_constraint_island);
  53. }
  54. }
  55. }
  56. bool Step2DSW::_setup_island(Constraint2DSW *p_island, real_t p_delta) {
  57. Constraint2DSW *ci = p_island;
  58. Constraint2DSW *prev_ci = nullptr;
  59. bool removed_root = false;
  60. while (ci) {
  61. bool process = ci->setup(p_delta);
  62. if (!process) {
  63. //remove from island if process fails
  64. if (prev_ci) {
  65. prev_ci->set_island_next(ci->get_island_next());
  66. } else {
  67. removed_root = true;
  68. prev_ci = ci;
  69. }
  70. } else {
  71. prev_ci = ci;
  72. }
  73. ci = ci->get_island_next();
  74. }
  75. return removed_root;
  76. }
  77. void Step2DSW::_solve_island(Constraint2DSW *p_island, int p_iterations, real_t p_delta) {
  78. for (int i = 0; i < p_iterations; i++) {
  79. Constraint2DSW *ci = p_island;
  80. while (ci) {
  81. ci->solve(p_delta);
  82. ci = ci->get_island_next();
  83. }
  84. }
  85. }
  86. void Step2DSW::_check_suspend(Body2DSW *p_island, real_t p_delta) {
  87. bool can_sleep = true;
  88. Body2DSW *b = p_island;
  89. while (b) {
  90. if (b->get_mode() == Physics2DServer::BODY_MODE_STATIC || b->get_mode() == Physics2DServer::BODY_MODE_KINEMATIC) {
  91. b = b->get_island_next();
  92. continue; //ignore for static
  93. }
  94. if (!b->sleep_test(p_delta)) {
  95. can_sleep = false;
  96. }
  97. b = b->get_island_next();
  98. }
  99. //put all to sleep or wake up everyoen
  100. b = p_island;
  101. while (b) {
  102. if (b->get_mode() == Physics2DServer::BODY_MODE_STATIC || b->get_mode() == Physics2DServer::BODY_MODE_KINEMATIC) {
  103. b = b->get_island_next();
  104. continue; //ignore for static
  105. }
  106. bool active = b->is_active();
  107. if (active == can_sleep) {
  108. b->set_active(!can_sleep);
  109. }
  110. b = b->get_island_next();
  111. }
  112. }
  113. void Step2DSW::step(Space2DSW *p_space, real_t p_delta, int p_iterations) {
  114. p_space->lock(); // can't access space during this
  115. p_space->setup(); //update inertias, etc
  116. const SelfList<Body2DSW>::List *body_list = &p_space->get_active_body_list();
  117. /* INTEGRATE FORCES */
  118. uint64_t profile_begtime = OS::get_singleton()->get_ticks_usec();
  119. uint64_t profile_endtime = 0;
  120. int active_count = 0;
  121. const SelfList<Body2DSW> *b = body_list->first();
  122. while (b) {
  123. b->self()->integrate_forces(p_delta);
  124. b = b->next();
  125. active_count++;
  126. }
  127. p_space->set_active_objects(active_count);
  128. // Update the broadphase to register collision pairs.
  129. p_space->update();
  130. { //profile
  131. profile_endtime = OS::get_singleton()->get_ticks_usec();
  132. p_space->set_elapsed_time(Space2DSW::ELAPSED_TIME_INTEGRATE_FORCES, profile_endtime - profile_begtime);
  133. profile_begtime = profile_endtime;
  134. }
  135. /* GENERATE CONSTRAINT ISLANDS */
  136. Body2DSW *island_list = nullptr;
  137. Constraint2DSW *constraint_island_list = nullptr;
  138. b = body_list->first();
  139. int island_count = 0;
  140. while (b) {
  141. Body2DSW *body = b->self();
  142. if (body->get_island_step() != _step) {
  143. Body2DSW *island = nullptr;
  144. Constraint2DSW *constraint_island = nullptr;
  145. _populate_island(body, &island, &constraint_island);
  146. island->set_island_list_next(island_list);
  147. island_list = island;
  148. if (constraint_island) {
  149. constraint_island->set_island_list_next(constraint_island_list);
  150. constraint_island_list = constraint_island;
  151. island_count++;
  152. }
  153. }
  154. b = b->next();
  155. }
  156. p_space->set_island_count(island_count);
  157. const SelfList<Area2DSW>::List &aml = p_space->get_moved_area_list();
  158. while (aml.first()) {
  159. for (const Set<Constraint2DSW *>::Element *E = aml.first()->self()->get_constraints().front(); E; E = E->next()) {
  160. Constraint2DSW *c = E->get();
  161. if (c->get_island_step() == _step) {
  162. continue;
  163. }
  164. c->set_island_step(_step);
  165. c->set_island_next(nullptr);
  166. c->set_island_list_next(constraint_island_list);
  167. constraint_island_list = c;
  168. }
  169. p_space->area_remove_from_moved_list((SelfList<Area2DSW> *)aml.first()); //faster to remove here
  170. }
  171. { //profile
  172. profile_endtime = OS::get_singleton()->get_ticks_usec();
  173. p_space->set_elapsed_time(Space2DSW::ELAPSED_TIME_GENERATE_ISLANDS, profile_endtime - profile_begtime);
  174. profile_begtime = profile_endtime;
  175. }
  176. /* SETUP CONSTRAINT ISLANDS */
  177. {
  178. Constraint2DSW *ci = constraint_island_list;
  179. Constraint2DSW *prev_ci = nullptr;
  180. while (ci) {
  181. if (_setup_island(ci, p_delta)) {
  182. //removed the root from the island graph because it is not to be processed
  183. Constraint2DSW *next = ci->get_island_next();
  184. if (next) {
  185. //root from list being deleted no longer exists, replace by next
  186. next->set_island_list_next(ci->get_island_list_next());
  187. if (prev_ci) {
  188. prev_ci->set_island_list_next(next);
  189. } else {
  190. constraint_island_list = next;
  191. }
  192. prev_ci = next;
  193. } else {
  194. //list is empty, just skip
  195. if (prev_ci) {
  196. prev_ci->set_island_list_next(ci->get_island_list_next());
  197. } else {
  198. constraint_island_list = ci->get_island_list_next();
  199. }
  200. }
  201. } else {
  202. prev_ci = ci;
  203. }
  204. ci = ci->get_island_list_next();
  205. }
  206. }
  207. { //profile
  208. profile_endtime = OS::get_singleton()->get_ticks_usec();
  209. p_space->set_elapsed_time(Space2DSW::ELAPSED_TIME_SETUP_CONSTRAINTS, profile_endtime - profile_begtime);
  210. profile_begtime = profile_endtime;
  211. }
  212. /* SOLVE CONSTRAINT ISLANDS */
  213. {
  214. Constraint2DSW *ci = constraint_island_list;
  215. while (ci) {
  216. //iterating each island separatedly improves cache efficiency
  217. _solve_island(ci, p_iterations, p_delta);
  218. ci = ci->get_island_list_next();
  219. }
  220. }
  221. { //profile
  222. profile_endtime = OS::get_singleton()->get_ticks_usec();
  223. p_space->set_elapsed_time(Space2DSW::ELAPSED_TIME_SOLVE_CONSTRAINTS, profile_endtime - profile_begtime);
  224. profile_begtime = profile_endtime;
  225. }
  226. /* INTEGRATE VELOCITIES */
  227. b = body_list->first();
  228. while (b) {
  229. const SelfList<Body2DSW> *n = b->next();
  230. b->self()->integrate_velocities(p_delta);
  231. b = n; // in case it shuts itself down
  232. }
  233. /* SLEEP / WAKE UP ISLANDS */
  234. {
  235. Body2DSW *bi = island_list;
  236. while (bi) {
  237. _check_suspend(bi, p_delta);
  238. bi = bi->get_island_list_next();
  239. }
  240. }
  241. { //profile
  242. profile_endtime = OS::get_singleton()->get_ticks_usec();
  243. p_space->set_elapsed_time(Space2DSW::ELAPSED_TIME_INTEGRATE_VELOCITIES, profile_endtime - profile_begtime);
  244. //profile_begtime=profile_endtime;
  245. }
  246. p_space->unlock();
  247. _step++;
  248. }
  249. Step2DSW::Step2DSW() {
  250. _step = 1;
  251. }