From af98ec6c89ab7e23e47f0f193957aa46a8dd2350 Mon Sep 17 00:00:00 2001 From: docentYT <63965954+docentYT@users.noreply.github.com> Date: Tue, 3 Feb 2026 00:09:11 +0100 Subject: [PATCH 1/5] ComputeAxisSpeed function --- Camera.cpp | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/Camera.cpp b/Camera.cpp index 9957fe5b..0377dfab 100644 --- a/Camera.cpp +++ b/Camera.cpp @@ -42,6 +42,15 @@ void TCamera::OnCursorMove(double x, double y) { m_rotationoffsets += glm::dvec3 { y, x, 0.0 }; } +static double ComputeAxisSpeed(double param, double walkspeed, double maxspeed, double threshold) { + double absval = std::abs(param); + // 2/3rd of the stick range lerps walk speed, past that we lerp between max walk and run speed + double walk = walkspeed * std::min(absval / threshold, 1.0); + double run = (std::max(0.0, absval - threshold) / (1.0 - threshold)) * std::max(0.0, maxspeed - walkspeed); + return (param >= 0.0 ? 1.0 : -1.0) * (walk + run); +} + + bool TCamera::OnCommand( command_data const &Command ) { @@ -78,26 +87,9 @@ TCamera::OnCommand( command_data const &Command ) { 1.0 ); // left-right - auto const movexparam { Command.param1 }; - // 2/3rd of the stick range lerps walk speed, past that we lerp between max walk and run speed - auto const movex { walkspeed * std::min(std::abs(movexparam) * (1.0 / stickthreshold), 1.0) - + ( std::max( 0.0, std::abs( movexparam ) - stickthreshold ) / (1.0 - stickthreshold) ) * std::max( 0.0, movespeed - walkspeed ) }; - - m_moverate.x = ( - movexparam > 0.0 ? movex * speedmultiplier : - movexparam < 0.0 ? -movex * speedmultiplier : - 0.0 ); - + m_moverate.x = ComputeAxisSpeed(Command.param1, walkspeed, movespeed, stickthreshold) * speedmultiplier; // forward-back - double const movezparam { Command.param2 }; - auto const movez { walkspeed * std::min(std::abs(movezparam) * (1.0 / stickthreshold), 1.0) - + ( std::max( 0.0, std::abs( movezparam ) - stickthreshold ) / (1.0 - stickthreshold) ) * std::max( 0.0, movespeed - walkspeed ) }; - - // NOTE: z-axis is flipped given world coordinate system - m_moverate.z = ( - movezparam > 0.0 ? -movez * speedmultiplier : - movezparam < 0.0 ? movez * speedmultiplier : - 0.0 ); + m_moverate.z = -ComputeAxisSpeed(Command.param2, walkspeed, movespeed, stickthreshold) * speedmultiplier; break; } From 0fdbc5cc42d4d41218998f3fc22c973937fb489b Mon Sep 17 00:00:00 2001 From: docentYT <63965954+docentYT@users.noreply.github.com> Date: Tue, 3 Feb 2026 00:11:03 +0100 Subject: [PATCH 2/5] std::remainder instead of not optimal while loops --- Camera.cpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Camera.cpp b/Camera.cpp index 0377dfab..31ba67d4 100644 --- a/Camera.cpp +++ b/Camera.cpp @@ -152,12 +152,7 @@ void TCamera::Update() Angle.y -= m_rotationoffsets.y * rotationfactor; m_rotationoffsets.y *= ( 1.0 - rotationfactor ); - while( Angle.y > M_PI ) { - Angle.y -= 2 * M_PI; - } - while( Angle.y < -M_PI ) { - Angle.y += 2 * M_PI; - } + Angle.y = std::remainder(Angle.y, 2.0 * M_PI); // Limit the camera pitch to +/- 90°. Angle.x = clamp(Angle.x - (m_rotationoffsets.x * rotationfactor), -M_PI_2, M_PI_2); From 3ecfbd33ea6462666cde2f54237964b2a91b3678 Mon Sep 17 00:00:00 2001 From: docentYT <63965954+docentYT@users.noreply.github.com> Date: Tue, 3 Feb 2026 00:14:12 +0100 Subject: [PATCH 3/5] UpdateVelocityAxis static function --- Camera.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Camera.cpp b/Camera.cpp index 31ba67d4..623a7d33 100644 --- a/Camera.cpp +++ b/Camera.cpp @@ -132,6 +132,12 @@ TCamera::OnCommand( command_data const &Command ) { return iscameracommand; } +static void UpdateVelocityAxis(double& velocity, double moverate, double deltatime) +{ + velocity = clamp(velocity + moverate * 10.0 * deltatime, -std::abs(moverate), std::abs(moverate)); +} + + void TCamera::Update() { // check for sent user commands @@ -164,9 +170,9 @@ void TCamera::Update() || ( true == DebugCameraFlag ) ) { // ctrl is used for mirror view, so we ignore the controls when in vehicle if ctrl is pressed // McZapkie-170402: poruszanie i rozgladanie we free takie samo jak w follow - Velocity.x = clamp( Velocity.x + m_moverate.x * 10.0 * deltatime, -std::abs( m_moverate.x ), std::abs( m_moverate.x ) ); - Velocity.z = clamp( Velocity.z + m_moverate.z * 10.0 * deltatime, -std::abs( m_moverate.z ), std::abs( m_moverate.z ) ); - Velocity.y = clamp( Velocity.y + m_moverate.y * 10.0 * deltatime, -std::abs( m_moverate.y ), std::abs( m_moverate.y ) ); + UpdateVelocityAxis(Velocity.x, m_moverate.x, deltatime); + UpdateVelocityAxis(Velocity.y, m_moverate.y, deltatime); + UpdateVelocityAxis(Velocity.z, m_moverate.z, deltatime); } if( ( m_owner == nullptr ) || ( true == DebugCameraFlag ) ) { From 62b98abecdc0cc5cead8ac3d98aeeff7d3b8959a Mon Sep 17 00:00:00 2001 From: docentYT <63965954+docentYT@users.noreply.github.com> Date: Tue, 3 Feb 2026 00:41:31 +0100 Subject: [PATCH 4/5] ComputeAxisSpeed for m_moverate.y --- Camera.cpp | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Camera.cpp b/Camera.cpp index 623a7d33..28f489e0 100644 --- a/Camera.cpp +++ b/Camera.cpp @@ -109,15 +109,7 @@ TCamera::OnCommand( command_data const &Command ) { 10.0 : 1.0 ); // up-down - auto const moveyparam { Command.param1 }; - // 2/3rd of the stick range lerps walk speed, past that we lerp between max walk and run speed - auto const movey { walkspeed * std::min(std::abs(moveyparam) * (1.0 / stickthreshold), 1.0) - + ( std::max( 0.0, std::abs( moveyparam ) - stickthreshold ) / (1.0 - stickthreshold) ) * std::max( 0.0, movespeed - walkspeed ) }; - - m_moverate.y = ( - moveyparam > 0.0 ? movey * speedmultiplier : - moveyparam < 0.0 ? -movey * speedmultiplier : - 0.0 ); + m_moverate.y = ComputeAxisSpeed(Command.param1, walkspeed, movespeed, stickthreshold) * speedmultiplier; break; } From c545da2c9783c978f64ec66a4a12aba1bc8d6a39 Mon Sep 17 00:00:00 2001 From: docentYT <63965954+docentYT@users.noreply.github.com> Date: Tue, 3 Feb 2026 00:50:05 +0100 Subject: [PATCH 5/5] Make OnCursorMove simpler --- Camera.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Camera.cpp b/Camera.cpp index 28f489e0..6b46fa6c 100644 --- a/Camera.cpp +++ b/Camera.cpp @@ -38,8 +38,8 @@ void TCamera::Reset() { void TCamera::OnCursorMove(double x, double y) { - - m_rotationoffsets += glm::dvec3 { y, x, 0.0 }; + m_rotationoffsets.x += y; + m_rotationoffsets.y += x; } static double ComputeAxisSpeed(double param, double walkspeed, double maxspeed, double threshold) {