Third Person Camera
This blog is focused on creating third person view style controlled camera, which follows a user controlled object.
Camera::Camera(float fov, float aspect, float nearPlane, float farPlane)
{
m_right = { 0,0,0 };
m_up = { 0,0,0 };
m_look = { 0,0,0 };
m_position = { 0,0,0 };
m_Oldposition = { 0,0,0 };
m_lookAt = { 0,0,0 };
m_velocity = { 0,0,0 };
SetLookAt(XMFLOAT3(0, 0, 5));
#if _DEBUG
m_maxVelocity = 0.5f;
#else
m_maxVelocity = 0.2f;
#endif
CreateProjectionMatrix(fov, aspect, nearPlane, farPlane);
m_viewMatrix = { 1,0,0,0, 0,1,0,0, 0,0,1,0 ,0,0,0,1 };
}
- Positioning the camera and moving towards the player
void Camera::MoveForward(float units)
{
// Moving the camera through y axis while its lowering
if (Y_Movement)
{
m_velocity = m_velocity + m_look * units;
}
// Making a checks so that there is no change in y
else
{
XMFLOAT3 moveVector;
moveVector.x = m_look.x;
moveVector.y = 0.0f;
moveVector.z = m_look.z;
moveVector = Normalize(moveVector);
moveVector = moveVector * units;
m_velocity = m_velocity + moveVector;
}
}
- Create a new projection matrix with CreativePerspectiveFieldOfView. It controls how camera coordinate values are transformed to screen coordinates
void Camera::CreateProjectionMatrix(float fov, float aspect, float nearPlane, float farPlane)
{
m_fov = XMConvertToRadians(fov);
m_aspect = aspect;
m_nearPlane = nearPlane;
m_farPlane = farPlane;
XMStoreFloat4x4(&m_projection, XMMatrixPerspectiveFovLH(m_fov, aspect, nearPlane, farPlane));
}
- Updating the camera
void Camera::Update(NavMesh * _nav)
{
//Clamping how fast the camera is moving if (vectorLength(m_velocity) > m_maxVelocity)
{
m_velocity = Normalize(m_velocity) * m_maxVelocity;
}
//MOVING m_Oldposition = m_position;
m_position = m_position + m_velocity;
CameraBoundcheck(_nav);
//STOP! m_velocity = XMFLOAT3(m_velocity.x * .9f, m_velocity.y * .9f, m_velocity.z * .9f);
m_lookAt = m_position + m_look;
//Making new view Matrix XMFLOAT4 up = { 0,1,0,1.0f };
XMFLOAT4X4 temp;
XMStoreFloat4x4(&temp, XMMatrixInverse(nullptr, XMLoadFloat4x4(&m_viewMatrix)));
XMStoreFloat4x4(&m_viewMatrix, XMMatrixLookAtLH(XMLoadFloat3(&m_position), XMLoadFloat3(&m_lookAt), XMLoadFloat4(&up)));
BuildViewFrustum();
m_right.x = temp._11;
m_right.y = temp._12;
m_right.z = temp._13;
m_up.x = temp._21;
m_up.y = temp._22;
m_up.z = temp._23;
m_look.x = temp._31;
m_look.y = temp._32;
m_look.z = temp._33;
}
Making Third Person Camera
- Setting up the camera with important elements of view matrix and lookAt vectorCamera::Camera(float fov, float aspect, float nearPlane, float farPlane)
{
m_right = { 0,0,0 };
m_up = { 0,0,0 };
m_look = { 0,0,0 };
m_position = { 0,0,0 };
m_Oldposition = { 0,0,0 };
m_lookAt = { 0,0,0 };
m_velocity = { 0,0,0 };
SetLookAt(XMFLOAT3(0, 0, 5));
#if _DEBUG
m_maxVelocity = 0.5f;
#else
m_maxVelocity = 0.2f;
#endif
CreateProjectionMatrix(fov, aspect, nearPlane, farPlane);
m_viewMatrix = { 1,0,0,0, 0,1,0,0, 0,0,1,0 ,0,0,0,1 };
}
- Positioning the camera and moving towards the player
void Camera::MoveForward(float units)
{
// Moving the camera through y axis while its lowering
if (Y_Movement)
{
m_velocity = m_velocity + m_look * units;
}
// Making a checks so that there is no change in y
else
{
XMFLOAT3 moveVector;
moveVector.x = m_look.x;
moveVector.y = 0.0f;
moveVector.z = m_look.z;
moveVector = Normalize(moveVector);
moveVector = moveVector * units;
m_velocity = m_velocity + moveVector;
}
}
- Create a new projection matrix with CreativePerspectiveFieldOfView. It controls how camera coordinate values are transformed to screen coordinates
void Camera::CreateProjectionMatrix(float fov, float aspect, float nearPlane, float farPlane)
{
m_fov = XMConvertToRadians(fov);
m_aspect = aspect;
m_nearPlane = nearPlane;
m_farPlane = farPlane;
XMStoreFloat4x4(&m_projection, XMMatrixPerspectiveFovLH(m_fov, aspect, nearPlane, farPlane));
}
- Updating the camera
void Camera::Update(NavMesh * _nav)
{
//Clamping how fast the camera is moving if (vectorLength(m_velocity) > m_maxVelocity)
{
m_velocity = Normalize(m_velocity) * m_maxVelocity;
}
//MOVING m_Oldposition = m_position;
m_position = m_position + m_velocity;
CameraBoundcheck(_nav);
//STOP! m_velocity = XMFLOAT3(m_velocity.x * .9f, m_velocity.y * .9f, m_velocity.z * .9f);
m_lookAt = m_position + m_look;
//Making new view Matrix XMFLOAT4 up = { 0,1,0,1.0f };
XMFLOAT4X4 temp;
XMStoreFloat4x4(&temp, XMMatrixInverse(nullptr, XMLoadFloat4x4(&m_viewMatrix)));
XMStoreFloat4x4(&m_viewMatrix, XMMatrixLookAtLH(XMLoadFloat3(&m_position), XMLoadFloat3(&m_lookAt), XMLoadFloat4(&up)));
BuildViewFrustum();
m_right.x = temp._11;
m_right.y = temp._12;
m_right.z = temp._13;
m_up.x = temp._21;
m_up.y = temp._22;
m_up.z = temp._23;
m_look.x = temp._31;
m_look.y = temp._32;
m_look.z = temp._33;
}
Comments
Post a Comment