step_sw.cpp 8.5 KB

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