Fichier source en cours : FW - ThirdPersonCamera.cpp






Voici le code source cpp :




	#include "Framework.h"

using namespace fw;

ThirdPersonCamera::ThirdPersonCamera(const std::string& sName) :
Camera(sName),
m_fSpeed(30), // Mettre define
m_bCenterMouse(true),
m_bActive(true),
m_fDistance(15.0f) // Mettre define
{
	m_camAngle = D3DXVECTOR2(0, 0);
}

ThirdPersonCamera::~ThirdPersonCamera()
{
}

void ThirdPersonCamera::SetSpeed(float fSpeed)
{
	m_fSpeed = fSpeed;
}

void ThirdPersonCamera::SetTarget(const D3DXVECTOR3& target)
{
	m_lookAt = m_translation - target;

	BuildView();
}

void ThirdPersonCamera::Update(float dt)
{
	if (!m_bActive)
	{
		return;
	}

	float x = 0.0f;
	float y = 0.0f;

	INPUT_MANAGER->GetMouseRelativePosition(x, y);

	m_camAngle.x += x * CAMERA_SPEED_ROTATE_AROUND * dt;
	m_camAngle.y -= y * CAMERA_SPEED_ROTATE_AROUND * dt;

	m_camAngle.y = MATHS->Clamp(0.003f, (float) D3DX_PI - 0.003f, m_camAngle.y);

	// Trigonométrie pour faire en sorte que la caméra 
	// fasse une rotation autour d'un point
	float cosX = cos(m_camAngle.x);
	float cosY = cos(m_camAngle.y);
	float sinY = sin(m_camAngle.y);
	float sinX = sin(m_camAngle.x);

	float xf = cosX * sinY;
	float yf = cosY;
	float zf = sinX * sinY;

	D3DXVECTOR3 finalPos(xf, yf, zf);

	m_translation = finalPos * m_fDistance;

	INPUT_MANAGER->GetMouseRelativePosition(x, y);

	if (m_bCenterMouse)
	{
		INPUT_MANAGER->CenterMouseCursor();
	}

	Camera::Update(dt);
}

void ThirdPersonCamera::ToggleCenteredMouseCursor()
{
	m_bCenterMouse = !m_bCenterMouse;

	ShowCursor(!m_bCenterMouse);
}

void ThirdPersonCamera::BuildView()
{
	D3DXMatrixLookAtLH(&m_view, &m_translation, &m_lookAt, &m_up);

	/*
	D3DXVec3Cross(&m_up, &m_lookAt, &m_right);
	D3DXVec3Normalize(&m_up, &m_up);

	D3DXVec3Cross(&m_right, &m_up, &m_lookAt);
	D3DXVec3Normalize(&m_right, &m_right);

	float x = -D3DXVec3Dot(&m_pos, &m_right);
	float y = -D3DXVec3Dot(&m_pos, &m_up);
	float z = -D3DXVec3Dot(&m_pos, &m_lookAt);

	m_view(0, 0) = m_right.x;
	m_view(1, 0) = m_right.y;
	m_view(2, 0) = m_right.z;
	m_view(3, 0) = x;

	m_view(0, 1) = m_up.x;
	m_view(1, 1) = m_up.y;
	m_view(2, 1) = m_up.z;
	m_view(3, 1) = y;

	m_view(0, 2) = m_lookAt.x;
	m_view(1, 2) = m_lookAt.y;
	m_view(2, 2) = m_lookAt.z;
	m_view(3, 2) = z;

	m_view(0, 3) = 0.0f;
	m_view(1, 3) = 0.0f;
	m_view(2, 3) = 0.0f;
	m_view(3, 3) = 1.0f;
	*/
}

void ThirdPersonCamera::SetActive(bool bActive)
{
	m_bActive = bActive;

	ShowCursor(!bActive);

	if (bActive)
	{
		INPUT_MANAGER->SaveMousePosition();
		INPUT_MANAGER->CenterMouseCursor();
	}
	else // On restaure l'ancienne position de la souris
	{
		INPUT_MANAGER->RestoreMousePosition();
	}
}

bool ThirdPersonCamera::GetActive()
{
	return m_bActive;
}

void ThirdPersonCamera::SetDistance(float fDistance)
{
	m_fDistance = fDistance;
}