Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL rotate around a spot

I want to rotate a gluSphere around a fixed point in a circular motion, like a planet going around the sun.

Would it be best to use glRotatef or glTranslate? If so, in which order should I call them?

like image 742
user64392 Avatar asked Apr 24 '09 15:04

user64392


People also ask

How do you rotate an object around a point in OpenGL?

To rotate around a different point, the formula: X = cx + (x-cx)*cosA - (y-cy)*sinA, Y = cy + (x-cx)*sinA + (y-cy)*cosA, cx, cy is centre coordinates, A is the angle of rotation. The OpenGL function is glRotatef (A, x, y, z). 3. Scaling: Scaling refers to zooming in and out an object on different scales across axes.

What is glRotate?

The glRotatef function computes a matrix that performs a counterclockwise rotation of angle degrees about the vector from the origin through the point (x, y, z). The current matrix (see glMatrixMode) is multiplied by this rotation matrix, with the product replacing the current matrix.


1 Answers

You'll have to do a little of both:

  • Make sure the gluSphere is "facing" the fixed point, so that translating forward with respect to the sphere puts you closer to the center of its orbit
  • glTranslatef the gluSphere forward to the point around which you want it to rotate
  • glRotatef the direction you want the sphere to orbit
  • glTranslatef backwards just as far as you went forward

That way, your sphere stays the same distance from the center, but gets translated "around" in a nice orbit.

like image 76
ojrac Avatar answered Oct 22 '22 10:10

ojrac