From 3363729b0eaf6799a69593348a64dc3d4f1416cc Mon Sep 17 00:00:00 2001 From: Holger Frydrych Date: Fri, 18 Aug 2023 23:11:17 +0200 Subject: [PATCH] Fix motion tracker markers for hand-based binoculars --- .../Solution1/CryGame/ScriptObjectGame.cpp | 6 +- .../Solution1/CryGame/VRManager.cpp | 58 ++++++++++++------- .../CryGame C++/Solution1/CryGame/VRManager.h | 7 ++- .../Solution1/CryGame/XPlayerCamera.cpp | 5 ++ 4 files changed, 53 insertions(+), 23 deletions(-) diff --git a/Sources/CryGame C++/Solution1/CryGame/ScriptObjectGame.cpp b/Sources/CryGame C++/Solution1/CryGame/ScriptObjectGame.cpp index 375a469..802617c 100644 --- a/Sources/CryGame C++/Solution1/CryGame/ScriptObjectGame.cpp +++ b/Sources/CryGame C++/Solution1/CryGame/ScriptObjectGame.cpp @@ -1828,6 +1828,7 @@ int CScriptObjectGame::GetEntitiesScreenSpace(IFunctionHandler *pH) IEntityItPtr It=m_pSystem->GetIEntitySystem()->GetEntityInFrustrumIterator(); CCamera Cam=m_pSystem->GetViewCamera(); Vec3 CamVec=Cam.GetAngles(); + Vec3 CamPos = Cam.GetPos(); CamVec=ConvertToRadAngles(CamVec); int i=1; IEntity *pEnt; @@ -1896,11 +1897,11 @@ int CScriptObjectGame::GetEntitiesScreenSpace(IFunctionHandler *pH) } } - Vec3 diff(Center-Cam.GetPos()); + Vec3 diff(Center-CamPos); if(GetLengthSquared(diff)>700*700) continue; - if (m_pSystem->GetIPhysicalWorld()->RayWorldIntersection(vectorf(Cam.GetPos()), diff, + if (m_pSystem->GetIPhysicalWorld()->RayWorldIntersection(vectorf(CamPos), diff, ent_terrain|ent_static,0, &RayHit, 1,pPE)) continue; m_pSystem->GetIRenderer()->ProjectToScreen(Center.x, Center.y, Center.z, &px, &py, &pz); @@ -1934,6 +1935,7 @@ int CScriptObjectGame::GetEntitiesScreenSpace(IFunctionHandler *pH) i++; } } + return pH->EndFunction(*pTable); } diff --git a/Sources/CryGame C++/Solution1/CryGame/VRManager.cpp b/Sources/CryGame C++/Solution1/CryGame/VRManager.cpp index 9dbbba8..5c92b23 100644 --- a/Sources/CryGame C++/Solution1/CryGame/VRManager.cpp +++ b/Sources/CryGame C++/Solution1/CryGame/VRManager.cpp @@ -523,6 +523,12 @@ void VRManager::Modify2DCamera(CCamera& cam) return; } + if (m_pGame->AreBinocularsActive()) + { + // already corrected in player cam by necessity - otherwise, the motion tracking markers just don't display at the right position + return; + } + Ang3 angles = cam.GetAngles(); Vec3 position = cam.GetPos(); @@ -551,27 +557,39 @@ void VRManager::Modify2DCamera(CCamera& cam) cam.SetPos(muzzlePos); cam.SetAngle(muzzleAngles); } +} - if (m_pGame->AreBinocularsActive()) - { - // set camera to off hand position, instead - Matrix34 offset = Matrix34::CreateTranslationMat(Vec3(-BinocularWidth / 2, 0, BinocularWidth / 2)); - Matrix34 controllerTransform = GetControllerTransform(m_pGame->g_LeftHanded->GetIVal() == 1 ? 1 : 0); - modifiedViewMat = viewMat * controllerTransform * offset; - cam.SetPos(modifiedViewMat.GetTranslation()); - angles.SetAnglesXYZ(Matrix33(modifiedViewMat)); - angles.Rad2Deg(); - - // smooth rotation for a more stable zoom - Vec3 smoothedAngles = angles; - float factor = 0.025 * (DEFAULT_FOV / cam.GetFov()); - float yawPitchDecay = powf(2.f, -m_pGame->GetSystem()->GetITimer()->GetFrameTime() / factor); - smoothedAngles.z = angles.z + GetAngleDifference360(m_prevBinocularAngles.z, angles.z) * yawPitchDecay; - smoothedAngles.x = angles.x + GetAngleDifference360(m_prevBinocularAngles.x, angles.x) * yawPitchDecay; - m_prevBinocularAngles = smoothedAngles; - - cam.SetAngle(smoothedAngles); - } +void VRManager::ModifyBinocularCamera(IEntityCamera* cam) +{ + if (!cam || !UseMotionControllers() || !m_pGame->AreBinocularsActive()) + return; + + Ang3 angles = cam->GetAngles(); + Vec3 position = cam->GetPos(); + angles = Deg2Rad(angles); + // eliminate pitch and roll + angles.y = 0; + angles.x = 0; + Matrix34 viewMat = Matrix34::CreateRotationXYZ(angles, position); + + // set camera to off hand position, instead + Matrix34 offset = Matrix34::CreateTranslationMat(Vec3(-BinocularWidth / 2, 0, BinocularWidth / 2)); + Matrix34 controllerTransform = GetControllerTransform(m_pGame->g_LeftHanded->GetIVal() == 1 ? 1 : 0); + Matrix34 modifiedViewMat = viewMat * controllerTransform * offset; + m_curBinocularPos = modifiedViewMat.GetTranslation(); + cam->SetPos(m_curBinocularPos); + angles.SetAnglesXYZ(Matrix33(modifiedViewMat)); + angles.Rad2Deg(); + + // smooth rotation for a more stable zoom + Vec3 smoothedAngles = angles; + float factor = 0.025 * (DEFAULT_FOV / cam->GetFov()); + float yawPitchDecay = powf(2.f, -m_pGame->GetSystem()->GetITimer()->GetFrameTime() / factor); + smoothedAngles.z = angles.z + GetAngleDifference360(m_curBinocularAngles.z, angles.z) * yawPitchDecay; + smoothedAngles.x = angles.x + GetAngleDifference360(m_curBinocularAngles.x, angles.x) * yawPitchDecay; + m_curBinocularAngles = smoothedAngles; + + cam->SetAngles(smoothedAngles); } void VRManager::GetEffectiveRenderLimits(int eye, float* left, float* right, float* top, float* bottom) diff --git a/Sources/CryGame C++/Solution1/CryGame/VRManager.h b/Sources/CryGame C++/Solution1/CryGame/VRManager.h index f675644..3046517 100644 --- a/Sources/CryGame C++/Solution1/CryGame/VRManager.h +++ b/Sources/CryGame C++/Solution1/CryGame/VRManager.h @@ -38,6 +38,7 @@ class VRManager void ModifyViewCamera(int eye, CCamera& cam); void Modify2DCamera(CCamera& cam); + void ModifyBinocularCamera(IEntityCamera* cam); void GetEffectiveRenderLimits(int eye, float* left, float* right, float* top, float* bottom); @@ -59,6 +60,9 @@ class VRManager VRHaptics* GetHaptics() { return &m_vrHaptics; } + const Vec3& GetBinocularAngles() const { return m_curBinocularAngles; } + const Vec3& GetBinocularPos() const { return m_curBinocularPos; } + private: struct D3DResources; @@ -126,7 +130,8 @@ class VRManager Matrix34 m_fixedHudTransform; - Ang3 m_prevBinocularAngles; + Ang3 m_curBinocularAngles; + Vec3 m_curBinocularPos; void UpdateHmdTransform(); void ProcessRoomscale(); diff --git a/Sources/CryGame C++/Solution1/CryGame/XPlayerCamera.cpp b/Sources/CryGame C++/Solution1/CryGame/XPlayerCamera.cpp index a3ec1df..ba074d6 100644 --- a/Sources/CryGame C++/Solution1/CryGame/XPlayerCamera.cpp +++ b/Sources/CryGame C++/Solution1/CryGame/XPlayerCamera.cpp @@ -308,6 +308,11 @@ void CPlayer::UpdateCamera() } } + if (IsMyPlayer()) + { + gVR->ModifyBinocularCamera(camera); + } + if (camera) camera->Update();