MoveCamera.cpp 1.51 KB
Newer Older
Anakin's avatar
Anakin committed
1 2
#include "..\Header\MoveCamera.h"
#include <QVector2D>
Anakin's avatar
Anakin committed
3
#include <qmath.h>
Anakin's avatar
Anakin committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24


/////////////////////////////////////////////////////////////////////////
// constructor/destructor

MoveCamera::MoveCamera()
{
	resetView();
}

MoveCamera::~MoveCamera()
{

}


/////////////////////////////////////////////////////////////////////////
// functions

void MoveCamera::rotateAction(QVector2D diff)
{
Anakin's avatar
Anakin committed
25 26
	m_phi -= diff.x() * 0.01;
	m_theta += diff.y() * 0.01;
Anakin's avatar
Anakin committed
27

Anakin's avatar
Anakin committed
28
	m_theta = qMax(qMin(M_PI - 0.0001, m_theta), 0.0001);
Anakin's avatar
Anakin committed
29 30 31 32
}

void MoveCamera::moveAction(QVector2D diff)
{
Anakin's avatar
Anakin committed
33 34 35 36 37 38
	if (diff.y() > 0)
		m_sidewardSpeed = 1;
	else if (diff.y() < 0)
		m_sidewardSpeed = -1;
	else
		m_sidewardSpeed = 0;
Anakin's avatar
Anakin committed
39 40 41 42
}

void MoveCamera::wheelAction(double value)
{
Anakin's avatar
Anakin committed
43 44 45 46 47 48
	if (value > 0)
		m_forwardSpeed = 1;
	else if (value < 0)
		m_forwardSpeed = -1;
	else
		m_forwardSpeed = 0;
Anakin's avatar
Anakin committed
49 50 51 52
}

void MoveCamera::recalculateMatrix()
{
53
	m_matrix = QMatrix4x4();
Anakin's avatar
Anakin committed
54

Anakin's avatar
Anakin committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	// different coordinate (spherical -> world) X->Z | Y->X | Z->Y
	QVector3D tmpdirection(
		qSin(m_theta) * qSin(m_phi),
		qCos(m_theta),
		qSin(m_theta) * qCos(m_phi)
	);

	QVector3D tmpRight(
		qSin(m_phi - M_PI_2),
		0,
		qCos(m_phi - M_PI_2)
	);

	m_position += m_forwardSpeed * m_zSpeed * 0.1 * tmpdirection;
	m_position += m_sidewardSpeed * m_zSpeed * 0.1 * tmpRight;

	m_matrix.lookAt(m_position, m_position + tmpdirection, QVector3D::crossProduct(tmpRight, tmpdirection));
Anakin's avatar
Anakin committed
72 73 74 75
}

void MoveCamera::resetView()
{
Anakin's avatar
Anakin committed
76 77 78 79 80
	m_position = { 0,0,4 };
	m_phi = M_PI;
	m_theta = M_PI_2;
	m_forwardSpeed = 0;
	m_sidewardSpeed = 0;
Anakin's avatar
Anakin committed
81 82
	CameraInterface::resetView();
}