OpenGL rotating a camera around a point

74,146

Solution 1

You need to either:

  • rotate the camera around the origin and then translate it (*)

or:

  • use gluLookAt to keep the camera pointing at the center of the circle

(*) rotation functions normally rotate about the origin. To rotate around another point P you have to:

  • translate(-P)
  • rotate
  • translate(P)

Solution 2

it is a little confusing, but i think you should:

// move camera a distance r away from the center
glTranslatef(0, 0, -r);

// rotate 
glRotatef(angley, 0, 1, 0);
glRotatef(anglex, 1, 0, 0);

// move to center of circle    
glTranslatef(-cx, -cy, -cz)

note the order must NOT be changed.

Solution 3

Why bother with all the trouble of rotating the camera and not rotate the scene itself?
It's much more straight forward. just rotate the modelview matrix around the origin. You'll get the exact same result.

Solution 4

I find problems like this much easier to solve with gluLookAt(). You define a path for the camera (a circle is easy!) and keep the "center" point fixed (i.e. the thing you're looking at).

The only possible trick is defining a good up vector--but not usually too much work. If the path and the target point are in the same plane, you can use the same up vector each time!

Share:
74,146
Bryan Denny
Author by

Bryan Denny

I am a senior web developer who specializes in .NET, C#, SQL, MVC, Web Forms, Web API and WCF. I design and maintain large web applications from the UI front-end to the code and database back-end. I also have experience developing and publishing Java Android applications. For more information, check out my resume and portfolio website: http://www.bryandenny.com

Updated on November 04, 2020

Comments

  • Bryan Denny
    Bryan Denny over 3 years

    In OpenGL I'm trying to rotate a camera around a point, with camera being distance r from the point and facing the point when it rotates. In other words, I want the camera to move along the circumference of a circle at a radius r from the center, with the camera facing the center at any point along the circumference.

    Lets say that in 3d space the center of the circle is (3, 0, 3);

    I've tried:

    // move to center of circle    
    glTranslatef(-3, 0, -3)
    // move a distance away from the circle
    glTranslatef(0, 0, r);
    // rotate along the y "up" axis
    glRotatef(CameraAngle, 0, 1, 0);
    

    where CameraAngle is the degrees being moved around the circle.

    My end result is the camera is still rotating along the origin, not the center of the circle. Can anyone help me fix this problem? Thanks!