Anonymous Anonymous - 7 days ago
48 0

No description

C++

Camera Class

// Header
#include "Vector3.h"

class Camera {

	public:
		void update();

		float getPosX();
		float getPosY();
		float getPosZ();

		void setPosX(float x);
		void setPosY(float y);
		void setPosZ(float z);

		float getPitch();
		float getYaw();
		float getRoll();

		void setPitch(float pitch);
		void setYaw(float yaw);
		void setRoll(float roll);

		float getLookAtX();
		float getLookAtY();
		float getLookAtZ();

		void setCamRotateSpeed(float speed);
		void setCamMoveSpeed(float speed);

		void rotateLeft(float dt);
		void rotateRight(float dt);
		void rotateUp(float dt);
		void rotateDown(float dt);

		void moveForward(float dt);
		void moveBack(float dt);
		void moveLeft(float dt);
		void moveRight(float dt);
		void moveUp(float dt);
		void moveDown(float dt);

		void mouseRotations(float xDistance, float yDistance, float dt);
		
		float Pitch = 0;
		float Yaw = 0;
		float Roll = 0;

		float posX = 0;
		float posY = 0;
		float posZ = 0;

		float lookAtX = 0;
		float lookAtY = 0;
		float lookAtZ = 0;

		float camRotateSpeed = 0;
		float camMoveSpeed = 0;

		Vector3 forward = { 0, 0, 0 };
		Vector3 up = { 0, 0, 0 };
		Vector3 right = { 0, 0, 0 };
};


// Class
#include "Camera.h"

void Camera::update()
{
	float cosR, cosP, cosY; // Temp values for Pitch/Roll/Yaw
	float sinR, sinP, sinY; // Pitch is around x, Yaw is around y, Roll is around z

	cosY = cosf(Yaw*3.1415 / 180);
	cosP = cosf(Pitch*3.1415 / 180);
	cosR = cosf(Roll*3.1415 / 180);
	sinY = sinf(Yaw*3.1415 / 180);
	sinP = sinf(Pitch*3.1415 / 180);
	sinR = sinf(Roll*3.1415 / 180);

	// Forward vector
	forward.x = sinY * cosP;
	forward.y = sinP;
	forward.z = cosP * -cosY;

	// Up Vector
	up.x = -cosY * sinR - sinY * sinP * cosR;
	up.y = cosP * cosR;
	up.z = -sinY * sinR - sinP * cosR * -cosY;

	// Right Vector
	right.x = forward.y * up.z - forward.z * up.y;
	right.y = forward.z * up.x - forward.x * up.z;
	right.z = forward.x * up.y - forward.y * up.x;

	// Calculate LookAt
	lookAtX = posX + forward.x;
	lookAtY = posY + forward.y;
	lookAtZ = posZ + forward.z;
}

float Camera::getPosX()
{
	return this->posX;
}

float Camera::getPosY()
{
	return this->posY;
}

float Camera::getPosZ()
{
	return this->posZ;
}

void Camera::setPosX(float x)
{
	this->posX = x;
}

void Camera::setPosY(float y)
{
	this->posY = y;
}

void Camera::setPosZ(float z)
{
	this->posZ = z;
}

float Camera::getPitch()
{
	return this->Pitch;
}

float Camera::getYaw()
{
	return this->Yaw;
}

float Camera::getRoll()
{
	return this->Roll;
}

void Camera::setPitch(float pitch)
{
	this->Pitch = pitch;
}

void Camera::setYaw(float yaw)
{
	this->Yaw = yaw;
}

void Camera::setRoll(float roll)
{
	this->Roll = roll;
}

float Camera::getLookAtX()
{
	return this->lookAtX;
}

float Camera::getLookAtY()
{
	return this->lookAtY;
}

float Camera::getLookAtZ()
{
	return this->lookAtZ;
}

void Camera::setCamRotateSpeed(float speed)
{
	this->camRotateSpeed = speed;
}

void Camera::setCamMoveSpeed(float speed)
{
	this->camMoveSpeed = speed;
}

void Camera::rotateLeft(float dt)
{
	float newYaw;

	newYaw = Yaw - camRotateSpeed * dt;

	setYaw(newYaw);
}

void Camera::rotateRight(float dt)
{
	float newYaw;

	newYaw = Yaw + camRotateSpeed * dt;

	setYaw(newYaw);
}

void Camera::rotateUp(float dt)
{
	float newPitch;

	newPitch = Pitch + camRotateSpeed * dt;

	setPitch(newPitch);
}

void Camera::rotateDown(float dt)
{
	float newPitch;

	newPitch = Pitch - camRotateSpeed * dt;

	setPitch(newPitch);
}

void Camera::moveForward(float dt)
{
	float newXPos, newYPos, newZPos;

	newXPos = posX + forward.x * camMoveSpeed * dt;
	newYPos = posY + forward.y * camMoveSpeed * dt;
	newZPos = posZ + forward.z * camMoveSpeed * dt;

	setPosX(newXPos);
	setPosY(newYPos);
	setPosZ(newZPos);
}

void Camera::moveBack(float dt)
{
	float newXPos, newYPos, newZPos;

	newXPos = posX - forward.x * camMoveSpeed * dt;
	newYPos = posY - forward.y * camMoveSpeed * dt;
	newZPos = posZ - forward.z * camMoveSpeed * dt;

	setPosX(newXPos);
	setPosY(newYPos);
	setPosZ(newZPos);
}

void Camera::moveLeft(float dt)
{
	float newXPos, newYPos, newZPos;

	newXPos = posX + right.x * camMoveSpeed * dt;
	newYPos = posY + right.y * camMoveSpeed * dt;
	newZPos = posZ + right.z * camMoveSpeed * dt;

	setPosX(newXPos);
	setPosY(newYPos);
	setPosZ(newZPos);
}

void Camera::moveRight(float dt)
{
	float newXPos, newYPos, newZPos;

	newXPos = posX - right.x * camMoveSpeed * dt;
	newYPos = posY - right.y * camMoveSpeed * dt;
	newZPos = posZ - right.z * camMoveSpeed * dt;

	setPosX(newXPos);
	setPosY(newYPos);
	setPosZ(newZPos);
}

void Camera::moveUp(float dt)
{
	float newXPos, newYPos, newZPos;

	newXPos = posX + up.x * camMoveSpeed * dt;
	newYPos = posY + up.y * camMoveSpeed * dt;
	newZPos = posZ + up.z * camMoveSpeed * dt;

	setPosX(newXPos);
	setPosY(newYPos);
	setPosZ(newZPos);
}

void Camera::moveDown(float dt)
{
	float newXPos, newYPos, newZPos;

	newXPos = posX - up.x * camMoveSpeed * dt;
	newYPos = posY - up.y * camMoveSpeed * dt;
	newZPos = posZ - up.z * camMoveSpeed * dt;

	setPosX(newXPos);
	setPosY(newYPos);
	setPosZ(newZPos);
}

void Camera::mouseRotations(float xDistance, float yDistance, float dt)
{
	float newPitch;
	float newYaw;

	newPitch = Pitch - yDistance * camRotateSpeed * dt * 0.7f;
	newYaw = Yaw + xDistance * camRotateSpeed * dt;

	setPitch(newPitch);
	setYaw(newYaw);
}