godot_step_2d.cpp 11 KB

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