godot_step_3d.cpp 14 KB

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