Skip to content

Commit

Permalink
Snappy orbit cam (#174)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
johannesugb authored Oct 9, 2023
1 parent 9f64e84 commit c5451cf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion auto_vk
43 changes: 34 additions & 9 deletions auto_vk_toolkit/src/orbit_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions visual_studio/auto_vk_toolkit/auto_vk_toolkit.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@
<PreprocessorDefinitions>NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>false</ConformanceMode>
<PrecompiledHeaderFile>cg_stdafx.hpp</PrecompiledHeaderFile>
<LanguageStandard>stdcpplatest</LanguageStandard>
<LanguageStandard>stdcpp20</LanguageStandard>
<ForcedIncludeFiles>cg_stdafx.hpp</ForcedIncludeFiles>
<RuntimeTypeInfo>true</RuntimeTypeInfo>
<TreatSpecificWarningsAsErrors>4715</TreatSpecificWarningsAsErrors>
Expand All @@ -556,7 +556,7 @@
<PreprocessorDefinitions>NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>false</ConformanceMode>
<PrecompiledHeaderFile>cg_stdafx.hpp</PrecompiledHeaderFile>
<LanguageStandard>stdcpplatest</LanguageStandard>
<LanguageStandard>stdcpp20</LanguageStandard>
<ForcedIncludeFiles>cg_stdafx.hpp</ForcedIncludeFiles>
<RuntimeTypeInfo>true</RuntimeTypeInfo>
<TreatSpecificWarningsAsErrors>4715</TreatSpecificWarningsAsErrors>
Expand Down

0 comments on commit c5451cf

Please sign in to comment.