From c5451cf8fab787ea2abd293bc974128285283e5b Mon Sep 17 00:00:00 2001 From: Johannes Unterguggenberger Date: Mon, 9 Oct 2023 13:54:44 +0200 Subject: [PATCH] Snappy orbit cam (#174) * window::renderpass() -> window::get_renderpass() to make it compile with GCC. I wonder if there is a way around this. * In an attept to make the orbit_camera better, it is snapping to the same positions when moving in/out via scrolling * C++20, not latest, also latest Auto-Vk * Removed debug logs from orbit_camera * Trying to make orbit cam better: Zoom speed is now based on pivot distance --- auto_vk | 2 +- auto_vk_toolkit/src/orbit_camera.cpp | 43 +++++++++++++++---- .../auto_vk_toolkit/auto_vk_toolkit.vcxproj | 4 +- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/auto_vk b/auto_vk index a79bf351..2be07e68 160000 --- a/auto_vk +++ b/auto_vk @@ -1 +1 @@ -Subproject commit a79bf351b17bb4f577336e354706d8786ac07fd8 +Subproject commit 2be07e6834d1617995414141710fb897ac344a03 diff --git a/auto_vk_toolkit/src/orbit_camera.cpp b/auto_vk_toolkit/src/orbit_camera.cpp index b40a89f1..a3ba4d55 100644 --- a/auto_vk_toolkit/src/orbit_camera.cpp +++ b/auto_vk_toolkit/src/orbit_camera.cpp @@ -12,7 +12,7 @@ namespace avk , mPivotDistanceSpeed{ .5f } , mMinPivotDistance{ 1.f } , mMaxPivotDistance{ 30.f } - , mPivotDistanceSlowDownRange{ 10.f } + , mPivotDistanceSlowDownRange{ 3.333f } , mLateralSpeed{ 1.f } , mFastMultiplier(6.0f) , mSlowMultiplier(0.2f) @@ -86,26 +86,51 @@ namespace avk const auto moveCloser = scrollDist > 0.f; const auto moveAway = scrollDist < 0.f; + auto getMoveSpeed = [this](float x) { + x = glm::round(x * 20.f) / 20.f; + auto spd = glm::smoothstep(mMinPivotDistance, mMinPivotDistance + mPivotDistanceSlowDownRange, x) * glm::smoothstep(mMaxPivotDistance, mMaxPivotDistance - mPivotDistanceSlowDownRange, x); + return spd; + }; + if (moveCloser) { - auto len = glm::smoothstep(mMinPivotDistance, mMinPivotDistance + mPivotDistanceSlowDownRange, mPivotDistance); - auto move = front(*this) * len * pivDistSpeed; + auto spd = getMoveSpeed(mPivotDistance); + if (mPivotDistance - mMinPivotDistance > mMaxPivotDistance - mPivotDistance) { + // try to match the moveAway speed + auto candidate = mPivotDistance - spd * pivDistSpeed; + for (int safety = 0; safety < 10 && mPivotDistance - candidate - getMoveSpeed(candidate) * pivDistSpeed < -1e-5; ++safety) { + spd = getMoveSpeed(candidate); + candidate = mPivotDistance - spd * pivDistSpeed; + } + } + auto move = front(*this) * spd * pivDistSpeed; translate(*this, move); - mPivotDistance -= len * pivDistSpeed; + mPivotDistance -= spd * pivDistSpeed; calculate_lateral_speed(); } if (moveAway) { - auto len = glm::smoothstep(mMaxPivotDistance, mMaxPivotDistance - mPivotDistanceSlowDownRange, mPivotDistance); - auto move = back(*this) * len * pivDistSpeed; + auto spd = getMoveSpeed(mPivotDistance); + if (mPivotDistance - mMinPivotDistance < mMaxPivotDistance - mPivotDistance) { + // try to match the moveCloser speed + auto candidate = mPivotDistance + spd * pivDistSpeed; + for (int safety = 0; safety < 10 && candidate - getMoveSpeed(candidate) * pivDistSpeed - mPivotDistance < -1e-5; ++safety) { + spd = getMoveSpeed(candidate); + candidate = mPivotDistance + spd * pivDistSpeed; + } + } + auto move = back(*this) * spd * pivDistSpeed; translate(*this, move); - mPivotDistance += len * pivDistSpeed; - calculate_lateral_speed(); + mPivotDistance += spd * pivDistSpeed; + calculate_lateral_speed(); } } } void orbit_camera::set_pivot_distance(float aDistanceFromCamera) { - mPivotDistance = glm::clamp(aDistanceFromCamera, mMinPivotDistance, mMaxPivotDistance); + mPivotDistance = aDistanceFromCamera; calculate_lateral_speed(); + mPivotDistanceSpeed = mPivotDistance / 20.f; + mPivotDistanceSlowDownRange = mPivotDistance / 3.f; + mMaxPivotDistance = mPivotDistance * 3.f; } float orbit_camera::pivot_distance() const { diff --git a/visual_studio/auto_vk_toolkit/auto_vk_toolkit.vcxproj b/visual_studio/auto_vk_toolkit/auto_vk_toolkit.vcxproj index 9f5d7c29..40f92bf9 100644 --- a/visual_studio/auto_vk_toolkit/auto_vk_toolkit.vcxproj +++ b/visual_studio/auto_vk_toolkit/auto_vk_toolkit.vcxproj @@ -530,7 +530,7 @@ NDEBUG;_LIB;%(PreprocessorDefinitions) false cg_stdafx.hpp - stdcpplatest + stdcpp20 cg_stdafx.hpp true 4715 @@ -556,7 +556,7 @@ NDEBUG;_LIB;%(PreprocessorDefinitions) false cg_stdafx.hpp - stdcpplatest + stdcpp20 cg_stdafx.hpp true 4715