Possibly a stupidly naive mistake re: camera rotation

Started by
5 comments, last by Lil_Lloyd 11 years, 9 months ago
Hi. So I have rolled out a mini camera class. For rotating the camera I thought along the lines of a unit circle, where the sin of an angle gives one dimension and the cos of an angle gives another, so if you went through all 360 degrees all the points on a unit circle would be plotted.

HOWEVER, I just get an oscillating motion for some reason....

the main example code is here



//rotate around the x axis, so the y and z values change over time t

void Camera::RotateX(float angle)
{
m_xRotation += angle;

if(m_xRotation > 360.0f)
m_xRotation -= 360.0f;

if(m_xRotation < 0.0f)
m_xRotation += 360.0f;

m_lookPoint.y = glm::sin(glm::radians(m_xRotation));
m_lookPoint.z = glm::cos(glm::radians(m_xRotation));
m_lookVec = glm::normalize(m_lookPoint - m_eyePoint);

UpdateViewMatrix();
}
void Camera::UpdateViewMatrix()
{
m_rightVec = glm::normalize(glm::cross(vec3(0.0f,1.0f,0.0f),m_lookVec));

m_upVec = glm::normalize(glm::cross(m_rightVec,m_lookVec));

viewMatrix = glm::lookAt(m_eyePoint,m_lookPoint,vec3(0.0f,1.0f,0.0f));
}


It's obviously something REALLY simple i'm overlooking, any input is appreciated, thanks!
Advertisement
Well, what are the values of m_eyePoint and m_lookPoint?
m_eyePoint is the current camera 'position' and m_lookPoint is the point the camera is pointing at, so the lookVector is obtained by doing m_eyePoint - m_lookPoint.
Is m_lookPoint.x ever set to anything?
m_lookPoint.x is changed in other rotation functions. Here is the entire class:


using glm::vec3;
using glm::mat4;
class Camera
{
public:
Camera(const vec3& eyePoint,const vec3& lookPoint);
void Translate(const vec3& translation);
void RotateX(float angle);
void RotateY(float angle);
void RotateZ(float angle);
void MoveForward(float distance);
void Strafe(float distance);
const mat4& GetMatrix()const {return viewMatrix;}
private:
void UpdateViewDir();
void UpdateViewMatrix();
vec3 m_eyePoint;
vec3 m_lookVec;
vec3 m_upVec;
vec3 m_rightVec;
vec3 m_lookPoint;
float m_xRotation;
float m_yRotation;
float m_zRotation;
mat4 viewMatrix;
};
//Camera.cpp - implementation
//last update 16/07/2012
#include "Camera.h"

Camera::Camera(const vec3& eyePoint,const vec3& lookPoint)
{
m_eyePoint = eyePoint;
m_lookPoint = lookPoint;
m_lookVec = glm::normalize(lookPoint-eyePoint);
m_xRotation = 180.0f;
m_yRotation = 0.0f;
m_zRotation = 0.0f;
UpdateViewMatrix();
}
void Camera::Translate(const vec3& translation)
{
m_eyePoint += translation;
UpdateViewMatrix();
}
void Camera::RotateX(float angle)
{
m_xRotation += angle;
if(m_xRotation > 360.0f)
m_xRotation -= 360.0f;
if(m_xRotation < 0.0f)
m_xRotation += 360.0f;
m_lookPoint.y = glm::sin(glm::radians(m_xRotation));
m_lookPoint.z = glm::cos(glm::radians(m_xRotation));
m_lookVec = glm::normalize(m_lookPoint - m_eyePoint);
UpdateViewMatrix();
}
void Camera::RotateY(float angle)
{
m_yRotation += angle;
if(m_yRotation > 360.0f)
m_yRotation -= 360.0f;
if(m_yRotation < 0.0f)
m_yRotation += 360.0f;
UpdateViewDir();
UpdateViewMatrix();
}
void Camera::RotateZ(float angle)
{
m_zRotation += angle;
if(m_zRotation > 360.0f)
m_zRotation -= 360.0f;
if(m_zRotation < 0.0f)
m_zRotation += 360.0f;
UpdateViewDir();
UpdateViewMatrix();
}
void Camera::MoveForward(float distance)
{
m_eyePoint.z += distance;
m_lookPoint.z += distance;
UpdateViewMatrix();
}
void Camera::Strafe(float distance)
{
m_eyePoint.x += distance;
m_lookPoint.x += distance;
UpdateViewMatrix();
}

void Camera::UpdateViewDir()
{

m_lookVec = m_lookPoint - m_eyePoint;
}
void Camera::UpdateViewMatrix()
{
m_rightVec = glm::normalize(glm::cross(vec3(0.0f,1.0f,0.0f),m_lookVec));
m_upVec = glm::normalize(glm::cross(m_rightVec,m_lookVec));
viewMatrix = glm::lookAt(m_eyePoint,m_lookPoint,vec3(0.0f,1.0f,0.0f));
}


m_eyePoint is the current camera 'position' and m_lookPoint is the point the camera is pointing at, so the lookVector is obtained by doing m_eyePoint - m_lookPoint.

I was asking about m_eyePoint and m_lookPoint because you don't actually use m_lookVec when you calculate viewMatrix.

EDIT: There is something else that may help in solving this problem: What is m_eyePoint set to at construction?
Hey thank you for your advice and suggestions, I managed to solve my problem. The look point was being changed as if it was following a unit circle so the values calculated had a min of -1 and a max of +1. Since my eyepoint had a z value of -8.5 the result of lookPoint - eyePoint for the look Vector was ALWAYS negative. I changed the code to the below, and it now works fine!


void Camera::RotateX(float angle)
{
m_xRotation += angle;
if(m_xRotation > 360.0f)
m_xRotation -= 360.0f;
if(m_xRotation < 0.0f)
m_xRotation += 360.0f;
m_lookVec.y = glm::sin(glm::radians(m_xRotation));
m_lookVec.z = glm::cos(glm::radians(m_xRotation));
m_lookPoint = m_eyePoint + m_lookVec;
m_upVec = glm::cross(m_rightVec,m_lookVec);
m_viewMatrix = glm::lookAt(m_eyePoint,m_lookPoint,m_upVec);
}

This topic is closed to new replies.

Advertisement