godot_step_3d.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /**************************************************************************/
  2. /* godot_step_3d.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_3d.h"
  31. #include "godot_joint_3d.h"
  32. #include "core/object/worker_thread_pool.h"
  33. #include "core/os/os.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 GodotStep3D::_populate_island(GodotBody3D *p_body, LocalVector<GodotBody3D *> &p_body_island, LocalVector<GodotConstraint3D *> &p_constraint_island) {
  40. p_body->set_island_step(_step);
  41. if (p_body->get_mode() > PhysicsServer3D::BODY_MODE_KINEMATIC) {
  42. // Only rigid bodies are tested for activation.
  43. p_body_island.push_back(p_body);
  44. }
  45. for (const KeyValue<GodotConstraint3D *, int> &E : p_body->get_constraint_map()) {
  46. GodotConstraint3D *constraint = const_cast<GodotConstraint3D *>(E.key);
  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. // Find connected rigid bodies.
  54. for (int i = 0; i < constraint->get_body_count(); i++) {
  55. if (i == E.value) {
  56. continue;
  57. }
  58. GodotBody3D *other_body = constraint->get_body_ptr()[i];
  59. if (other_body->get_island_step() == _step) {
  60. continue; // Already processed.
  61. }
  62. if (other_body->get_mode() == PhysicsServer3D::BODY_MODE_STATIC) {
  63. continue; // Static bodies don't connect islands.
  64. }
  65. _populate_island(other_body, p_body_island, p_constraint_island);
  66. }
  67. // Find connected soft bodies.
  68. for (int i = 0; i < constraint->get_soft_body_count(); i++) {
  69. GodotSoftBody3D *soft_body = constraint->get_soft_body_ptr(i);
  70. if (soft_body->get_island_step() == _step) {
  71. continue; // Already processed.
  72. }
  73. _populate_island_soft_body(soft_body, p_body_island, p_constraint_island);
  74. }
  75. }
  76. }
  77. void GodotStep3D::_populate_island_soft_body(GodotSoftBody3D *p_soft_body, LocalVector<GodotBody3D *> &p_body_island, LocalVector<GodotConstraint3D *> &p_constraint_island) {
  78. p_soft_body->set_island_step(_step);
  79. for (const GodotConstraint3D *E : p_soft_body->get_constraints()) {
  80. GodotConstraint3D *constraint = const_cast<GodotConstraint3D *>(E);
  81. if (constraint->get_island_step() == _step) {
  82. continue; // Already processed.
  83. }
  84. constraint->set_island_step(_step);
  85. p_constraint_island.push_back(constraint);
  86. all_constraints.push_back(constraint);
  87. // Find connected rigid bodies.
  88. for (int i = 0; i < constraint->get_body_count(); i++) {
  89. GodotBody3D *body = constraint->get_body_ptr()[i];
  90. if (body->get_island_step() == _step) {
  91. continue; // Already processed.
  92. }
  93. if (body->get_mode() == PhysicsServer3D::BODY_MODE_STATIC) {
  94. continue; // Static bodies don't connect islands.
  95. }
  96. _populate_island(body, p_body_island, p_constraint_island);
  97. }
  98. }
  99. }
  100. void GodotStep3D::_setup_constraint(uint32_t p_constraint_index, void *p_userdata) {
  101. GodotConstraint3D *constraint = all_constraints[p_constraint_index];
  102. constraint->setup(delta);
  103. }
  104. void GodotStep3D::_pre_solve_island(LocalVector<GodotConstraint3D *> &p_constraint_island) const {
  105. uint32_t constraint_count = p_constraint_island.size();
  106. uint32_t valid_constraint_count = 0;
  107. for (uint32_t constraint_index = 0; constraint_index < constraint_count; ++constraint_index) {
  108. GodotConstraint3D *constraint = p_constraint_island[constraint_index];
  109. if (p_constraint_island[constraint_index]->pre_solve(delta)) {
  110. // Keep this constraint for solving.
  111. p_constraint_island[valid_constraint_count++] = constraint;
  112. }
  113. }
  114. p_constraint_island.resize(valid_constraint_count);
  115. }
  116. void GodotStep3D::_solve_island(uint32_t p_island_index, void *p_userdata) {
  117. LocalVector<GodotConstraint3D *> &constraint_island = constraint_islands[p_island_index];
  118. int current_priority = 1;
  119. uint32_t constraint_count = constraint_island.size();
  120. while (constraint_count > 0) {
  121. for (int i = 0; i < iterations; i++) {
  122. // Go through all iterations.
  123. for (uint32_t constraint_index = 0; constraint_index < constraint_count; ++constraint_index) {
  124. constraint_island[constraint_index]->solve(delta);
  125. }
  126. }
  127. // Check priority to keep only higher priority constraints.
  128. uint32_t priority_constraint_count = 0;
  129. ++current_priority;
  130. for (uint32_t constraint_index = 0; constraint_index < constraint_count; ++constraint_index) {
  131. GodotConstraint3D *constraint = constraint_island[constraint_index];
  132. if (constraint->get_priority() >= current_priority) {
  133. // Keep this constraint for the next iteration.
  134. constraint_island[priority_constraint_count++] = constraint;
  135. }
  136. }
  137. constraint_count = priority_constraint_count;
  138. }
  139. }
  140. void GodotStep3D::_check_suspend(const LocalVector<GodotBody3D *> &p_body_island) const {
  141. bool can_sleep = true;
  142. uint32_t body_count = p_body_island.size();
  143. for (uint32_t body_index = 0; body_index < body_count; ++body_index) {
  144. GodotBody3D *body = p_body_island[body_index];
  145. if (!body->sleep_test(delta)) {
  146. can_sleep = false;
  147. }
  148. }
  149. // Put all to sleep or wake up everyone.
  150. for (uint32_t body_index = 0; body_index < body_count; ++body_index) {
  151. GodotBody3D *body = p_body_island[body_index];
  152. bool active = body->is_active();
  153. if (active == can_sleep) {
  154. body->set_active(!can_sleep);
  155. }
  156. }
  157. }
  158. void GodotStep3D::step(GodotSpace3D *p_space, real_t p_delta) {
  159. p_space->lock(); // can't access space during this
  160. p_space->setup(); //update inertias, etc
  161. p_space->set_last_step(p_delta);
  162. iterations = p_space->get_solver_iterations();
  163. delta = p_delta;
  164. const SelfList<GodotBody3D>::List *body_list = &p_space->get_active_body_list();
  165. const SelfList<GodotSoftBody3D>::List *soft_body_list = &p_space->get_active_soft_body_list();
  166. /* INTEGRATE FORCES */
  167. uint64_t profile_begtime = OS::get_singleton()->get_ticks_usec();
  168. uint64_t profile_endtime = 0;
  169. int active_count = 0;
  170. const SelfList<GodotBody3D> *b = body_list->first();
  171. while (b) {
  172. b->self()->integrate_forces(p_delta);
  173. b = b->next();
  174. active_count++;
  175. }
  176. /* UPDATE SOFT BODY MOTION */
  177. const SelfList<GodotSoftBody3D> *sb = soft_body_list->first();
  178. while (sb) {
  179. sb->self()->predict_motion(p_delta);
  180. sb = sb->next();
  181. active_count++;
  182. }
  183. p_space->set_active_objects(active_count);
  184. // Update the broadphase to register collision pairs.
  185. p_space->update();
  186. { //profile
  187. profile_endtime = OS::get_singleton()->get_ticks_usec();
  188. p_space->set_elapsed_time(GodotSpace3D::ELAPSED_TIME_INTEGRATE_FORCES, profile_endtime - profile_begtime);
  189. profile_begtime = profile_endtime;
  190. }
  191. /* GENERATE CONSTRAINT ISLANDS FOR MOVING AREAS */
  192. uint32_t island_count = 0;
  193. const SelfList<GodotArea3D>::List &aml = p_space->get_moved_area_list();
  194. while (aml.first()) {
  195. for (GodotConstraint3D *E : aml.first()->self()->get_constraints()) {
  196. GodotConstraint3D *constraint = E;
  197. if (constraint->get_island_step() == _step) {
  198. continue;
  199. }
  200. constraint->set_island_step(_step);
  201. // Each constraint can be on a separate island for areas as there's no solving phase.
  202. ++island_count;
  203. if (constraint_islands.size() < island_count) {
  204. constraint_islands.resize(island_count);
  205. }
  206. LocalVector<GodotConstraint3D *> &constraint_island = constraint_islands[island_count - 1];
  207. constraint_island.clear();
  208. all_constraints.push_back(constraint);
  209. constraint_island.push_back(constraint);
  210. }
  211. p_space->area_remove_from_moved_list((SelfList<GodotArea3D> *)aml.first()); //faster to remove here
  212. }
  213. /* GENERATE CONSTRAINT ISLANDS FOR ACTIVE RIGID BODIES */
  214. b = body_list->first();
  215. uint32_t body_island_count = 0;
  216. while (b) {
  217. GodotBody3D *body = b->self();
  218. if (body->get_island_step() != _step) {
  219. ++body_island_count;
  220. if (body_islands.size() < body_island_count) {
  221. body_islands.resize(body_island_count);
  222. }
  223. LocalVector<GodotBody3D *> &body_island = body_islands[body_island_count - 1];
  224. body_island.clear();
  225. body_island.reserve(BODY_ISLAND_SIZE_RESERVE);
  226. ++island_count;
  227. if (constraint_islands.size() < island_count) {
  228. constraint_islands.resize(island_count);
  229. }
  230. LocalVector<GodotConstraint3D *> &constraint_island = constraint_islands[island_count - 1];
  231. constraint_island.clear();
  232. constraint_island.reserve(ISLAND_SIZE_RESERVE);
  233. _populate_island(body, body_island, constraint_island);
  234. if (body_island.is_empty()) {
  235. --body_island_count;
  236. }
  237. if (constraint_island.is_empty()) {
  238. --island_count;
  239. }
  240. }
  241. b = b->next();
  242. }
  243. /* GENERATE CONSTRAINT ISLANDS FOR ACTIVE SOFT BODIES */
  244. sb = soft_body_list->first();
  245. while (sb) {
  246. GodotSoftBody3D *soft_body = sb->self();
  247. if (soft_body->get_island_step() != _step) {
  248. ++body_island_count;
  249. if (body_islands.size() < body_island_count) {
  250. body_islands.resize(body_island_count);
  251. }
  252. LocalVector<GodotBody3D *> &body_island = body_islands[body_island_count - 1];
  253. body_island.clear();
  254. body_island.reserve(BODY_ISLAND_SIZE_RESERVE);
  255. ++island_count;
  256. if (constraint_islands.size() < island_count) {
  257. constraint_islands.resize(island_count);
  258. }
  259. LocalVector<GodotConstraint3D *> &constraint_island = constraint_islands[island_count - 1];
  260. constraint_island.clear();
  261. constraint_island.reserve(ISLAND_SIZE_RESERVE);
  262. _populate_island_soft_body(soft_body, body_island, constraint_island);
  263. if (body_island.is_empty()) {
  264. --body_island_count;
  265. }
  266. if (constraint_island.is_empty()) {
  267. --island_count;
  268. }
  269. }
  270. sb = sb->next();
  271. }
  272. p_space->set_island_count((int)island_count);
  273. { //profile
  274. profile_endtime = OS::get_singleton()->get_ticks_usec();
  275. p_space->set_elapsed_time(GodotSpace3D::ELAPSED_TIME_GENERATE_ISLANDS, profile_endtime - profile_begtime);
  276. profile_begtime = profile_endtime;
  277. }
  278. /* SETUP CONSTRAINTS / PROCESS COLLISIONS */
  279. uint32_t total_constraint_count = all_constraints.size();
  280. WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &GodotStep3D::_setup_constraint, nullptr, total_constraint_count, -1, true, SNAME("Physics3DConstraintSetup"));
  281. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
  282. { //profile
  283. profile_endtime = OS::get_singleton()->get_ticks_usec();
  284. p_space->set_elapsed_time(GodotSpace3D::ELAPSED_TIME_SETUP_CONSTRAINTS, profile_endtime - profile_begtime);
  285. profile_begtime = profile_endtime;
  286. }
  287. /* PRE-SOLVE CONSTRAINT ISLANDS */
  288. // Warning: This doesn't run on threads, because it involves thread-unsafe processing.
  289. for (uint32_t island_index = 0; island_index < island_count; ++island_index) {
  290. _pre_solve_island(constraint_islands[island_index]);
  291. }
  292. /* SOLVE CONSTRAINT ISLANDS */
  293. // Warning: _solve_island modifies the constraint islands for optimization purpose,
  294. // their content is not reliable after these calls and shouldn't be used anymore.
  295. group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &GodotStep3D::_solve_island, nullptr, island_count, -1, true, SNAME("Physics3DConstraintSolveIslands"));
  296. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
  297. { //profile
  298. profile_endtime = OS::get_singleton()->get_ticks_usec();
  299. p_space->set_elapsed_time(GodotSpace3D::ELAPSED_TIME_SOLVE_CONSTRAINTS, profile_endtime - profile_begtime);
  300. profile_begtime = profile_endtime;
  301. }
  302. /* INTEGRATE VELOCITIES */
  303. b = body_list->first();
  304. while (b) {
  305. const SelfList<GodotBody3D> *n = b->next();
  306. b->self()->integrate_velocities(p_delta);
  307. b = n;
  308. }
  309. /* SLEEP / WAKE UP ISLANDS */
  310. for (uint32_t island_index = 0; island_index < body_island_count; ++island_index) {
  311. _check_suspend(body_islands[island_index]);
  312. }
  313. /* UPDATE SOFT BODY CONSTRAINTS */
  314. sb = soft_body_list->first();
  315. while (sb) {
  316. sb->self()->solve_constraints(p_delta);
  317. sb = sb->next();
  318. }
  319. { //profile
  320. profile_endtime = OS::get_singleton()->get_ticks_usec();
  321. p_space->set_elapsed_time(GodotSpace3D::ELAPSED_TIME_INTEGRATE_VELOCITIES, profile_endtime - profile_begtime);
  322. profile_begtime = profile_endtime;
  323. }
  324. all_constraints.clear();
  325. p_space->unlock();
  326. _step++;
  327. }
  328. GodotStep3D::GodotStep3D() {
  329. body_islands.reserve(BODY_ISLAND_COUNT_RESERVE);
  330. constraint_islands.reserve(ISLAND_COUNT_RESERVE);
  331. all_constraints.reserve(CONSTRAINT_COUNT_RESERVE);
  332. }
  333. GodotStep3D::~GodotStep3D() {
  334. }