Skip to content

Commit

Permalink
Fix motion tracker markers for hand-based binoculars
Browse files Browse the repository at this point in the history
  • Loading branch information
fholger committed Aug 18, 2023
1 parent d9d61b2 commit 3363729
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
6 changes: 4 additions & 2 deletions Sources/CryGame C++/Solution1/CryGame/ScriptObjectGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1934,6 +1935,7 @@ int CScriptObjectGame::GetEntitiesScreenSpace(IFunctionHandler *pH)
i++;
}
}

return pH->EndFunction(*pTable);
}

Expand Down
58 changes: 38 additions & 20 deletions Sources/CryGame C++/Solution1/CryGame/VRManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion Sources/CryGame C++/Solution1/CryGame/VRManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;

Expand Down Expand Up @@ -126,7 +130,8 @@ class VRManager

Matrix34 m_fixedHudTransform;

Ang3 m_prevBinocularAngles;
Ang3 m_curBinocularAngles;
Vec3 m_curBinocularPos;

void UpdateHmdTransform();
void ProcessRoomscale();
Expand Down
5 changes: 5 additions & 0 deletions Sources/CryGame C++/Solution1/CryGame/XPlayerCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ void CPlayer::UpdateCamera()
}
}

if (IsMyPlayer())
{
gVR->ModifyBinocularCamera(camera);
}

if (camera)
camera->Update();

Expand Down

0 comments on commit 3363729

Please sign in to comment.