step_sw.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*************************************************************************/
  2. /* step_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. }
  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->setup(); //update inertias, etc
  124. const SelfList<BodySW>::List *body_list = &p_space->get_active_body_list();
  125. /* INTEGRATE FORCES */
  126. uint64_t profile_begtime = OS::get_singleton()->get_ticks_usec();
  127. uint64_t profile_endtime = 0;
  128. int active_count = 0;
  129. const SelfList<BodySW> *b = body_list->first();
  130. while (b) {
  131. b->self()->integrate_forces(p_delta);
  132. b = b->next();
  133. active_count++;
  134. }
  135. p_space->set_active_objects(active_count);
  136. // Update the broadphase to register collision pairs.
  137. p_space->update();
  138. { //profile
  139. profile_endtime = OS::get_singleton()->get_ticks_usec();
  140. p_space->set_elapsed_time(SpaceSW::ELAPSED_TIME_INTEGRATE_FORCES, profile_endtime - profile_begtime);
  141. profile_begtime = profile_endtime;
  142. }
  143. /* GENERATE CONSTRAINT ISLANDS */
  144. BodySW *island_list = nullptr;
  145. ConstraintSW *constraint_island_list = nullptr;
  146. b = body_list->first();
  147. int island_count = 0;
  148. while (b) {
  149. BodySW *body = b->self();
  150. if (body->get_island_step() != _step) {
  151. BodySW *island = nullptr;
  152. ConstraintSW *constraint_island = nullptr;
  153. _populate_island(body, &island, &constraint_island);
  154. island->set_island_list_next(island_list);
  155. island_list = island;
  156. if (constraint_island) {
  157. constraint_island->set_island_list_next(constraint_island_list);
  158. constraint_island_list = constraint_island;
  159. island_count++;
  160. }
  161. }
  162. b = b->next();
  163. }
  164. p_space->set_island_count(island_count);
  165. const SelfList<AreaSW>::List &aml = p_space->get_moved_area_list();
  166. while (aml.first()) {
  167. for (const Set<ConstraintSW *>::Element *E = aml.first()->self()->get_constraints().front(); E; E = E->next()) {
  168. ConstraintSW *c = E->get();
  169. if (c->get_island_step() == _step) {
  170. continue;
  171. }
  172. c->set_island_step(_step);
  173. c->set_island_next(nullptr);
  174. c->set_island_list_next(constraint_island_list);
  175. constraint_island_list = c;
  176. }
  177. p_space->area_remove_from_moved_list((SelfList<AreaSW> *)aml.first()); //faster to remove here
  178. }
  179. { //profile
  180. profile_endtime = OS::get_singleton()->get_ticks_usec();
  181. p_space->set_elapsed_time(SpaceSW::ELAPSED_TIME_GENERATE_ISLANDS, profile_endtime - profile_begtime);
  182. profile_begtime = profile_endtime;
  183. }
  184. /* SETUP CONSTRAINT ISLANDS */
  185. {
  186. ConstraintSW *ci = constraint_island_list;
  187. while (ci) {
  188. _setup_island(ci, p_delta);
  189. ci = ci->get_island_list_next();
  190. }
  191. }
  192. { //profile
  193. profile_endtime = OS::get_singleton()->get_ticks_usec();
  194. p_space->set_elapsed_time(SpaceSW::ELAPSED_TIME_SETUP_CONSTRAINTS, profile_endtime - profile_begtime);
  195. profile_begtime = profile_endtime;
  196. }
  197. /* SOLVE CONSTRAINT ISLANDS */
  198. {
  199. ConstraintSW *ci = constraint_island_list;
  200. while (ci) {
  201. //iterating each island separatedly improves cache efficiency
  202. _solve_island(ci, p_iterations, p_delta);
  203. ci = ci->get_island_list_next();
  204. }
  205. }
  206. { //profile
  207. profile_endtime = OS::get_singleton()->get_ticks_usec();
  208. p_space->set_elapsed_time(SpaceSW::ELAPSED_TIME_SOLVE_CONSTRAINTS, profile_endtime - profile_begtime);
  209. profile_begtime = profile_endtime;
  210. }
  211. /* INTEGRATE VELOCITIES */
  212. b = body_list->first();
  213. while (b) {
  214. const SelfList<BodySW> *n = b->next();
  215. b->self()->integrate_velocities(p_delta);
  216. b = n;
  217. }
  218. /* SLEEP / WAKE UP ISLANDS */
  219. {
  220. BodySW *bi = island_list;
  221. while (bi) {
  222. _check_suspend(bi, p_delta);
  223. bi = bi->get_island_list_next();
  224. }
  225. }
  226. { //profile
  227. profile_endtime = OS::get_singleton()->get_ticks_usec();
  228. p_space->set_elapsed_time(SpaceSW::ELAPSED_TIME_INTEGRATE_VELOCITIES, profile_endtime - profile_begtime);
  229. profile_begtime = profile_endtime;
  230. }
  231. p_space->unlock();
  232. _step++;
  233. }
  234. StepSW::StepSW() {
  235. _step = 1;
  236. }