metaforce/Runtime/Camera/CFirstPersonCamera.cpp

379 lines
14 KiB
C++
Raw Normal View History

#include "CFirstPersonCamera.hpp"
#include "GameGlobalObjects.hpp"
#include "CStateManager.hpp"
#include "World/CPlayer.hpp"
2016-09-16 22:21:19 +00:00
#include "World/CScriptGrapplePoint.hpp"
2017-10-07 05:32:11 +00:00
#include "World/CScriptCameraPitchVolume.hpp"
2017-01-15 03:07:01 +00:00
#include "TCastTo.hpp"
namespace urde
{
2017-08-19 06:52:13 +00:00
CFirstPersonCamera::CFirstPersonCamera(TUniqueId uid, const zeus::CTransform& xf, TUniqueId watchedObj,
float orbitCameraSpeed, float fov, float nearz, float farz, float aspect)
2016-09-16 22:21:19 +00:00
: CGameCamera(uid, true, "First Person Camera", CEntityInfo(kInvalidAreaId, CEntity::NullConnectionList), xf, fov,
2017-10-07 05:32:11 +00:00
nearz, farz, aspect, watchedObj, false, 0), x188_orbitCameraSpeed(orbitCameraSpeed),
x190_gunFollowXf(xf)
{
2017-10-07 05:32:11 +00:00
x1c6_24_deferBallTransitionProcessing = false;
}
2017-03-24 05:30:16 +00:00
void CFirstPersonCamera::Accept(IVisitor& visitor)
{
visitor.Visit(this);
}
2017-10-07 05:32:11 +00:00
void CFirstPersonCamera::PreThink(float dt, CStateManager& mgr)
2017-02-18 02:19:50 +00:00
{
2017-10-07 05:32:11 +00:00
// Empty
}
2017-02-18 02:19:50 +00:00
2017-10-07 05:32:11 +00:00
void CFirstPersonCamera::Think(float dt, CStateManager& mgr)
{
if (TCastToPtr<CPlayer> player = mgr.ObjectById(xe8_watchedObject))
{
if (!x1c6_24_deferBallTransitionProcessing)
{
if (player->GetMorphballTransitionState() == CPlayer::EPlayerMorphBallState::Morphed)
{
if (player->GetCameraState() != CPlayer::EPlayerCameraState::Spawned)
return;
SetTransform(player->CreateTransformFromMovementDirection());
SetTranslation(player->GetEyePosition());
return;
}
if (player->GetMorphballTransitionState() != CPlayer::EPlayerMorphBallState::Unmorphed)
{
if (player->GetMorphballTransitionState() != CPlayer::EPlayerMorphBallState::Unmorphing)
return;
2017-10-15 05:38:10 +00:00
if (std::fabs(player->GetMorphFactor() - 1.f) >= 0.00001f)
2017-10-07 05:32:11 +00:00
return;
}
}
else
{
x1c6_24_deferBallTransitionProcessing = false;
}
zeus::CTransform backupXf = x34_transform;
UpdateElevation(mgr);
UpdateTransform(mgr, dt);
SetTransform(ValidateCameraTransform(x34_transform, backupXf));
if (x1d4_closeInTimer > 0.f)
x1d4_closeInTimer -= dt;
}
2017-02-18 02:19:50 +00:00
}
2017-10-07 05:32:11 +00:00
void CFirstPersonCamera::ProcessInput(const CFinalInput&, CStateManager& mgr)
{
// Empty
}
2017-10-07 05:32:11 +00:00
void CFirstPersonCamera::Reset(const zeus::CTransform& xf, CStateManager& mgr)
{
SetTransform(xf);
SetTranslation(mgr.GetPlayer().GetEyePosition());
x190_gunFollowXf = x34_transform;
}
2017-02-18 02:19:50 +00:00
void CFirstPersonCamera::SkipCinematic()
{
2017-10-07 05:32:11 +00:00
x1c8_closeInVec = zeus::CVector3f::skZero;
x1d4_closeInTimer = 0.f;
}
2016-09-16 22:21:19 +00:00
void CFirstPersonCamera::CalculateGunFollowOrientationAndTransform(zeus::CTransform& gunXf, zeus::CQuaternion& gunQ,
float dt, zeus::CVector3f& rVec)
{
2016-09-16 22:29:38 +00:00
zeus::CVector3f gunFrontVec = x190_gunFollowXf.frontVector();
gunFrontVec.z = 0.f;
if (gunFrontVec.canBeNormalized())
gunFrontVec.normalize();
2016-09-16 22:21:19 +00:00
zeus::CVector3f rVecNoZ = rVec;
rVecNoZ.z = 0.f;
if (rVecNoZ.canBeNormalized())
rVecNoZ.normalize();
2017-12-16 00:19:15 +00:00
gunXf = zeus::CQuaternion::lookAt(gunFrontVec, rVecNoZ, 2.f * M_PIF).toTransform() *
2017-11-30 01:22:46 +00:00
x190_gunFollowXf.getRotation();
2016-09-16 22:21:19 +00:00
2016-09-16 22:29:38 +00:00
zeus::CVector3f newgunFront = gunXf.frontVector();
if (newgunFront.canBeNormalized())
newgunFront.normalize();
2016-09-16 22:21:19 +00:00
2016-09-16 22:29:38 +00:00
float angle = newgunFront.dot(rVec);
2017-12-16 00:19:15 +00:00
angle = zeus::clamp(-1.f, angle, 1.f);
2017-11-30 01:22:46 +00:00
gunQ = zeus::CQuaternion::lookAt(newgunFront, rVec, zeus::clamp(0.f, std::acos(angle) / dt, 1.f) * dt);
}
2016-09-16 22:21:19 +00:00
void CFirstPersonCamera::UpdateTransform(CStateManager& mgr, float dt)
{
2017-01-15 03:59:37 +00:00
TCastToPtr<CPlayer> player(mgr.ObjectById(GetWatchedObject()));
if (!player)
{
2017-08-18 01:30:15 +00:00
SetTransform(zeus::CTransform::Identity());
return;
}
zeus::CTransform playerXf = player->GetTransform();
2016-09-16 22:21:19 +00:00
zeus::CVector3f rVec =
2017-10-07 05:32:11 +00:00
playerXf.rotate({0.f, std::min(std::fabs(std::cos(x1c0_pitch)), 1.0f), std::min(std::fabs(std::sin(x1c0_pitch)), 1.0f)});
2017-04-07 05:35:09 +00:00
if (player->x3dc_inFreeLook)
2016-09-16 22:21:19 +00:00
{
2017-07-17 03:04:14 +00:00
float angle = player->x3ec_freeLookPitchAngle;
2017-10-07 05:32:11 +00:00
float angleClamp = g_tweakPlayer->GetVerticalFreeLookAngleVel() - std::fabs(x1c0_pitch);
2017-08-19 06:52:13 +00:00
angle = zeus::clamp(-angleClamp, angle, angleClamp);
2016-09-16 22:21:19 +00:00
zeus::CVector3f vec;
vec.z = std::sin(angle);
vec.y = std::cos(-player->x3e4_freeLookYawAngle) * std::cos(angle);
vec.x = std::sin(-player->x3e4_freeLookYawAngle) * std::cos(angle);
2017-11-30 01:22:46 +00:00
if (g_tweakPlayer->GetFreeLookTurnsPlayer())
{
vec.x = 0.f;
if (!zeus::close_enough(vec, zeus::CVector3f::skZero))
vec.normalize();
}
2016-09-16 22:21:19 +00:00
2017-12-16 00:19:15 +00:00
rVec = zeus::CQuaternion::lookAt({0.f, 1.f, 0.f}, rVec, 2.f * M_PIF).transform(vec);
2016-09-16 22:21:19 +00:00
}
zeus::CVector3f eyePos = player->GetEyePosition();
2017-10-07 05:32:11 +00:00
if (x1d4_closeInTimer > 0.f)
2016-09-16 22:21:19 +00:00
{
2017-10-07 05:32:11 +00:00
eyePos += zeus::clamp(0.f, 0.5f * x1d4_closeInTimer, 1.f) * x1c8_closeInVec;
2016-09-16 22:21:19 +00:00
player->GetCameraBob()->ResetCameraBobTime();
player->GetCameraBob()->SetCameraBobTransform(zeus::CTransform::Identity());
}
2017-11-30 01:22:46 +00:00
switch (player->GetOrbitState())
{
case CPlayer::EPlayerOrbitState::ForcedOrbitObject:
case CPlayer::EPlayerOrbitState::OrbitObject:
2016-09-16 22:21:19 +00:00
{
2017-06-12 04:23:34 +00:00
const CActor* act = TCastToConstPtr<CActor>(mgr.GetObjectById(player->x310_orbitTargetId));
2017-11-30 01:22:46 +00:00
if (act && act->GetMaterialList().HasMaterial(EMaterialTypes::Orbit))
2016-09-16 22:21:19 +00:00
{
2017-06-12 04:23:34 +00:00
zeus::CVector3f v = player->x314_orbitPoint.y - eyePos;
2016-09-16 22:21:19 +00:00
if (v.canBeNormalized())
v.normalize();
rVec = v;
}
2017-11-30 01:22:46 +00:00
else
{
rVec = player->x314_orbitPoint - eyePos;
}
break;
2016-09-16 22:21:19 +00:00
}
2017-11-30 01:22:46 +00:00
case CPlayer::EPlayerOrbitState::OrbitPoint:
case CPlayer::EPlayerOrbitState::OrbitCarcass:
2016-09-16 22:21:19 +00:00
{
2017-11-30 01:22:46 +00:00
if (!player->x3dd_lookButtonHeld)
2016-09-16 22:21:19 +00:00
{
2017-11-30 01:22:46 +00:00
rVec = player->x314_orbitPoint - eyePos;
2016-09-16 22:21:19 +00:00
}
2017-11-30 01:22:46 +00:00
break;
}
case CPlayer::EPlayerOrbitState::NoOrbit:
{
if (player->GetMorphballTransitionState() == CPlayer::EPlayerMorphBallState::Unmorphed &&
!player->x3dc_inFreeLook && x1c4_pitchId == kInvalidUniqueId)
2016-09-16 22:21:19 +00:00
{
2017-11-30 01:22:46 +00:00
if (player->x294_jumpCameraTimer > 0.f)
{
float angle = zeus::clamp(0.f, (player->x294_jumpCameraTimer - g_tweakPlayer->GetJumpCameraPitchDownStart()) /
g_tweakPlayer->GetJumpCameraPitchDownFull(), 1.f) *
g_tweakPlayer->GetJumpCameraPitchDownAngle();
angle += x1c0_pitch;
rVec.x = 0.f;
rVec.y = std::cos(angle);
rVec.z = -std::sin(angle);
rVec = playerXf.rotate(rVec);
}
else if (player->x29c_fallCameraTimer > 0.f)
{
float angle = zeus::clamp(0.f, (player->x29c_fallCameraTimer - g_tweakPlayer->GetFallCameraPitchDownStart()) /
g_tweakPlayer->GetFallCameraPitchDownFull(), 1.f) *
g_tweakPlayer->GetFallCameraPitchDownAngle();
2017-12-16 00:19:15 +00:00
rVec.x = 0.f;
rVec.y = std::cos(angle);
rVec.z = -std::sin(angle);
2017-11-30 01:22:46 +00:00
2017-12-16 00:19:15 +00:00
rVec = playerXf.rotate(rVec);
2017-11-30 01:22:46 +00:00
}
2016-09-16 22:21:19 +00:00
}
2017-11-30 01:22:46 +00:00
break;
}
default:
break;
2016-09-16 22:21:19 +00:00
}
if (rVec.canBeNormalized())
rVec.normalize();
zeus::CTransform gunXf = x190_gunFollowXf;
zeus::CQuaternion qGun = zeus::CQuaternion::skNoRotation;
2017-11-30 01:22:46 +00:00
if (!player->x3dc_inFreeLook)
{
2017-11-30 01:22:46 +00:00
switch (player->GetOrbitState())
2016-09-16 22:21:19 +00:00
{
2017-11-30 01:22:46 +00:00
default:
{
CalculateGunFollowOrientationAndTransform(gunXf, qGun,
dt * g_tweakPlayer->GetFirstPersonCameraSpeed(), rVec);
break;
}
case CPlayer::EPlayerOrbitState::Grapple:
{
CalculateGunFollowOrientationAndTransform(gunXf, qGun,
dt * g_tweakPlayer->GetGrappleCameraSpeed(), rVec);
break;
}
case CPlayer::EPlayerOrbitState::OrbitPoint:
case CPlayer::EPlayerOrbitState::OrbitCarcass:
{
CalculateGunFollowOrientationAndTransform(gunXf, qGun,
dt * g_tweakPlayer->GetOrbitCameraSpeed() * 0.25f, rVec);
break;
}
case CPlayer::EPlayerOrbitState::ForcedOrbitObject:
case CPlayer::EPlayerOrbitState::OrbitObject:
{
zeus::CVector3f gunFrontVec = x190_gunFollowXf.frontVector();
2016-09-16 22:21:19 +00:00
2016-09-16 22:29:38 +00:00
if (gunFrontVec.canBeNormalized())
gunFrontVec.normalize();
2016-09-16 22:21:19 +00:00
2017-08-19 06:52:13 +00:00
float scaledDt = (dt * g_tweakPlayer->GetOrbitCameraSpeed());
2016-09-16 22:29:38 +00:00
float angle = gunFrontVec.dot(rVec);
2017-08-19 06:52:13 +00:00
angle = zeus::clamp(-1.f, angle, 1.f);
2016-09-16 22:21:19 +00:00
float clampedAngle = zeus::clamp(0.f, std::acos(angle) / scaledDt, 1.f);
2017-11-30 01:22:46 +00:00
if (angle > 0.999f || x18c_lockCamera || player->x374_orbitLockEstablished)
2017-12-16 00:19:15 +00:00
qGun = zeus::CQuaternion::lookAt(gunFrontVec, rVec, 2.f * M_PIF);
2016-09-16 22:21:19 +00:00
else
2017-11-30 01:22:46 +00:00
qGun = zeus::CQuaternion::lookAt(gunFrontVec, rVec, scaledDt * clampedAngle);
2016-09-16 22:21:19 +00:00
const CScriptGrapplePoint* gPoint =
2017-06-12 04:23:34 +00:00
TCastToConstPtr<CScriptGrapplePoint>(mgr.GetObjectById(player->x310_orbitTargetId));
2017-08-19 06:52:13 +00:00
if (gPoint && player->x29c_fallCameraTimer > 0.f)
2016-09-16 22:21:19 +00:00
{
2016-09-16 22:29:38 +00:00
gunFrontVec = x190_gunFollowXf.frontVector();
if (gunFrontVec.canBeNormalized())
gunFrontVec.normalize();
2016-09-16 22:21:19 +00:00
zeus::CVector3f rVecCpy = rVec;
2017-11-30 01:22:46 +00:00
rVecCpy.z = 0.f;
2016-09-16 22:21:19 +00:00
if (rVecCpy.canBeNormalized())
rVecCpy.normalize();
2017-01-15 03:59:37 +00:00
gunXf =
2017-12-16 00:19:15 +00:00
zeus::CQuaternion::lookAt(gunFrontVec, rVecCpy, 2.f * M_PIF).toTransform() *
2017-01-15 03:59:37 +00:00
x190_gunFollowXf.getRotation();
2016-09-16 22:21:19 +00:00
2016-09-16 22:29:38 +00:00
gunFrontVec = gunXf.frontVector();
if (gunFrontVec.canBeNormalized())
gunFrontVec.normalize();
2016-09-16 22:21:19 +00:00
2017-11-30 01:22:46 +00:00
//float angle = gunFrontVec.dot(rVec);
//float sdt = dt * g_tweakPlayer->GetGrappleCameraSpeed();
2016-09-16 22:21:19 +00:00
2017-11-30 01:22:46 +00:00
//angle = zeus::clamp(-1.f, angle, 1.f);
//angle = zeus::clamp(0.f, std::acos(angle) / sdt, 1.f);
2017-12-16 00:19:15 +00:00
qGun = zeus::CQuaternion::lookAt(gunFrontVec, rVecCpy, 2.f * M_PIF);
2016-09-16 22:21:19 +00:00
}
2017-11-30 01:22:46 +00:00
break;
2016-09-16 22:21:19 +00:00
}
}
}
2016-09-16 22:21:19 +00:00
else
{
2016-09-16 22:29:38 +00:00
zeus::CVector3f gunFront = x190_gunFollowXf.frontVector();
2017-11-30 01:22:46 +00:00
gunFront.z = 0.f;
2016-09-16 22:29:38 +00:00
if (gunFront.canBeNormalized())
gunFront.normalize();
2016-09-16 22:21:19 +00:00
zeus::CVector3f rVecCpy = rVec;
2017-11-30 01:22:46 +00:00
rVecCpy.z = 0.f;
2016-09-16 22:21:19 +00:00
if (rVecCpy.canBeNormalized())
rVecCpy.normalize();
2017-12-16 00:19:15 +00:00
gunXf = zeus::CQuaternion::lookAt(gunFront, rVecCpy, 2.f * M_PIF).toTransform() *
2016-09-16 22:21:19 +00:00
x190_gunFollowXf.getRotation();
2016-09-16 22:29:38 +00:00
gunFront = gunXf.frontVector();
if (gunFront.canBeNormalized())
gunFront.normalize();
2016-09-16 22:21:19 +00:00
2016-09-16 22:29:38 +00:00
float angle = gunFront.dot(rVec);
2017-08-19 06:52:13 +00:00
angle = zeus::clamp(-1.f, angle, 1.f);
float sdt = dt * g_tweakPlayer->GetFreeLookSpeed();
2016-09-16 22:21:19 +00:00
qGun = zeus::CQuaternion::lookAt(
2017-11-30 01:22:46 +00:00
gunFront, rVec, sdt * zeus::clamp(0.f, g_tweakPlayer->GetFreeLookDampenFactor() *
2017-08-19 06:52:13 +00:00
(std::acos(angle) / sdt), 1.f));
2016-09-16 22:21:19 +00:00
}
zeus::CTransform bobXf = player->GetCameraBob()->GetCameraBobTransformation();
2017-04-03 01:39:23 +00:00
if (player->GetMorphballTransitionState() == CPlayer::EPlayerMorphBallState::Morphed ||
2017-08-19 06:52:13 +00:00
player->GetOrbitState() == CPlayer::EPlayerOrbitState::Grapple ||
player->GetGrappleState() != CPlayer::EGrappleState::None ||
2017-07-17 03:04:14 +00:00
mgr.GetGameState() == CStateManager::EGameState::SoftPaused ||
2017-11-30 01:22:46 +00:00
mgr.GetCameraManager()->IsInCinematicCamera() ||
x1d4_closeInTimer > 0.f)
2016-09-16 22:21:19 +00:00
{
bobXf = zeus::CTransform::Identity();
player->GetCameraBob()->SetCameraBobTransform(bobXf);
}
x190_gunFollowXf = qGun.toTransform() * gunXf;
2017-12-16 00:19:15 +00:00
SetTransform(x190_gunFollowXf * bobXf.getRotation());
x190_gunFollowXf.origin = eyePos;
CActor::SetTranslation(eyePos + player->GetTransform().rotate(bobXf.origin));
2016-09-16 22:21:19 +00:00
x190_gunFollowXf.orthonormalize();
}
2017-10-07 05:32:11 +00:00
void CFirstPersonCamera::UpdateElevation(CStateManager& mgr)
{
x1c0_pitch = 0.f;
if (TCastToConstPtr<CPlayer> player = mgr.GetObjectById(xe8_watchedObject))
{
if (x1c4_pitchId != kInvalidUniqueId)
{
if (TCastToConstPtr<CScriptCameraPitchVolume> pvol = mgr.GetObjectById(x1c4_pitchId))
{
zeus::CVector3f pitchDirFlat = pvol->GetTransform().basis[1];
pitchDirFlat.z = 0.f;
if (!pitchDirFlat.canBeNormalized())
pitchDirFlat = zeus::CVector3f::skForward;
zeus::CVector3f playerDirFlat = player->GetTransform().basis[1];
playerDirFlat.z = 0.f;
playerDirFlat.normalize();
float pitchDot = zeus::clamp(-1.f, pitchDirFlat.dot(playerDirFlat), 1.f);
if (pitchDot < 0.f)
x1c0_pitch = pvol->GetDownPitch() * -pitchDot;
else
x1c0_pitch = pvol->GetUpPitch() * -pitchDot;
zeus::CVector3f pvolToPlayerFlat = player->GetTranslation() - pvol->GetTranslation();
pvolToPlayerFlat.z = 0.f;
float pitchMul = 0.f;
if (pvolToPlayerFlat.canBeNormalized())
{
float pvolPlayerProj =
std::fabs(zeus::clamp(-1.f, pvolToPlayerFlat.dot(pitchDirFlat), 1.f)) *
pvolToPlayerFlat.magnitude();
if (pvolPlayerProj <= pvol->GetMaxInterpolationDistance())
pitchMul = 1.f;
else
pitchMul = 1.f - zeus::clamp(-1.f, (pvolPlayerProj - pvol->GetMaxInterpolationDistance()) /
(pvol->GetScale().y - pvol->GetMaxInterpolationDistance()), 1.f);
}
x1c0_pitch *= pitchMul;
}
}
}
}
}