PDA

View Full Version : rotate around selected object



schwinn
03-18-2002, 10:48 AM
I use the mouse to select an object which the camera then looks at.
I use gluLookAt( cameraX, cameraY, cameraZ, targetX, targetY, targetZ, upX, upY, upZ )
where upX = 0 upY = 0 upZ = 1

That works fine.
Now I want to rotate the camera around the selected object. The selected object is in the middle of my view & the camera is looking at it. Whenever I rotate the camera, I want it to always be looking at the selected object in the middle of the view.

The problem is the camera rotates around but the selected object is no longer in the middle of the view. The camera seems to always look at the middle of the view 0,0,0 rather than at the object.
To do that I translate the camera & target to the origin, increase my horizontal angle, get the new look vector & translate back.
Here's the code which does it:

cameraX -= targetX;
cameraY -= targetY;
cameraZ -= targetZ;

// target point is 0,0,0
lookVector[0] = 0 - cameraX;
lookVector[1] = 0 - cameraY;
lookVector[2] = 0 - cameraZ;

double length = sqrt((lookVector[0] * lookVector[0]) + (lookVector[1] * lookVector[1]) + (lookVector[2] * lookVector[2]));

double horzAngle = (atan2(lookVector[1], lookVector[0]));

double vertAngle = (acos(lookVector[2] / length));

horzAngle += panSpeed*(pi/180);
if (horzAngle >= (2*pi))
horzAngle -= (2*pi);

double newVector[3];
newVector[0] = length * sin(vertAngle) * cos(horzAngle);
newVector[1] = length * sin(vertAngle) * sin(horzAngle);
newVector[2] = length * cos(vertAngle);

// target point is 0,0,0
cameraX = 0 - newVector[0];
cameraY = 0 - newVector[1];
cameraZ = 0 - newVector[2];

cameraX += targetX;
cameraY += targetY;
cameraZ += targetZ;

Any ideas how to rotate & always look at what's selected?

Thug
03-25-2002, 06:02 AM
Use the gluLookAt(...) with the approperate params.