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