step_2d_sw.cpp 9.0 KB

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