Jump to content

  • Log In with Google      Sign In   
  • Create Account

Possibly a stupidly naive mistake re: camera rotation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Lil_Lloyd   Members   -  Reputation: 287

Like
0Likes
Like

Posted 15 July 2012 - 08:11 PM

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!

Edited by Lil_Lloyd, 15 July 2012 - 08:13 PM.


Sponsor:

#2 RulerOfNothing   Members   -  Reputation: 1160

Like
0Likes
Like

Posted 16 July 2012 - 03:21 AM

Well, what are the values of m_eyePoint and m_lookPoint?

#3 Lil_Lloyd   Members   -  Reputation: 287

Like
0Likes
Like

Posted 16 July 2012 - 08:21 PM

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.

#4 Hodgman   Moderators   -  Reputation: 30441

Like
0Likes
Like

Posted 16 July 2012 - 08:30 PM

Is m_lookPoint.x ever set to anything?

#5 Lil_Lloyd   Members   -  Reputation: 287

Like
0Likes
Like

Posted 16 July 2012 - 08:52 PM

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));
}


Edited by Lil_Lloyd, 16 July 2012 - 08:53 PM.


#6 RulerOfNothing   Members   -  Reputation: 1160

Like
0Likes
Like

Posted 16 July 2012 - 08:57 PM

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?

Edited by RulerOfNothing, 17 July 2012 - 01:09 AM.


#7 Lil_Lloyd   Members   -  Reputation: 287

Like
1Likes
Like

Posted 17 July 2012 - 02:12 AM

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);
}





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS