Archived

This topic is now archived and is closed to further replies.

rotate around selected object

This topic is 5752 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

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?

Share this post


Link to post
Share on other sites
Hi there.

First of all, I do not know exactly where your problem lies though your always looking to the target position. (perhaps not clearing the matrix to identity before gluLookAting when drawing? ) I think your code should work.

Different solution:
Since you rotate around the z-axis your z position never changes (is this what you want? )

with the first 3 lines you get a vector which points from target to camera. All you now have to do is rotate this vector around the z-axis. You can do this by hand or use OpenGL to compute the resulting direction.

By hand:

cam -= target // taken from your code

float dangle = panSpeed*(pi/180);

newCamX = cos(dangle)*camX - sin(dangle)*camY; // taken from rotation Matrix
newCamY = sin(dangle)*camX + cos(dangle)*camY; // taken from rotation Matrix
newCamZ = camZ; // does not change

newCam += target; // taken from your code

With this you get rid of computing angles and square roots But I think the problem remains.

Good luck

[edited by - NextS on March 18, 2002 8:06:10 PM]

Share this post


Link to post
Share on other sites
Thanks.
I figured it out though.
Whenever I select an object, I slowly pan the camera over to the new object & I was calculating the difference between the angles of the current look vector & the new look vector & then slowly incrementing the angles until I got to the new position but I forgot to find the difference of the lengths of the look vectors & slowly increment the distance. Once I did that, it worked.

Share this post


Link to post
Share on other sites