step_2d_sw.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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-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_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 (const List<Pair<Constraint2DSW *, int>>::Element *E = p_body->get_constraint_list().front(); E; E = E->next()) {
  37. Constraint2DSW *c = (Constraint2DSW *)E->get().first;
  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().second) {
  46. continue;
  47. }
  48. Body2DSW *b = c->get_body_ptr()[i];
  49. if (b->get_island_step() == _step || b->get_mode() == PhysicsServer2D::BODY_MODE_STATIC || b->get_mode() == PhysicsServer2D::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() == PhysicsServer2D::BODY_MODE_STATIC || b->get_mode() == PhysicsServer2D::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() == PhysicsServer2D::BODY_MODE_STATIC || b->get_mode() == PhysicsServer2D::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. { //profile
  129. profile_endtime = OS::get_singleton()->get_ticks_usec();
  130. p_space->set_elapsed_time(Space2DSW::ELAPSED_TIME_INTEGRATE_FORCES, profile_endtime - profile_begtime);
  131. profile_begtime = profile_endtime;
  132. }
  133. /* GENERATE CONSTRAINT ISLANDS */
  134. Body2DSW *island_list = nullptr;
  135. Constraint2DSW *constraint_island_list = nullptr;
  136. b = body_list->first();
  137. int island_count = 0;
  138. while (b) {
  139. Body2DSW *body = b->self();
  140. if (body->get_island_step() != _step) {
  141. Body2DSW *island = nullptr;
  142. Constraint2DSW *constraint_island = nullptr;
  143. _populate_island(body, &island, &constraint_island);
  144. island->set_island_list_next(island_list);
  145. island_list = island;
  146. if (constraint_island) {
  147. constraint_island->set_island_list_next(constraint_island_list);
  148. constraint_island_list = constraint_island;
  149. island_count++;
  150. }
  151. }
  152. b = b->next();
  153. }
  154. p_space->set_island_count(island_count);
  155. const SelfList<Area2DSW>::List &aml = p_space->get_moved_area_list();
  156. while (aml.first()) {
  157. for (const Set<Constraint2DSW *>::Element *E = aml.first()->self()->get_constraints().front(); E; E = E->next()) {
  158. Constraint2DSW *c = E->get();
  159. if (c->get_island_step() == _step) {
  160. continue;
  161. }
  162. c->set_island_step(_step);
  163. c->set_island_next(nullptr);
  164. c->set_island_list_next(constraint_island_list);
  165. constraint_island_list = c;
  166. }
  167. p_space->area_remove_from_moved_list((SelfList<Area2DSW> *)aml.first()); //faster to remove here
  168. }
  169. { //profile
  170. profile_endtime = OS::get_singleton()->get_ticks_usec();
  171. p_space->set_elapsed_time(Space2DSW::ELAPSED_TIME_GENERATE_ISLANDS, profile_endtime - profile_begtime);
  172. profile_begtime = profile_endtime;
  173. }
  174. /* SETUP CONSTRAINT ISLANDS */
  175. {
  176. Constraint2DSW *ci = constraint_island_list;
  177. Constraint2DSW *prev_ci = nullptr;
  178. while (ci) {
  179. if (_setup_island(ci, p_delta)) {
  180. //removed the root from the island graph because it is not to be processed
  181. Constraint2DSW *next = ci->get_island_next();
  182. if (next) {
  183. //root from list being deleted no longer exists, replace by next
  184. next->set_island_list_next(ci->get_island_list_next());
  185. if (prev_ci) {
  186. prev_ci->set_island_list_next(next);
  187. } else {
  188. constraint_island_list = next;
  189. }
  190. prev_ci = next;
  191. } else {
  192. //list is empty, just skip
  193. if (prev_ci) {
  194. prev_ci->set_island_list_next(ci->get_island_list_next());
  195. } else {
  196. constraint_island_list = ci->get_island_list_next();
  197. }
  198. }
  199. } else {
  200. prev_ci = ci;
  201. }
  202. ci = ci->get_island_list_next();
  203. }
  204. }
  205. { //profile
  206. profile_endtime = OS::get_singleton()->get_ticks_usec();
  207. p_space->set_elapsed_time(Space2DSW::ELAPSED_TIME_SETUP_CONSTRAINTS, profile_endtime - profile_begtime);
  208. profile_begtime = profile_endtime;
  209. }
  210. /* SOLVE CONSTRAINT ISLANDS */
  211. {
  212. Constraint2DSW *ci = constraint_island_list;
  213. while (ci) {
  214. //iterating each island separatedly improves cache efficiency
  215. _solve_island(ci, p_iterations, p_delta);
  216. ci = ci->get_island_list_next();
  217. }
  218. }
  219. { //profile
  220. profile_endtime = OS::get_singleton()->get_ticks_usec();
  221. p_space->set_elapsed_time(Space2DSW::ELAPSED_TIME_SOLVE_CONSTRAINTS, profile_endtime - profile_begtime);
  222. profile_begtime = profile_endtime;
  223. }
  224. /* INTEGRATE VELOCITIES */
  225. b = body_list->first();
  226. while (b) {
  227. const SelfList<Body2DSW> *n = b->next();
  228. b->self()->integrate_velocities(p_delta);
  229. b = n; // in case it shuts itself down
  230. }
  231. /* SLEEP / WAKE UP ISLANDS */
  232. {
  233. Body2DSW *bi = island_list;
  234. while (bi) {
  235. _check_suspend(bi, p_delta);
  236. bi = bi->get_island_list_next();
  237. }
  238. }
  239. { //profile
  240. profile_endtime = OS::get_singleton()->get_ticks_usec();
  241. p_space->set_elapsed_time(Space2DSW::ELAPSED_TIME_INTEGRATE_VELOCITIES, profile_endtime - profile_begtime);
  242. //profile_begtime=profile_endtime;
  243. }
  244. p_space->update();
  245. p_space->unlock();
  246. _step++;
  247. }
  248. Step2DSW::Step2DSW() {
  249. _step = 1;
  250. }