godot_step_2d.cpp 11 KB

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