How to use quaternions to store the rotation of a camera? - c++

I'm trying to make a "6 degrees of freedom" camera like the ones used in space games. I would like to learn how to store camera rotation as a quaternion but I can't exactly find anything on the internet to help me (or maybe I don't know what keywords I should be using).
What I want to do is use a glm::quat to store the orientation of the camera but the parts that I'm having issues with is changing the pitch, yaw, roll and finding out how to get the new up, right and direction vectors needed for the glm::lookAt function to create a view matrix.
So far this is my code with the functions I need help with marked:
#include "camera.hpp"
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/quaternion.hpp>
Camera::Camera(glm::vec3 position, glm::quat orientation, float zNear, float zFar)
{
this->position = position;
this->orientation = orientation;
right = glm::vec3( 1, 0, 0);
up = glm::vec3( 0, 1, 0);
direction = glm::vec3( 0, 0, -1);
this->zNear = zNear;
this->zFar = zFar;
updateViewMatrix();
}
Camera::Camera(glm::vec3 position, glm::vec3 orientation, float zNear, float zFar)
{
this->position = position;
this->orientation = glm::quat(glm::vec3(glm::radians(orientation.x), glm::radians(orientation.y), glm::radians(orientation.z)));
right = glm::vec3( 1, 0, 0);
up = glm::vec3( 0, 1, 0);
direction = glm::vec3( 0, 0, -1);
this->zNear = zNear;
this->zFar = zFar;
updateViewMatrix();
}
void Camera::setPosition(glm::vec3 position)
{
this->position = position;
}
void Camera::setOrientation(glm::quat orientation)
{
this->orientation = orientation;
}
void Camera::setOrientation(glm::vec3 orientation)
{
glm::quat orientationQuat = glm::quat(orientation);
this->orientation = orientationQuat;
}
void Camera::setPitch(float amount)
{
// NEED HELP
updateViewMatrix();
}
void Camera::setYaw(float amount)
{
// NEED HELP
updateViewMatrix();
}
void Camera::setRoll(float amount)
{
// NEED HELP
updateViewMatrix();
}
void Camera::setZNear(float zNear)
{
this->zNear = zNear;
updateProjectionMatrix();
}
void Camera::setZFar(float zFar)
{
this->zFar = zFar;
updateProjectionMatrix();
}
void Camera::move(glm::vec3 movement)
{
position += right * movement.x + up * movement.y + direction * movement.z;
updateViewMatrix();
}
void Camera::moveAxis(glm::vec3 translation)
{
position += translation;
updateViewMatrix();
}
void Camera::rotate(glm::quat rotation)
{
// NEED HELP
}
void Camera::rotate(glm::vec3 rotation)
{
glm::quat rotationQuat = glm::quat(rotation);
// NEED HELP
}
void Camera::pitch(float amount)
{
// NEED HELP
updateViewMatrix();
}
void Camera::yaw(float amount)
{
// NEED HELP
updateViewMatrix();
}
void Camera::roll(float amount)
{
// NEED HELP
updateViewMatrix();
}
// Excluded a few get___() functions here
glm::mat4 Camera::getViewMatrix() const
{
return viewMatrix;
}
glm::mat4 Camera::getProjectionMatrix() const
{
return projectionMatrix;
}
glm::mat4 Camera::getViewProjectionMatrix() const
{
return viewProjectionMatrix;
}
void Camera::updateViewMatrix()
{
viewMatrix = glm::lookAt(position, position + direction, up/*glm::cross(right, direction)*/);
viewProjectionMatrix = projectionMatrix * viewMatrix;
}
These are the relevant variables I create in my header file:
glm::vec3 position;
glm::quat orientation;
glm::vec3 direction; // Camera Direction / Camera View Facing
glm::vec3 right;
glm::vec3 up;

I found the answer to my problem of "How to find the right, up and direction vectors for the glm::lookat function"
All I need to do now is change the world up, right and direction vectors by the orientation of the quaternion like so:
right = glm::normalize(orientation * glm::vec3(1, 0, 0));
up = glm::normalize(orientation * glm::vec3(0, 1, 0));
direction = glm::normalize(orientation * glm::vec3(0, 0, -1));

Related

Quaternion rotations strange behavior

I'm currently working on my own small game engine (I am learning OpenGL). I made camera mechanism that can rotate vectors with Euler angles, and now I'm working on rotations with quaternions. Now I'm stuck because my quaternions rotations behave very strangely (flipping objects, not rotating camera as it should). Please, help me find out what is wrong with my algorithm. Could you suggest some fixes to my camera code? Below is my source code, and here is some marks to it: target I want to look at is at coordinates (0.0f, 0.0f, 0.0f). I want my position from which I am look at the target to be glm::vec3 position = glm::vec3(0.0f, 0.0f, 3.0f)
My camera class:
class Camera {
private:
bool eulerMode = false;
float m_mouseSensitivity;
float m_velocity;
glm::vec3 m_rightAxis{};
glm::vec3 m_upAxis;
glm::vec3 m_position;
glm::vec3 m_target{};
glm::vec3 m_r{};
glm::vec3 m_direction{};
static glm::vec3 rotateVector(float angle, glm::vec3 rotationAxis, glm::vec3 vectorToRotate);
static glm::quat quaternion(float angle, glm::vec3 vec);
public:
static Camera *s_context;
float m_yaw;
float m_pitch;
float m_mouseLastX;
float m_mouseLastY;
bool m_firstMouse;
glm::vec3 m_frontAxis;
Camera(float speed,
int width,
int height,
glm::vec3 position = glm::vec3(0.0f, 0.0f, 3.0f),
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f),
glm::vec3 target = glm::vec3(0.0f, 0.0f, 0.0f)
);
glm::mat4 getLookAtMatrix();
static void quaternionRotate(GLFWwindow *window, double x, double y);
};
Camera::Camera(
float speed,
int width,
int height,
glm::vec3 position,
glm::vec3 up,
glm::vec3 target
)
: m_pitch(0.0f), m_yaw(-90.0f), m_mouseLastX((float) width / 2),
m_mouseLastY((float) height / 2), m_mouseSensitivity(0.1f), m_upAxis(up), m_position(position),
m_frontAxis(glm::vec3(0.0f, 0.0f, -1.0f)), m_firstMouse(true) {
m_velocity = speed;
m_direction = glm::normalize(position - target);
m_rightAxis = glm::normalize(glm::cross(up, m_direction));
m_upAxis = glm::cross(m_direction, m_rightAxis);
s_context = this;
glfwSetWindowUserPointer(g_Window->getOpenGLWindow(), this);
if (eulerMode) {
glfwSetCursorPosCallback(g_Window->getOpenGLWindow(), eulerRotate);
} else {
glfwSetCursorPosCallback(g_Window->getOpenGLWindow(), quaternionRotate);
}
}
glm::mat4 Camera::getLookAtMatrix() {
glm::mat4 view = glm::lookAt(m_position, m_r, m_upAxis);
return view;
}
// for the sake of brevity, I skipped some class methods that are unnecessary for quaternions rotations
void Camera::quaternionRotate(GLFWwindow *window, double x, double y) {
if (s_context->m_firstMouse) {
s_context->m_mouseLastX = (float) x;
s_context->m_mouseLastY = (float) y;
s_context->m_firstMouse = false;
}
auto xoffset = (float) (x - s_context->m_mouseLastX);
auto yoffset = (float) (s_context->m_mouseLastY - y);
s_context->m_mouseLastX = (float) x;
s_context->m_mouseLastY = (float) y;
float sensitivity = 0.1f;
xoffset *= sensitivity;
yoffset *= sensitivity;
s_context->m_yaw += xoffset;
s_context->m_pitch += yoffset;
glm::vec3 yAxis = glm::vec3(0, 1, 0);
// Rotate the view vector by the horizontal angle around the vertical axis
glm::vec3 view = s_context->m_direction;
view = glm::normalize(rotateVector(s_context->m_yaw, yAxis, view));
// Rotate the view vector by the vertical angle around the horizontal axis
glm::vec3 xAxis = glm::normalize(glm::cross(yAxis, view));
view = glm::normalize(rotateVector(s_context->m_pitch, xAxis, view));
s_context->m_r = view;
s_context->m_upAxis = glm::normalize(glm::cross(s_context->m_r, xAxis));
}
glm::vec3 Camera::rotateVector(float angle, const glm::vec3 rotationAxis, const glm::vec3 vectorToRotate) {
glm::quat rotationQ = quaternion(angle, rotationAxis);
glm::quat conjugateQ = glm::conjugate(rotationQ);
glm::quat result = rotationQ * vectorToRotate * conjugateQ;
return {result.x, result.y, result.z};
}
glm::quat Camera::quaternion(float angle, const glm::vec3 vec) {
float HalfAngleInRadians = glm::radians(angle / 2);
float SineHalfAngle = sinf(HalfAngleInRadians);
float CosHalfAngle = cosf(HalfAngleInRadians);
float xC = vec.x * SineHalfAngle;
float yC = vec.y * SineHalfAngle;
float zC = vec.z * SineHalfAngle;
float wC = CosHalfAngle;
return {wC, xC, yC, zC};
}

How to move camera around and stick it to the player?

I'm trying to move camera around the player.
Right now I'm using camera class like this:
Camera::Camera(glm::vec3 position, glm::vec3 up, GLfloat yaw, GLfloat pitch)
{
this->position = position;
this->m_WorldUp = up;
this->up = up;
this->m_Yaw = yaw;
this->m_Pitch = pitch;
this->UpdateCameraVectors();
}
glm::mat4 Camera::getViewMatrix()
{
return glm::lookAt(position, position + m_Front, up);
}
void Camera::ProcessKeyboard(Camera_Movement direction, GLfloat deltaTime)
{
float velocity = moveSpeed * deltaTime;
switch (direction) {
case FORWARD: position += m_Front * velocity; break;
case BACKWARD: position -= m_Front * velocity; break;
case LEFT: position -= m_Right * velocity; break;
case RIGHT: position += m_Right * velocity; break;
case UPWARDS: position += m_WorldUp * velocity; break;
case DOWNWARDS: position -= m_WorldUp * velocity; break;
}
}
void Camera::ProcessMouseMovement(GLfloat xOffset, GLfloat yOffset, GLboolean constrainPitch)
{
xOffset *= sensitivity;
yOffset *= sensitivity;
m_Yaw += xOffset;
m_Pitch += yOffset;
if (constrainPitch) {
if (m_Pitch > 89.0f) {
m_Pitch = 89.0f;
} else if (m_Pitch < -89.0f) {
m_Pitch = -89.0f;
}
}
UpdateCameraVectors();
}
void Camera::UpdateCameraVectors()
{
glm::vec3 front;
front.x = cos(glm::radians(m_Yaw)) * cos(glm::radians(m_Pitch));
front.y = -sin(glm::radians(m_Pitch));
front.z = sin(glm::radians(m_Yaw)) * cos(glm::radians(m_Pitch));
m_Front = glm::normalize(front);
m_Right = glm::normalize(glm::cross(m_Front, m_WorldUp));
up = glm::normalize(glm::cross(m_Right, m_Front));
}
It allows me to free look and move around the world.
Player's update method at the moment:
glm::mat4 projection = glm::mat4(1.0f);
projection = glm::perspective(glm::radians(45.0f), 16.0f / 9.0f, 0.1f, 1000.0f);
glm::mat4 view = glm::mat4(1.0f);
view = camera->getViewMatrix();
glm::mat4 model = glm::mat4(1.0f); {
glm::mat4 translate = glm::translate(model, position);
glm::mat4 rotate = glm::rotate(model, glm::radians(180.0f), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 scale = glm::scale(model, glm::vec3(0.1f, 0.1f, 0.1f));
model = translate * rotate * scale;
}
glm::mat4 mvp = projection * view * model;
GLint u_mvp = shader.GetUniformLocation("u_mvp");
glUniformMatrix4fv(u_mvp, 1, GL_FALSE, glm::value_ptr(mvp));
I know that I have to change something with view matrix, but I have not got enough knowledge.
How can I upgrade my camera class that it can look, rotate, around a player, like in a circle, an MMO RPG style?
The camera class itself should not be receiving keyboard updates at all - that should be done in the player class. Every time the player moves, update the camera class with its new position. See comments in below code for more details.
Camera::Camera(glm::vec3 position, glm::vec3 up, GLfloat yaw, GLfloat pitch, GLfloat dist)
{
m_WorldUp = up;
// this->up = up; <- delete this variable; lookAt computes it for us
m_Pos = position; // this is the *player* position
m_Yaw = yaw;
m_Pitch = pitch;
m_Dist = dist; // distance from the player
UpdateViewMatrix(true);
}
// private method
void Camera::UpdateViewMatrix(bool computeDir = false)
{
// compute the new direction
if (computeDir)
{
glm::vec3 radial;
radial.x = cos(glm::radians(m_Yaw)) * cos(glm::radians(m_Pitch));
radial.y = sin(glm::radians(m_Pitch)); // there was a sign error here
radial.z = sin(glm::radians(m_Yaw)) * cos(glm::radians(m_Pitch));
m_Dir = -radial;
}
glm::vec3 pos = m_Pos - m_Dist * m_Dir; // *camera* position
// additional view matrix member variable
// you were using lookAt in the wrong way
m_View = glm::lookAt(m_Pos, pos, m_WorldUp);
}
// public method - call this everytime the player moves
void Camera::UpdateTargetPosition(glm::vec3 const & pos)
{
m_Pos = pos;
UpdateViewMatrix();
}
void Camera::UpdateAngles(GLfloat yaw, GLfloat pitch, GLboolean constrainPitch)
{
if (constrainPitch) {
if (pitch > 89.0f) {
pitch = 89.0f;
} else if (pitch < -89.0f) {
pitch = -89.0f;
}
}
// if yaw is outside the conventional range (-180.0, 180.0], shift it
if (yaw < -180.0f || yaw > 180.0f) {
yaw -= floor((yaw + 180.0f) / 360.0f) * 360.0f;
}
m_Yaw = yaw;
m_Pitch = pitch;
UpdateViewMatrix(true);
}
void Camera::ProcessMouseMovement(GLfloat xOffset, GLfloat yOffset, GLboolean constrainPitch)
{
UpdateAngles(m_Yaw + xOffset * sensitivity,
m_Pitch + yOffset * sensitivity,
constrainPitch);
}
for mouse follow you need:
camera = inverse(player * camera_view_and_offset)
where player is your player direct matrix, camera_view_and_offset is the view offset and turn around matrix relative to your player coordinate system and camera is your camera inverse matrix you should use as part of modelview ....
for more info see:
Understanding 4x4 homogenous transform matrices

Limiting rotation around the x-axis in opengl

For fun, I've made a 3d camera in opengl. It works well, except for the fact that I cannot figure out how to limit rotation about the x-axis. If you scroll up too much, the up and down controls will invert. I've tried limiting the camFront.y variable to an arbitrary value, but the camera will still flip over the x-axis.
Here is my code:
#ifndef CAMERA_H
#define CAMERA_H
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>
#define WORLD_UP glm::vec3(0.0f, 1.0f, 0.0f)
#include <iostream>
enum CamDirection {
CAM_FORWARD,
CAM_BACKWARD,
CAM_LEFT,
CAM_RIGHT
};
class Camera {
public:
void cameraUpdate();
glm::mat4 getViewMatrix();
Camera();
Camera(glm::vec3 startPosition);
void move(CamDirection dir, GLfloat deltaTime);
void look(double xOffset, double yOffset);
void update();
private:
glm::vec3 camPos;
glm::vec3 camFront;
glm::vec3 camUp;
glm::vec3 camRight;
const GLfloat camSpeed = 5.05f;
};
glm::mat4 Camera::getViewMatrix() {
return glm::lookAt(camPos, camPos + camFront, camUp);
}
Camera::Camera():
camPos (glm::vec3(0.0f, 0.0f, 0.0f)),
camFront(glm::vec3(0.0f, 0.0f, -1.0f)),
camUp (WORLD_UP)
{}
Camera::Camera(glm::vec3 startPos):
camPos (startPos),
camFront (glm::vec3(0.0f, 0.0f, -1.0f)),
camUp (WORLD_UP)
{}
void Camera::move(CamDirection dir, GLfloat deltaTime) {
const GLfloat v = camSpeed * deltaTime;
if (dir == CAM_FORWARD)
camPos += v * camFront;
else if (dir == CAM_BACKWARD)
camPos -= v * camFront;
else if (dir == CAM_RIGHT)
camPos += v * camRight;
else
camPos -= v * camRight;
}
void Camera::look(double xOffset, double yOffset) {
glm::quat startQuat = {0, camFront.x, camFront.y, camFront.z};
glm::quat rotation = glm::angleAxis((GLfloat)xOffset, glm::vec3(0.0f, 1.0f, 0.0f));
glm::quat view = startQuat * rotation;
rotation = glm::angleAxis((GLfloat)yOffset, glm::vec3(-1.0f, 0.0f, 0.0f));
view = view * rotation;
camFront = glm::vec3(view.x, view.y, view.z);
std::cerr << camFront.x << ' ' << camFront.y << ' ' << camFront.z << std::endl;
}
void Camera::update() {
// Also re-calculate the Right and Up vector
camRight = glm::normalize(glm::cross(camFront, WORLD_UP)); // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
camUp = glm::normalize(glm::cross(camRight, camFront));
}
#endif // CAMERA_H
How can I fix this?
You need to limit view.y and view.z values before assigning them to camFront, to be max 89 degrees and minimum -89 degrees. At 90 and -90 degrees it starts to invert. So a very simple approach could be,
if(view.y > 89)
{
view.y = 89;
}
if(view.y < -89)
{
view.y = -89;
}
if(view.z > 89)
{
view.z = 89;
}
if(view.z < -89)
{
view.z = -89;
}

Calculation for View Matrix in C++ DirectX 9

I'm trying to create a first-person shooter camera for my DirectX game. But I'm struggling in trying to get my view matrix right. At the moment I have only got a triangle to display on the screen, but when I rotate the camera and move forward it seems like the triangle is rotating around the camera instead. Here is the code I have done for the Camera Class
Camera.h
#pragma once
#include"Window.h"
#include"Matrix.h"
#include "Vector3.h"
class Camera
{
public:
Camera();
Camera(float pitch, float yaw, Vector3 position);
~Camera();
void Update();
D3DXMATRIX BuildViewMatrix();
Vector3 GetPosition() const { return position; }
void SetPosition(Vector3 val) { position = val; }
protected:
float yaw;
float pitch;
Vector3 position;
};
Camera.cpp
#include"Camera.h"
Camera::Camera()
{
yaw = 0.0f;
pitch = 0.0f;
}
Camera::Camera(float pitch, float yaw, Vector3 position)
{
this->pitch = pitch;
this->yaw = yaw;
this->position = position;
}
Camera::~Camera()
{
}
void Camera::Update()
{
pitch = min(pitch, 90.0f);
pitch = max(pitch, -90.0f);
if(yaw < 0)
yaw += 360.0f;
if( yaw > 360.0f)
yaw -= 360.0f;
if(Window::GetKeyboard()->KeyDown(KEYBOARD_W))
{
D3DXMATRIX translateMat;
D3DXMatrixTranslation(&translateMat, position.x, position.y, position.z);
D3DXMATRIX rotationMat;
D3DXMatrixRotationY(&rotationMat, D3DXToRadian(yaw));
D3DXMATRIX forwardMat;
D3DXMatrixTranslation(&forwardMat, 0.0f, 0.0f, 10.0f);
D3DXMATRIX transformMat = translateMat * rotationMat * forwardMat;
position.x = transformMat._41;
position.y = transformMat._42;
position.z = transformMat._43;
std::cout << "X: " << transformMat._41 << std::endl;
std::cout << "Y: " << transformMat._42 << std::endl;
std::cout << "Z: " << transformMat._43 << std::endl;
}
else if(Window::GetKeyboard()->KeyDown(KEYBOARD_S))
{
D3DXMATRIX translateMat;
D3DXMatrixTranslation(&translateMat, position.x, position.y, position.z);
D3DXMATRIX rotationMat;
D3DXMatrixRotationY(&rotationMat, D3DXToRadian(yaw));
D3DXMATRIX forwardMat;
D3DXMatrixTranslation(&forwardMat, 0.0f, 0.0f, -10.0f);
D3DXMATRIX transformMat = translateMat * rotationMat * forwardMat;
position.x = transformMat._41;
position.y = transformMat._42;
position.z = transformMat._43;
std::cout << "X: " << transformMat._41 << std::endl;
std::cout << "Y: " << transformMat._42 << std::endl;
std::cout << "Z: " << transformMat._43 << std::endl;
}
if(Window::GetKeyboard()->KeyDown(KEYBOARD_Q))
{
yaw = yaw - 1.0f;
}
if(Window::GetKeyboard()->KeyDown(KEYBOARD_E))
{
yaw = yaw + 1.0f;
}
}
D3DXMATRIX Camera::BuildViewMatrix()
{
D3DXMATRIX translationMat;
D3DXMatrixTranslation(&translationMat, (position.x * -1.0f), (position.y * -1.0f), (position.z * -1.0f));
D3DXMATRIX yawMat;
D3DXMatrixRotationY(&yawMat, D3DXToRadian((yaw * -1.0f)));
D3DXMATRIX pitchMat;
D3DXMatrixRotationX(&pitchMat, D3DXToRadian((pitch * -1.0f)));
D3DXMATRIX viewMat = pitchMat * yawMat * translationMat;
return viewMat;
}
I'm using device->SetTransform(D3DTS_VIEW, &camera->BuildViewMatrix()); to send the view matrix to directX. What exactly am I doing wrong? Please help
UPDATE:
I have change the code and tried to use the D3DXMatrixLookAtLH function (doesn't use the Camera at all), but this time, it doesn't do anything - the triangle stays in the same position no matter how I change the LookAt parameter on the function. Here is the code for it:
void Renderer::Render()
{
device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(40, 40, 40), 1.0f, 0);
device->BeginScene(); //Must be used as it tells DirectX we're starting to draw stuff.
D3DXMATRIX worldMat;
D3DXMatrixTranslation(&worldMat, 0.0f, 0.0f, 0.0f);
device->SetTransform(D3DTS_WORLD, &worldMat);
D3DXMATRIX viewMatrix;
D3DXMatrixLookAtLH( &viewMatrix,
&D3DXVECTOR3(0.0f, -100.0f, 1000.0f), //position
&D3DXVECTOR3(0.0f, 1.0f, 0.0f), //Look at
&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
device->SetTransform(D3DTS_VIEW, &viewMatrix);
//device->SetTransform(D3DTS_VIEW, &camera->BuildViewMatrix());
D3DXMATRIX projMatrix;
D3DXMatrixPerspectiveFovLH( &projMatrix,
D3DXToRadian(45),
(float)width/(float)height,
1.0f,
10000.0f);
device->SetTransform(D3DTS_PROJECTION, &projMatrix);
device->SetFVF(VERTEXFORMAT);
device->SetStreamSource(0, vertexBuffer, 0, sizeof(VERTEX));
device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
device->EndScene(); //Thank you for waiting, I have finished drawing stuff on the screen, please handle the rest Mr DirectX.
device->Present(NULL, NULL, NULL, NULL);
}
Found out the problem, the matrices I've calculated isn't correct as well as the view matrix. the fixed it by adding the following codes:
Camera.h
#pragma once
#pragma comment(lib, "d3dx9.lib")
#include<d3dx9math.h>
#include"Window.h"
class Camera
{
public:
Camera();
Camera(float pitch, float yaw, D3DXVECTOR3 position);
~Camera();
void Update();
D3DXMATRIX BuildViewMatrix();
inline D3DXVECTOR3 GetPosition() const { return position; }
inline void SetPosition(D3DXVECTOR3 position){ this->position = position;}
protected:
float yaw;
float pitch;
float roll;
D3DXVECTOR3 position;
private:
D3DXVECTOR3 xAxis;
D3DXVECTOR3 yAxis;
D3DXVECTOR3 zAxis;
};
Camera.cpp
#include"Camera.h"
Camera::Camera()
{
yaw = 0.0f;
pitch = 0.0f;
roll = 0.0f;
}
//yaw = head twist
//pitch = head tilt
Camera::Camera(float pitch, float yaw, D3DXVECTOR3 position)
{
this->pitch = pitch;
this->yaw = yaw;
roll = 0.0f;
this->position = position;
xAxis = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
yAxis = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
zAxis = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
}
Camera::~Camera()
{
}
void Camera::Update()
{
if(Window::GetKeyboard()->KeyDown(KEYBOARD_Q)){
yaw -= 1.0f;
}
else if(Window::GetKeyboard()->KeyDown(KEYBOARD_E)){
yaw += 1.0f;
}
if(Window::GetKeyboard()->KeyDown(KEYBOARD_Z)){
pitch += 1.0f;
}
else if(Window::GetKeyboard()->KeyDown(KEYBOARD_X)){
pitch -= 1.0f;
}
pitch = min(pitch, 90.0f);
pitch = max(pitch, -90.0f);
if(yaw < 0.0f)
yaw += 360.0f;
if( yaw > 360.0f)
yaw -= 360.0f;
D3DXMATRIX yawMat;
D3DXMatrixRotationAxis(&yawMat, &yAxis, D3DXToRadian(yaw));
D3DXVec3TransformCoord(&zAxis, &zAxis, &yawMat);
D3DXVec3TransformCoord(&xAxis, &xAxis, &yawMat);
D3DXMATRIX pitchMat;
D3DXMatrixRotationAxis(&pitchMat, &xAxis, D3DXToRadian(pitch));
D3DXVec3TransformCoord(&zAxis, &zAxis, &pitchMat);
D3DXVec3TransformCoord(&yAxis, &yAxis, &pitchMat);
D3DXMATRIX rollMat;
D3DXMatrixRotationAxis(&rollMat, &zAxis, D3DXToRadian(0.0f));
D3DXVec3TransformCoord(&xAxis, &xAxis, &rollMat);
D3DXVec3TransformCoord(&yAxis, &yAxis, &rollMat);
if(Window::GetKeyboard()->KeyDown(KEYBOARD_W))
{
position = position + (zAxis * -1) * 10.0f;
}
else if(Window::GetKeyboard()->KeyDown(KEYBOARD_S))
{
position = position + zAxis * 10.0f;
}
if(Window::GetKeyboard()->KeyDown(KEYBOARD_A))
{
position = position + xAxis * 10.0f;
}
else if(Window::GetKeyboard()->KeyDown(KEYBOARD_D))
{
position = position - xAxis * 10.0f;
}
yaw = 0.0f;
pitch = 0.0f;
roll = 0.0f;
}
D3DXMATRIX Camera::BuildViewMatrix()
{
D3DXMATRIX viewMatrix;
D3DXMatrixIdentity(&viewMatrix);
viewMatrix._11 = xAxis.x;
viewMatrix._21 = xAxis.y;
viewMatrix._31 = xAxis.z;
viewMatrix._12 = yAxis.x;
viewMatrix._22 = yAxis.y;
viewMatrix._32 = yAxis.z;
viewMatrix._13 = zAxis.x;
viewMatrix._23 = zAxis.y;
viewMatrix._33 = zAxis.z;
viewMatrix._41 = D3DXVec3Dot(&position, &xAxis);
viewMatrix._42 = D3DXVec3Dot(&position, &yAxis);
viewMatrix._43 = D3DXVec3Dot(&position, &zAxis);
return viewMatrix;
}

Camera Translation in OpenGL

I am trying to do a class to walk through the world in OpenGL, but I am having problems with the mathematics. My idea here is to use the function lookAt from glm to set the observer in the position I wanted, and then just operate with the points that I pass to the function.
I think the functions to do rotations that I made are correct, but the translation part in the walk method seems to be wrong, and when I try to walk in the world if I just translate, or just rotated, things go right, but when I do both things just get messed.
here is the class so far:
#ifndef OBSERVER_H
#define OBSERVER_H
#include <GL/gl.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
class Observer {
private:
glm::vec3 eye, center, upp;
public:
glm::mat4 view;
Observer() {}
~Observer() {}
void initialize(glm::vec3 eye, glm::vec3 center, glm::vec3 upp);
void walk(GLfloat distance);
void pitch(GLfloat pitch);
void yaw(GLfloat yaw);
void roll(GLfloat roll);
void setView();
};
void Observer::initialize(glm::vec3 eye, glm::vec3 center, glm::vec3 upp)
{
this->eye = eye;
this->center = center;
this->upp = upp;
}
void Observer::walk(GLfloat distance)
{
glm::vec3 vector = glm::normalize(center - eye);
glm::vec3 translate = vector*distance - vector;
eye += translate;
center += translate;
upp += translate;
}
void Observer::roll(GLfloat roll) {
glm::mat4 rotate(1.0f);
rotate = glm::rotate(rotate, roll, glm::vec3(center - eye));
center = glm::vec3(rotate * glm::vec4(center, 1.0f));
upp = glm::vec3(rotate * glm::vec4(upp, 1.0f));
}
void Observer::yaw(GLfloat yaw) {
glm::mat4 rotate(1.0f);
rotate = glm::rotate(rotate, yaw, glm::vec3(upp - eye));
center = glm::vec3(rotate * glm::vec4(center, 1.0f));
upp = glm::vec3(rotate * glm::vec4(upp, 1.0f));
}
void Observer::pitch(GLfloat pitch) {
glm::mat4 rotate(1.0f);
glm::vec3 cross = glm::cross(center - eye, upp - eye);
rotate = glm::rotate(rotate, pitch, cross);
center = glm::vec3(rotate * glm::vec4(center, 1.0f));
upp = glm::vec3(rotate * glm::vec4(upp, 1.0f));
}
void Observer::setView()
{
view = glm::lookAt(eye, center, upp);
}
#endif
So right before I starting draw things I set the view matrix with this class in other part in the program. Can someone tell me if my maths are right?
When you walk, you only want to transform the eye and center position, not the upp vector. Just remove the upp += translate; line.