Debian Codegraph @

modules/godot_physics_2d/godot_step_2d.cpp

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