# Rotating around a point

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

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

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.

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 13
• ### Forum Statistics

• Total Topics
632956
• Total Posts
3009452
• ### Who's Online (See full list)

There are no registered users currently online

×