From 66f7c48e39c6479f11c0dd2426c093b0f2910f76 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Thu, 15 Sep 2022 18:57:34 +0200 Subject: Implement adjusting the maximum number of physics steps per rendered frame When using high physics FPS (which is a requirement to minimize input lag and improve precision in simulation racing games), a higher value prevents the game from slowing down at low rendering FPS. This can be done via an Engine property for run-time changes, or a project setting for initial changes. --- core/config/engine.cpp | 9 +++++++++ core/config/engine.h | 4 ++++ core/core_bind.cpp | 11 +++++++++++ core/core_bind.h | 3 +++ 4 files changed, 27 insertions(+) (limited to 'core') diff --git a/core/config/engine.cpp b/core/config/engine.cpp index 21e910be5b..7aa5f4d06d 100644 --- a/core/config/engine.cpp +++ b/core/config/engine.cpp @@ -48,6 +48,15 @@ int Engine::get_physics_ticks_per_second() const { return ips; } +void Engine::set_max_physics_steps_per_frame(int p_max_physics_steps) { + ERR_FAIL_COND_MSG(p_max_physics_steps <= 0, "Maximum number of physics steps per frame must be greater than 0."); + max_physics_steps_per_frame = p_max_physics_steps; +} + +int Engine::get_max_physics_steps_per_frame() const { + return max_physics_steps_per_frame; +} + void Engine::set_physics_jitter_fix(double p_threshold) { if (p_threshold < 0) { p_threshold = 0; diff --git a/core/config/engine.h b/core/config/engine.h index 21517e46b7..1b179c5727 100644 --- a/core/config/engine.h +++ b/core/config/engine.h @@ -63,6 +63,7 @@ private: int _max_fps = 0; double _time_scale = 1.0; uint64_t _physics_frames = 0; + int max_physics_steps_per_frame = 8; double _physics_interpolation_fraction = 0.0f; bool abort_on_gpu_errors = false; bool use_validation_layers = false; @@ -93,6 +94,9 @@ public: virtual void set_physics_ticks_per_second(int p_ips); virtual int get_physics_ticks_per_second() const; + virtual void set_max_physics_steps_per_frame(int p_max_physics_steps); + virtual int get_max_physics_steps_per_frame() const; + void set_physics_jitter_fix(double p_threshold); double get_physics_jitter_fix() const; diff --git a/core/core_bind.cpp b/core/core_bind.cpp index 7496ba1979..5c4ed9e483 100644 --- a/core/core_bind.cpp +++ b/core/core_bind.cpp @@ -1471,6 +1471,14 @@ int Engine::get_physics_ticks_per_second() const { return ::Engine::get_singleton()->get_physics_ticks_per_second(); } +void Engine::set_max_physics_steps_per_frame(int p_max_physics_steps) { + ::Engine::get_singleton()->set_max_physics_steps_per_frame(p_max_physics_steps); +} + +int Engine::get_max_physics_steps_per_frame() const { + return ::Engine::get_singleton()->get_max_physics_steps_per_frame(); +} + void Engine::set_physics_jitter_fix(double p_threshold) { ::Engine::get_singleton()->set_physics_jitter_fix(p_threshold); } @@ -1622,6 +1630,8 @@ bool Engine::is_printing_error_messages() const { void Engine::_bind_methods() { ClassDB::bind_method(D_METHOD("set_physics_ticks_per_second", "physics_ticks_per_second"), &Engine::set_physics_ticks_per_second); ClassDB::bind_method(D_METHOD("get_physics_ticks_per_second"), &Engine::get_physics_ticks_per_second); + ClassDB::bind_method(D_METHOD("set_max_physics_steps_per_frame", "max_physics_steps"), &Engine::set_max_physics_steps_per_frame); + ClassDB::bind_method(D_METHOD("get_max_physics_steps_per_frame"), &Engine::get_max_physics_steps_per_frame); ClassDB::bind_method(D_METHOD("set_physics_jitter_fix", "physics_jitter_fix"), &Engine::set_physics_jitter_fix); ClassDB::bind_method(D_METHOD("get_physics_jitter_fix"), &Engine::get_physics_jitter_fix); ClassDB::bind_method(D_METHOD("get_physics_interpolation_fraction"), &Engine::get_physics_interpolation_fraction); @@ -1669,6 +1679,7 @@ void Engine::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "print_error_messages"), "set_print_error_messages", "is_printing_error_messages"); ADD_PROPERTY(PropertyInfo(Variant::INT, "physics_ticks_per_second"), "set_physics_ticks_per_second", "get_physics_ticks_per_second"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "max_physics_steps_per_frame"), "set_max_physics_steps_per_frame", "get_max_physics_steps_per_frame"); ADD_PROPERTY(PropertyInfo(Variant::INT, "max_fps"), "set_max_fps", "get_max_fps"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_scale"), "set_time_scale", "get_time_scale"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "physics_jitter_fix"), "set_physics_jitter_fix", "get_physics_jitter_fix"); diff --git a/core/core_bind.h b/core/core_bind.h index 9261698076..5c7702be0b 100644 --- a/core/core_bind.h +++ b/core/core_bind.h @@ -484,6 +484,9 @@ public: void set_physics_ticks_per_second(int p_ips); int get_physics_ticks_per_second() const; + void set_max_physics_steps_per_frame(int p_max_physics_steps); + int get_max_physics_steps_per_frame() const; + void set_physics_jitter_fix(double p_threshold); double get_physics_jitter_fix() const; double get_physics_interpolation_fraction() const; -- cgit v1.2.3