step_sw.cpp 8.3 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-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_sw.h"
  31. #include "joints_sw.h"
  32. #include "core/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. /* SETUP CONSTRAINT ISLANDS */
  177. {
  178. ConstraintSW *ci = constraint_island_list;
  179. while (ci) {
  180. _setup_island(ci, p_delta);
  181. ci = ci->get_island_list_next();
  182. }
  183. }
  184. { //profile
  185. profile_endtime = OS::get_singleton()->get_ticks_usec();
  186. p_space->set_elapsed_time(SpaceSW::ELAPSED_TIME_SETUP_CONSTRAINTS, profile_endtime - profile_begtime);
  187. profile_begtime = profile_endtime;
  188. }
  189. /* SOLVE CONSTRAINT ISLANDS */
  190. {
  191. ConstraintSW *ci = constraint_island_list;
  192. while (ci) {
  193. //iterating each island separatedly improves cache efficiency
  194. _solve_island(ci, p_iterations, p_delta);
  195. ci = ci->get_island_list_next();
  196. }
  197. }
  198. { //profile
  199. profile_endtime = OS::get_singleton()->get_ticks_usec();
  200. p_space->set_elapsed_time(SpaceSW::ELAPSED_TIME_SOLVE_CONSTRAINTS, profile_endtime - profile_begtime);
  201. profile_begtime = profile_endtime;
  202. }
  203. /* INTEGRATE VELOCITIES */
  204. b = body_list->first();
  205. while (b) {
  206. const SelfList<BodySW> *n = b->next();
  207. b->self()->integrate_velocities(p_delta);
  208. b = n;
  209. }
  210. /* SLEEP / WAKE UP ISLANDS */
  211. {
  212. BodySW *bi = island_list;
  213. while (bi) {
  214. _check_suspend(bi, p_delta);
  215. bi = bi->get_island_list_next();
  216. }
  217. }
  218. { //profile
  219. profile_endtime = OS::get_singleton()->get_ticks_usec();
  220. p_space->set_elapsed_time(SpaceSW::ELAPSED_TIME_INTEGRATE_VELOCITIES, profile_endtime - profile_begtime);
  221. profile_begtime = profile_endtime;
  222. }
  223. p_space->update();
  224. p_space->unlock();
  225. _step++;
  226. }
  227. StepSW::StepSW() {
  228. _step = 1;
  229. }