Skip to content

Commit 16fd7b6

Browse files
committed
Prepare NavigationServer for process() and physics_process() split
Prepares the NavigationServer API for a split of its functionality between frame process() and stepped physics_process().
1 parent c7ea861 commit 16fd7b6

15 files changed

+131
-103
lines changed

main/main.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4648,10 +4648,10 @@ bool Main::iteration() {
46484648
uint64_t navigation_begin = OS::get_singleton()->get_ticks_usec();
46494649

46504650
#ifndef NAVIGATION_2D_DISABLED
4651-
NavigationServer2D::get_singleton()->process(physics_step * time_scale);
4651+
NavigationServer2D::get_singleton()->physics_process(physics_step * time_scale);
46524652
#endif // NAVIGATION_2D_DISABLED
46534653
#ifndef NAVIGATION_3D_DISABLED
4654-
NavigationServer3D::get_singleton()->process(physics_step * time_scale);
4654+
NavigationServer3D::get_singleton()->physics_process(physics_step * time_scale);
46554655
#endif // NAVIGATION_3D_DISABLED
46564656

46574657
navigation_process_ticks = MAX(navigation_process_ticks, OS::get_singleton()->get_ticks_usec() - navigation_begin); // keep the largest one for reference
@@ -4691,6 +4691,13 @@ bool Main::iteration() {
46914691
}
46924692
message_queue->flush();
46934693

4694+
#ifndef NAVIGATION_2D_DISABLED
4695+
NavigationServer2D::get_singleton()->process(process_step * time_scale);
4696+
#endif // NAVIGATION_2D_DISABLED
4697+
#ifndef NAVIGATION_3D_DISABLED
4698+
NavigationServer3D::get_singleton()->process(process_step * time_scale);
4699+
#endif // NAVIGATION_3D_DISABLED
4700+
46944701
RenderingServer::get_singleton()->sync(); //sync if still drawing from previous frames.
46954702

46964703
const bool has_pending_resources_for_processing = RD::get_singleton() && RD::get_singleton()->has_pending_resources_for_processing();

modules/navigation_2d/2d/godot_navigation_server_2d.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,8 +1138,6 @@ Vector<Vector2> GodotNavigationServer2D::obstacle_get_vertices(RID p_obstacle) c
11381138
}
11391139

11401140
void GodotNavigationServer2D::flush_queries() {
1141-
// In c++ we can't be sure that this is performed in the main thread
1142-
// even with mutable functions.
11431141
MutexLock lock(commands_mutex);
11441142
MutexLock lock2(operations_mutex);
11451143

@@ -1259,7 +1257,21 @@ void GodotNavigationServer2D::internal_free_obstacle(RID p_object) {
12591257
}
12601258
}
12611259

1262-
void GodotNavigationServer2D::process(real_t p_delta_time) {
1260+
void GodotNavigationServer2D::process(double p_delta_time) {
1261+
// Called for each main loop iteration AFTER node and user script process() and BEFORE RenderingServer sync.
1262+
// Will run reliably every rendered frame independent of the physics tick rate.
1263+
// Use for things that (only) need to update once per main loop iteration and rendered frame or is visible to the user.
1264+
// E.g. (final) sync of objects for this main loop iteration, updating rendered debug visuals, updating debug statistics, ...
1265+
}
1266+
1267+
void GodotNavigationServer2D::physics_process(double p_delta_time) {
1268+
// Called for each physics process step AFTER node and user script physics_process() and BEFORE PhysicsServer sync.
1269+
// Will NOT run reliably every rendered frame. If there is no physics step this function will not run.
1270+
// Use for physics or step depending calculations and updates where the result affects the next step calculation.
1271+
// E.g. anything physics sync related, avoidance simulations, physics space state queries, ...
1272+
// If physics process needs to play catchup this function will be called multiple times per frame so it should not hold
1273+
// costly updates that are not important outside the stepped calculations to avoid causing a physics performance death spiral.
1274+
12631275
flush_queries();
12641276

12651277
if (!active) {

modules/navigation_2d/2d/godot_navigation_server_2d.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,9 @@ class GodotNavigationServer2D : public NavigationServer2D {
320320
virtual void set_active(bool p_active) override;
321321

322322
void flush_queries();
323-
virtual void process(real_t p_delta_time) override;
323+
324+
virtual void process(double p_delta_time) override;
325+
virtual void physics_process(double p_delta_time) override;
324326
virtual void init() override;
325327
virtual void sync() override;
326328
virtual void finish() override;

modules/navigation_2d/nav_map_2d.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -532,10 +532,8 @@ void NavMap2D::compute_single_avoidance_step(uint32_t p_index, NavAgent2D **p_ag
532532
(*(p_agent + p_index))->update();
533533
}
534534

535-
void NavMap2D::step(real_t p_deltatime) {
536-
deltatime = p_deltatime;
537-
538-
rvo_simulation.setTimeStep(float(deltatime));
535+
void NavMap2D::step(double p_delta_time) {
536+
rvo_simulation.setTimeStep(float(p_delta_time));
539537

540538
if (active_avoidance_agents.size() > 0) {
541539
if (use_threads && avoidance_use_multiple_threads) {

modules/navigation_2d/nav_map_2d.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ class NavMap2D : public NavRid2D {
9191
/// Are rvo obstacles modified?
9292
bool obstacles_dirty = true;
9393

94-
/// Physics delta time.
95-
real_t deltatime = 0.0;
96-
9794
/// Change the id each time the map is updated.
9895
uint32_t iteration_id = 0;
9996

@@ -203,7 +200,7 @@ class NavMap2D : public NavRid2D {
203200
Vector2 get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const;
204201

205202
void sync();
206-
void step(real_t p_deltatime);
203+
void step(double p_delta_time);
207204
void dispatch_callbacks();
208205

209206
// Performance Monitor

modules/navigation_3d/3d/godot_navigation_server_3d.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,8 +1306,6 @@ void GodotNavigationServer3D::set_active(bool p_active) {
13061306
}
13071307

13081308
void GodotNavigationServer3D::flush_queries() {
1309-
// In c++ we can't be sure that this is performed in the main thread
1310-
// even with mutable functions.
13111309
MutexLock lock(commands_mutex);
13121310
MutexLock lock2(operations_mutex);
13131311

@@ -1340,7 +1338,21 @@ void GodotNavigationServer3D::sync() {
13401338
}
13411339
}
13421340

1343-
void GodotNavigationServer3D::process(real_t p_delta_time) {
1341+
void GodotNavigationServer3D::process(double p_delta_time) {
1342+
// Called for each main loop iteration AFTER node and user script process() and BEFORE RenderingServer sync.
1343+
// Will run reliably every rendered frame independent of the physics tick rate.
1344+
// Use for things that (only) need to update once per main loop iteration and rendered frame or is visible to the user.
1345+
// E.g. (final) sync of objects for this main loop iteration, updating rendered debug visuals, updating debug statistics, ...
1346+
}
1347+
1348+
void GodotNavigationServer3D::physics_process(double p_delta_time) {
1349+
// Called for each physics process step AFTER node and user script physics_process() and BEFORE PhysicsServer sync.
1350+
// Will NOT run reliably every rendered frame. If there is no physics step this function will not run.
1351+
// Use for physics or step depending calculations and updates where the result affects the next step calculation.
1352+
// E.g. anything physics sync related, avoidance simulations, physics space state queries, ...
1353+
// If physics process needs to play catchup this function will be called multiple times per frame so it should not hold
1354+
// costly updates that are not important outside the stepped calculations to avoid causing a physics performance death spiral.
1355+
13441356
flush_queries();
13451357

13461358
if (!active) {

modules/navigation_3d/3d/godot_navigation_server_3d.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,9 @@ class GodotNavigationServer3D : public NavigationServer3D {
280280
virtual void set_active(bool p_active) override;
281281

282282
void flush_queries();
283-
virtual void process(real_t p_delta_time) override;
283+
284+
virtual void process(double p_delta_time) override;
285+
virtual void physics_process(double p_delta_time) override;
284286
virtual void init() override;
285287
virtual void sync() override;
286288
virtual void finish() override;

modules/navigation_3d/nav_map_3d.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -614,11 +614,9 @@ void NavMap3D::compute_single_avoidance_step_3d(uint32_t index, NavAgent3D **age
614614
(*(agent + index))->update();
615615
}
616616

617-
void NavMap3D::step(real_t p_deltatime) {
618-
deltatime = p_deltatime;
619-
620-
rvo_simulation_2d.setTimeStep(float(deltatime));
621-
rvo_simulation_3d.setTimeStep(float(deltatime));
617+
void NavMap3D::step(double p_delta_time) {
618+
rvo_simulation_2d.setTimeStep(float(p_delta_time));
619+
rvo_simulation_3d.setTimeStep(float(p_delta_time));
622620

623621
if (active_2d_avoidance_agents.size() > 0) {
624622
if (use_threads && avoidance_use_multiple_threads) {

modules/navigation_3d/nav_map_3d.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ class NavMap3D : public NavRid3D {
9999
/// Are rvo obstacles modified?
100100
bool obstacles_dirty = true;
101101

102-
/// Physics delta time
103-
real_t deltatime = 0.0;
104-
105102
/// Change the id each time the map is updated.
106103
uint32_t iteration_id = 0;
107104

@@ -221,7 +218,7 @@ class NavMap3D : public NavRid3D {
221218
Vector3 get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const;
222219

223220
void sync();
224-
void step(real_t p_deltatime);
221+
void step(double p_delta_time);
225222
void dispatch_callbacks();
226223

227224
// Performance Monitor

servers/navigation_server_2d.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ class NavigationServer2D : public Object {
308308
/// The result of this process is needed by the physics server,
309309
/// so this must be called in the main thread.
310310
/// Note: This function is not thread safe.
311-
virtual void process(real_t delta_time) = 0;
311+
virtual void process(double p_delta_time) = 0;
312+
virtual void physics_process(double p_delta_time) = 0;
312313
virtual void init() = 0;
313314
virtual void sync() = 0;
314315
virtual void finish() = 0;

0 commit comments

Comments
 (0)