Skip to content

Commit

Permalink
Stabilize weapon scope view
Browse files Browse the repository at this point in the history
  • Loading branch information
fholger committed Jan 11, 2025
1 parent 6949ccd commit 3a75ff6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions Code/HUD/HUD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3314,6 +3314,7 @@ void CHUD::OnPostUpdate(float frameTime)

float x = vScreenSpace.x*width*0.01f;
float y = vScreenSpace.y*height*0.01f;
x = y = 0;

//gEnv->pRenderer->Draw2dLabel(100,125,2,color,false,"%f, %f",x,y);

Expand Down
22 changes: 22 additions & 0 deletions Code/VR/VRManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,16 @@ void VRManager::ModifyCameraFor2D(CCamera& cam)
cam.SetMatrix(weaponView);
}

inline float GetAngleDifference360( float a1, float a2 )
{
float res = a1-a2;
if (res > gf_PI)
res = res - gf_PI2;
else if (res < -gf_PI)
res = gf_PI2 + res;
return res;
}

void VRManager::ModifyWeaponPosition(CPlayer* player, Ang3& weaponAngles, Vec3& weaponPosition, bool slave)
{
if (!g_pGameCVars->vr_enable_motion_controllers
Expand Down Expand Up @@ -447,7 +457,19 @@ void VRManager::ModifyWeaponPosition(CPlayer* player, Ang3& weaponAngles, Vec3&
weaponAngles = Ang3(trackedTransform);

if (weapon->IsZoomed())
{
weaponAngles.y = 0;
// smooth weapon orientation with exponential decay, since otherwise zoom is extremely unstable
IZoomMode* zm = weapon->GetZoomMode(weapon->GetCurrentZoomMode());
float factor = 0.03f * cry_powf(1.f / zm->GetZoomFoVScale(zm->GetCurrentStep()), 1.5f);
Ang3 smoothedAngles = weaponAngles;
float yawPitchDecay = powf(2.f, -gEnv->pSystem->GetITimer()->GetFrameTime() / factor);
smoothedAngles.z = weaponAngles.z + GetAngleDifference360(m_smoothedWeaponAngles.z, weaponAngles.z) * yawPitchDecay;
smoothedAngles.x = weaponAngles.x + GetAngleDifference360(m_smoothedWeaponAngles.x, weaponAngles.x) * yawPitchDecay;
weaponAngles = smoothedAngles;
}

m_smoothedWeaponAngles = weaponAngles;
}

Matrix34 VRManager::GetControllerTransform(int side)
Expand Down
2 changes: 2 additions & 0 deletions Code/VR/VRManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class VRManager
void ReleaseTextureSync(ID3D10Texture2D* target, int key);
void ReleaseTextureSync(ID3D11Texture2D* target, int key);

Ang3 m_smoothedWeaponAngles;

Vec3 m_referencePosition;
float m_referenceYaw = 0;
Matrix34 m_fixedHudTransform;
Expand Down

0 comments on commit 3a75ff6

Please sign in to comment.