godot_step_2d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**************************************************************************/
  2. /* godot_step_2d.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 "godot_step_2d.h"
  31. #include "core/object/worker_thread_pool.h"
  32. #include "core/os/os.h"
  33. #define BODY_ISLAND_COUNT_RESERVE 128
  34. #define BODY_ISLAND_SIZE_RESERVE 512
  35. #define ISLAND_COUNT_RESERVE 128
  36. #define ISLAND_SIZE_RESERVE 512
  37. #define CONSTRAINT_COUNT_RESERVE 1024
  38. void GodotStep2D::_populate_island(GodotBody2D *p_body, LocalVector<GodotBody2D *> &p_body_island, LocalVector<GodotConstraint2D *> &p_constraint_island) {
  39. p_body->set_island_step(_step);
  40. if (p_body->get_mode() > PhysicsServer2D::BODY_MODE_KINEMATIC) {
  41. // Only rigid bodies are tested for activation.
  42. p_body_island.push_back(p_body);
  43. }
  44. for (const Pair<GodotConstraint2D *, int> &E : p_body->get_constraint_list()) {
  45. GodotConstraint2D *constraint = const_cast<GodotConstraint2D *>(E.first);
  46. if (constraint->get_island_step() == _step) {
  47. continue; // Already processed.
  48. }
  49. constraint->set_island_step(_step);
  50. p_constraint_island.push_back(constraint);
  51. all_constraints.push_back(constraint);
  52. for (int i = 0; i < constraint->get_body_count(); i++) {
  53. if (i == E.second) {
  54. continue;
  55. }
  56. GodotBody2D *other_body = constraint->get_body_ptr()[i];
  57. if (other_body->get_island_step() == _step) {
  58. continue; // Already processed.
  59. }
  60. if (other_body->get_mode() == PhysicsServer2D::BODY_MODE_STATIC) {
  61. continue; // Static bodies don't connect islands.
  62. }
  63. _populate_island(other_body, p_body_island, p_constraint_island);
  64. }
  65. }
  66. }
  67. void GodotStep2D::_setup_constraint(uint32_t p_constraint_index, void *p_userdata) {
  68. GodotConstraint2D *constraint = all_constraints[p_constraint_index];
  69. constraint->setup(delta);
  70. }
  71. void GodotStep2D::_pre_solve_island(LocalVector<GodotConstraint2D *> &p_constraint_island) const {
  72. uint32_t constraint_count = p_constraint_island.size();
  73. uint32_t valid_constraint_count = 0;
  74. for (uint32_t constraint_index = 0; constraint_index < constraint_count; ++constraint_index) {
  75. GodotConstraint2D *constraint = p_constraint_island[constraint_index];
  76. if (p_constraint_island[constraint_index]->pre_solve(delta)) {
  77. // Keep this constraint for solving.
  78. p_constraint_island[valid_constraint_count++] = constraint;
  79. }
  80. }
  81. p_constraint_island.resize(valid_constraint_count);
  82. }
  83. void GodotStep2D::_solve_island(uint32_t p_island_index, void *p_userdata) const {
  84. const LocalVector<GodotConstraint2D *> &constraint_island = constraint_islands[p_island_index];
  85. for (int i = 0; i < iterations; i++) {
  86. uint32_t constraint_count = constraint_island.size();
  87. for (uint32_t constraint_index = 0; constraint_index < constraint_count; ++constraint_index) {
  88. constraint_island[constraint_index]->solve(delta);
  89. }
  90. }
  91. }
  92. void GodotStep2D::_check_suspend(LocalVector<GodotBody2D *> &p_body_island) const {
  93. bool can_sleep = true;
  94. uint32_t body_count = p_body_island.size();
  95. for (uint32_t body_index = 0; body_index < body_count; ++body_index) {
  96. GodotBody2D *body = p_body_island[body_index];
  97. if (!body->sleep_test(delta)) {
  98. can_sleep = false;
  99. }
  100. }
  101. // Put all to sleep or wake up everyone.
  102. for (uint32_t body_index = 0; body_index < body_count; ++body_index) {
  103. GodotBody2D *body = p_body_island[body_index];
  104. bool active = body->is_active();
  105. if (active == can_sleep) {
  106. body->set_active(!can_sleep);
  107. }
  108. }
  109. }
  110. void GodotStep2D::step(GodotSpace2D *p_space, real_t p_delta) {
  111. p_space->lock(); // can't access space during this
  112. p_space->setup(); //update inertias, etc
  113. p_space->set_last_step(p_delta);
  114. iterations = p_space->get_solver_iterations();
  115. delta = p_delta;
  116. const SelfList<GodotBody2D>::List *body_list = &p_space->get_active_body_list();
  117. /* INTEGRATE FORCES */
  118. uint64_t profile_begtime = OS::get_singleton()->get_ticks_usec();
  119. uint64_t profile_endtime = 0;
  120. int active_count = 0;
  121. const SelfList<GodotBody2D> *b = body_list->first();
  122. while (b) {
  123. b->self()->integrate_forces(p_delta);
  124. b = b->next();
  125. active_count++;
  126. }
  127. p_space->set_active_objects(active_count);
  128. // Update the broadphase to register collision pairs.
  129. p_space->update();
  130. { //profile
  131. profile_endtime = OS::get_singleton()->get_ticks_usec();
  132. p_space->set_elapsed_time(GodotSpace2D::ELAPSED_TIME_INTEGRATE_FORCES, profile_endtime - profile_begtime);
  133. profile_begtime = profile_endtime;
  134. }
  135. /* GENERATE CONSTRAINT ISLANDS FOR MOVING AREAS */
  136. uint32_t island_count = 0;
  137. const SelfList<GodotArea2D>::List &aml = p_space->get_moved_area_list();
  138. while (aml.first()) {
  139. for (GodotConstraint2D *E : aml.first()->self()->get_constraints()) {
  140. GodotConstraint2D *constraint = E;
  141. if (constraint->get_island_step() == _step) {
  142. continue;
  143. }
  144. constraint->set_island_step(_step);
  145. // Each constraint can be on a separate island for areas as there's no solving phase.
  146. ++island_count;
  147. if (constraint_islands.size() < island_count) {
  148. constraint_islands.resize(island_count);
  149. }
  150. LocalVector<GodotConstraint2D *> &constraint_island = constraint_islands[island_count - 1];
  151. constraint_island.clear();
  152. all_constraints.push_back(constraint);
  153. constraint_island.push_back(constraint);
  154. }
  155. p_space->area_remove_from_moved_list((SelfList<GodotArea2D> *)aml.first()); //faster to remove here
  156. }
  157. /* GENERATE CONSTRAINT ISLANDS FOR ACTIVE RIGID BODIES */
  158. b = body_list->first();
  159. uint32_t body_island_count = 0;
  160. while (b) {
  161. GodotBody2D *body = b->self();
  162. if (body->get_island_step() != _step) {
  163. ++body_island_count;
  164. if (body_islands.size() < body_island_count) {
  165. body_islands.resize(body_island_count);
  166. }
  167. LocalVector<GodotBody2D *> &body_island = body_islands[body_island_count - 1];
  168. body_island.clear();
  169. body_island.reserve(BODY_ISLAND_SIZE_RESERVE);
  170. ++island_count;
  171. if (constraint_islands.size() < island_count) {
  172. constraint_islands.resize(island_count);
  173. }
  174. LocalVector<GodotConstraint2D *> &constraint_island = constraint_islands[island_count - 1];
  175. constraint_island.clear();
  176. constraint_island.reserve(ISLAND_SIZE_RESERVE);
  177. _populate_island(body, body_island, constraint_island);
  178. if (body_island.is_empty()) {
  179. --body_island_count;
  180. }
  181. if (constraint_island.is_empty()) {
  182. --island_count;
  183. }
  184. }
  185. b = b->next();
  186. }
  187. p_space->set_island_count((int)island_count);
  188. { //profile
  189. profile_endtime = OS::get_singleton()->get_ticks_usec();
  190. p_space->set_elapsed_time(GodotSpace2D::ELAPSED_TIME_GENERATE_ISLANDS, profile_endtime - profile_begtime);
  191. profile_begtime = profile_endtime;
  192. }
  193. /* SETUP CONSTRAINTS / PROCESS COLLISIONS */
  194. uint32_t total_constraint_count = all_constraints.size();
  195. WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &GodotStep2D::_setup_constraint, nullptr, total_constraint_count, -1, true, SNAME("Physics2DConstraintSetup"));
  196. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
  197. { //profile
  198. profile_endtime = OS::get_singleton()->get_ticks_usec();
  199. p_space->set_elapsed_time(GodotSpace2D::ELAPSED_TIME_SETUP_CONSTRAINTS, profile_endtime - profile_begtime);
  200. profile_begtime = profile_endtime;
  201. }
  202. /* PRE-SOLVE CONSTRAINT ISLANDS */
  203. // Warning: This doesn't run on threads, because it involves thread-unsafe processing.
  204. for (uint32_t island_index = 0; island_index < island_count; ++island_index) {
  205. _pre_solve_island(constraint_islands[island_index]);
  206. }
  207. /* SOLVE CONSTRAINT ISLANDS */
  208. // Warning: _solve_island modifies the constraint islands for optimization purpose,
  209. // their content is not reliable after these calls and shouldn't be used anymore.
  210. group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &GodotStep2D::_solve_island, nullptr, island_count, -1, true, SNAME("Physics2DConstraintSolveIslands"));
  211. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
  212. { //profile
  213. profile_endtime = OS::get_singleton()->get_ticks_usec();
  214. p_space->set_elapsed_time(GodotSpace2D::ELAPSED_TIME_SOLVE_CONSTRAINTS, profile_endtime - profile_begtime);
  215. profile_begtime = profile_endtime;
  216. }
  217. /* INTEGRATE VELOCITIES */
  218. b = body_list->first();
  219. while (b) {
  220. const SelfList<GodotBody2D> *n = b->next();
  221. b->self()->integrate_velocities(p_delta);
  222. b = n; // in case it shuts itself down
  223. }
  224. /* SLEEP / WAKE UP ISLANDS */
  225. for (uint32_t island_index = 0; island_index < body_island_count; ++island_index) {
  226. _check_suspend(body_islands[island_index]);
  227. }
  228. { //profile
  229. profile_endtime = OS::get_singleton()->get_ticks_usec();
  230. p_space->set_elapsed_time(GodotSpace2D::ELAPSED_TIME_INTEGRATE_VELOCITIES, profile_endtime - profile_begtime);
  231. //profile_begtime=profile_endtime;
  232. }
  233. all_constraints.clear();
  234. p_space->unlock();
  235. _step++;
  236. }
  237. GodotStep2D::GodotStep2D() {
  238. body_islands.reserve(BODY_ISLAND_COUNT_RESERVE);
  239. constraint_islands.reserve(ISLAND_COUNT_RESERVE);
  240. all_constraints.reserve(CONSTRAINT_COUNT_RESERVE);
  241. }
  242. GodotStep2D::~GodotStep2D() {
  243. }