• Advertisement
Sign in to follow this  

Rotating around a point

This topic is 3234 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have a camera able to freely move around the world and I'm trying to add the possibility to rotate around a point/target. In agreement with the theory explained here: http://www.euclideanspace.com/maths/geometry/affine/aroundPoint/index.htm I wrote the following code:
void CCamera::SetModelViewMatrix()
{
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
	
  CVector position = GetPosition();
  CVector forward = GetForward();
  CVector up = GetUp();

  if(!hasTarget)
  {
    gluLookAt(
      position.x, position.y, position.z,
      position.x + forward.x, position.y + forward.y, position.z + forward.z,
      up.x, up.y, up.z);
  }
  else
  {
    gluLookAt(
      position.x, position.y, position.z,
      targetPoint.x, targetPoint.y, targetPoint.z,
      up.x, up.y, up.z);
  }
}

const CVector& CCamera::GetPosition() const
{
  return transformation.translation;
}

CVector CCamera::GetUp() const
{
  return transformation.rotation * CVector(0.0f, 1.0f, 0.0f);
}

CVector CCamera::GetForward() const
{
  return transformation.rotation * CVector(0.0f, 0.0f, 1.0f);
}

void CCamera::Rotate(float rotX, float rotY)
{
  pitch = fmod(pitch + rotX, 360.0f);
  yaw = fmod(yaw + rotY, 360.0f);
  CMatrix3f rot = CreateRotationYXZMatrix(pitch, yaw, 0.0f);

  // The rotation is the same with or without target
  transformation.rotation = rot; 

  if(hasTarget)
  {
    /*
	r00, r01, r02, x - r00*x - r01*y - r02*z
	r10, r11, r12, y - r10*x - r11*y - r12*z
	r20, r21, r22, z - r20*x - r21*y - r22*z
	0,   0,   0,   1
    */
    CVector transl = targetPoint - transformation.translation;
		
    transformation.translation.x =
      transl.x - rot(0,0)*transl.x - rot(0,1)*transl.y - rot(0,2)*transl.z;
    transformation.translation.y =
      transl.y - rot(1,0)*transl.x - rot(1,1)*transl.y - rot(1,2)*transl.z;
    transformation.translation.z =
      transl.z - rot(2,0)*transl.x - rot(2,1)*transl.y - rot(2,2)*transl.z;
  }
}

As I said, the rotation without target works, while I can't understand what's wrong with a target. Rotating with a target set, the only thing I see is that the camera goes quickly further away.. this makes me think the problem could be in: CVector transl = targetPoint - transformation.translation; but that's actually the vector between the camera and the target. Any help is really appreciated. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement