Skip to content

Commit

Permalink
Fix physics tick counter
Browse files Browse the repository at this point in the history
The counter is now incremented at the start of a physics tick rather than at the end.

Co-authored-by: lawnjelly <[email protected]>
  • Loading branch information
rburing and lawnjelly committed Jul 7, 2024
1 parent f3af22b commit 2352163
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 14 deletions.
5 changes: 3 additions & 2 deletions core/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -758,12 +758,13 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em

bool was_pressed = action_state.cache.pressed;
_update_action_cache(E.key, action_state);
// As input may come in part way through a physics tick, the earliest we can react to it is the next physics tick.
if (action_state.cache.pressed && !was_pressed) {
action_state.pressed_physics_frame = Engine::get_singleton()->get_physics_frames();
action_state.pressed_physics_frame = Engine::get_singleton()->get_physics_frames() + 1;
action_state.pressed_process_frame = Engine::get_singleton()->get_process_frames();
}
if (!action_state.cache.pressed && was_pressed) {
action_state.released_physics_frame = Engine::get_singleton()->get_physics_frames();
action_state.released_physics_frame = Engine::get_singleton()->get_physics_frames() + 1;
action_state.released_process_frame = Engine::get_singleton()->get_process_frames();
}
}
Expand Down
2 changes: 1 addition & 1 deletion main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4043,6 +4043,7 @@ bool Main::iteration() {
}

Engine::get_singleton()->_in_physics = true;
Engine::get_singleton()->_physics_frames++;

uint64_t physics_begin = OS::get_singleton()->get_ticks_usec();

Expand Down Expand Up @@ -4090,7 +4091,6 @@ bool Main::iteration() {

physics_process_ticks = MAX(physics_process_ticks, OS::get_singleton()->get_ticks_usec() - physics_begin); // keep the largest one for reference
physics_process_max = MAX(OS::get_singleton()->get_ticks_usec() - physics_begin, physics_process_max);
Engine::get_singleton()->_physics_frames++;

Engine::get_singleton()->_in_physics = false;
}
Expand Down
2 changes: 1 addition & 1 deletion scene/2d/camera_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Camera2D : public Node2D {
struct InterpolationData {
Transform2D xform_curr;
Transform2D xform_prev;
uint32_t last_update_physics_tick = 0;
uint32_t last_update_physics_tick = UINT32_MAX; // Ensure tick 0 is detected as a change.
} _interpolation_data;

void _ensure_update_interpolation_data();
Expand Down
3 changes: 0 additions & 3 deletions scene/2d/navigation_agent_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,6 @@ void NavigationAgent2D::_update_navigation() {
return;
}

update_frame_id = Engine::get_singleton()->get_physics_frames();

Vector2 origin = agent_parent->get_global_position();

bool reload_path = false;
Expand Down Expand Up @@ -767,7 +765,6 @@ void NavigationAgent2D::_request_repath() {
target_reached = false;
navigation_finished = false;
last_waypoint_reached = false;
update_frame_id = 0;
}

bool NavigationAgent2D::_is_last_waypoint() const {
Expand Down
2 changes: 0 additions & 2 deletions scene/2d/navigation_agent_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ class NavigationAgent2D : public Node {
bool target_reached = false;
bool navigation_finished = true;
bool last_waypoint_reached = false;
// No initialized on purpose
uint32_t update_frame_id = 0;

// Debug properties for exposed bindings
bool debug_enabled = false;
Expand Down
3 changes: 0 additions & 3 deletions scene/3d/navigation_agent_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,8 +737,6 @@ void NavigationAgent3D::_update_navigation() {
return;
}

update_frame_id = Engine::get_singleton()->get_physics_frames();

Vector3 origin = agent_parent->get_global_position();

bool reload_path = false;
Expand Down Expand Up @@ -835,7 +833,6 @@ void NavigationAgent3D::_request_repath() {
target_reached = false;
navigation_finished = false;
last_waypoint_reached = false;
update_frame_id = 0;
}

bool NavigationAgent3D::_is_last_waypoint() const {
Expand Down
2 changes: 0 additions & 2 deletions scene/3d/navigation_agent_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ class NavigationAgent3D : public Node {
bool target_reached = false;
bool navigation_finished = true;
bool last_waypoint_reached = false;
// No initialized on purpose
uint32_t update_frame_id = 0;

// Debug properties for exposed bindings
bool debug_enabled = false;
Expand Down

0 comments on commit 2352163

Please sign in to comment.