step_sw.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*************************************************************************/
  2. /* step_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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_sw.h"
  31. #include "joints_sw.h"
  32. #include "os/os.h"
  33. void StepSW::_populate_island(BodySW *p_body, BodySW **p_island, ConstraintSW **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<ConstraintSW *, int>::Element *E = p_body->get_constraint_map().front(); E; E = E->next()) {
  38. ConstraintSW *c = (ConstraintSW *)E->key();
  39. if (c->get_island_step() == _step)
  40. continue; //already processed
  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. BodySW *b = c->get_body_ptr()[i];
  48. if (b->get_island_step() == _step || b->get_mode() == PhysicsServer::BODY_MODE_STATIC || b->get_mode() == PhysicsServer::BODY_MODE_KINEMATIC)
  49. continue; //no go
  50. _populate_island(c->get_body_ptr()[i], p_island, p_constraint_island);
  51. }
  52. }
  53. }
  54. void StepSW::_setup_island(ConstraintSW *p_island, real_t p_delta) {
  55. ConstraintSW *ci = p_island;
  56. while (ci) {
  57. ci->setup(p_delta);
  58. //todo remove from island if process fails
  59. ci = ci->get_island_next();
  60. }
  61. }
  62. void StepSW::_solve_island(ConstraintSW *p_island, int p_iterations, real_t p_delta) {
  63. int at_priority = 1;
  64. while (p_island) {
  65. for (int i = 0; i < p_iterations; i++) {
  66. ConstraintSW *ci = p_island;
  67. while (ci) {
  68. ci->solve(p_delta);
  69. ci = ci->get_island_next();
  70. }
  71. }
  72. at_priority++;
  73. {
  74. ConstraintSW *ci = p_island;
  75. ConstraintSW *prev = NULL;
  76. while (ci) {
  77. if (ci->get_priority() < at_priority) {
  78. if (prev) {
  79. prev->set_island_next(ci->get_island_next()); //remove
  80. } else {
  81. p_island = ci->get_island_next();
  82. }
  83. } else {
  84. prev = ci;
  85. }
  86. ci = ci->get_island_next();
  87. }
  88. }
  89. }
  90. }
  91. void StepSW::_check_suspend(BodySW *p_island, real_t p_delta) {
  92. bool can_sleep = true;
  93. BodySW *b = p_island;
  94. while (b) {
  95. if (b->get_mode() == PhysicsServer::BODY_MODE_STATIC || b->get_mode() == PhysicsServer::BODY_MODE_KINEMATIC) {
  96. b = b->get_island_next();
  97. continue; //ignore for static
  98. }
  99. if (!b->sleep_test(p_delta))
  100. can_sleep = false;
  101. b = b->get_island_next();
  102. }
  103. //put all to sleep or wake up everyoen
  104. b = p_island;
  105. while (b) {
  106. if (b->get_mode() == PhysicsServer::BODY_MODE_STATIC || b->get_mode() == PhysicsServer::BODY_MODE_KINEMATIC) {
  107. b = b->get_island_next();
  108. continue; //ignore for static
  109. }
  110. bool active = b->is_active();
  111. if (active == can_sleep)
  112. b->set_active(!can_sleep);
  113. b = b->get_island_next();
  114. }
  115. }
  116. void StepSW::step(SpaceSW *p_space, real_t p_delta, int p_iterations) {
  117. p_space->lock(); // can't access space during this
  118. p_space->setup(); //update inertias, etc
  119. const SelfList<BodySW>::List *body_list = &p_space->get_active_body_list();
  120. /* INTEGRATE FORCES */
  121. uint64_t profile_begtime = OS::get_singleton()->get_ticks_usec();
  122. uint64_t profile_endtime = 0;
  123. int active_count = 0;
  124. const SelfList<BodySW> *b = body_list->first();
  125. while (b) {
  126. b->self()->integrate_forces(p_delta);
  127. b = b->next();
  128. active_count++;
  129. }
  130. p_space->set_active_objects(active_count);
  131. { //profile
  132. profile_endtime = OS::get_singleton()->get_ticks_usec();
  133. p_space->set_elapsed_time(SpaceSW::ELAPSED_TIME_INTEGRATE_FORCES, profile_endtime - profile_begtime);
  134. profile_begtime = profile_endtime;
  135. }
  136. /* GENERATE CONSTRAINT ISLANDS */
  137. BodySW *island_list = NULL;
  138. ConstraintSW *constraint_island_list = NULL;
  139. b = body_list->first();
  140. int island_count = 0;
  141. while (b) {
  142. BodySW *body = b->self();
  143. if (body->get_island_step() != _step) {
  144. BodySW *island = NULL;
  145. ConstraintSW *constraint_island = NULL;
  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<AreaSW>::List &aml = p_space->get_moved_area_list();
  159. while (aml.first()) {
  160. for (const Set<ConstraintSW *>::Element *E = aml.first()->self()->get_constraints().front(); E; E = E->next()) {
  161. ConstraintSW *c = E->get();
  162. if (c->get_island_step() == _step)
  163. continue;
  164. c->set_island_step(_step);
  165. c->set_island_next(NULL);
  166. c->set_island_list_next(constraint_island_list);
  167. constraint_island_list = c;
  168. }
  169. p_space->area_remove_from_moved_list((SelfList<AreaSW> *)aml.first()); //faster to remove here
  170. }
  171. { //profile
  172. profile_endtime = OS::get_singleton()->get_ticks_usec();
  173. p_space->set_elapsed_time(SpaceSW::ELAPSED_TIME_GENERATE_ISLANDS, profile_endtime - profile_begtime);
  174. profile_begtime = profile_endtime;
  175. }
  176. //print_line("island count: "+itos(island_count)+" active count: "+itos(active_count));
  177. /* SETUP CONSTRAINT ISLANDS */
  178. {
  179. ConstraintSW *ci = constraint_island_list;
  180. while (ci) {
  181. _setup_island(ci, p_delta);
  182. ci = ci->get_island_list_next();
  183. }
  184. }
  185. { //profile
  186. profile_endtime = OS::get_singleton()->get_ticks_usec();
  187. p_space->set_elapsed_time(SpaceSW::ELAPSED_TIME_SETUP_CONSTRAINTS, profile_endtime - profile_begtime);
  188. profile_begtime = profile_endtime;
  189. }
  190. /* SOLVE CONSTRAINT ISLANDS */
  191. {
  192. ConstraintSW *ci = constraint_island_list;
  193. while (ci) {
  194. //iterating each island separatedly improves cache efficiency
  195. _solve_island(ci, p_iterations, p_delta);
  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(SpaceSW::ELAPSED_TIME_SOLVE_CONSTRAINTS, profile_endtime - profile_begtime);
  202. profile_begtime = profile_endtime;
  203. }
  204. /* INTEGRATE VELOCITIES */
  205. b = body_list->first();
  206. while (b) {
  207. const SelfList<BodySW> *n = b->next();
  208. b->self()->integrate_velocities(p_delta);
  209. b = n;
  210. }
  211. /* SLEEP / WAKE UP ISLANDS */
  212. {
  213. BodySW *bi = island_list;
  214. while (bi) {
  215. _check_suspend(bi, p_delta);
  216. bi = bi->get_island_list_next();
  217. }
  218. }
  219. { //profile
  220. profile_endtime = OS::get_singleton()->get_ticks_usec();
  221. p_space->set_elapsed_time(SpaceSW::ELAPSED_TIME_INTEGRATE_VELOCITIES, profile_endtime - profile_begtime);
  222. profile_begtime = profile_endtime;
  223. }
  224. p_space->update();
  225. p_space->unlock();
  226. _step++;
  227. }
  228. StepSW::StepSW() {
  229. _step = 1;
  230. }