Rotate an object around a distant point.

I am trying to rotate an object (say a sphere at 3,3,3) around a point (say at 1,1,1). The rotation can go any arbitary direction, I think (not sure) I should use two angle to decide the direction (angel to rotate on xy plane and xz plane).
Can anyone help me?

note: I need to rotate the object to any direction, not the axis.

sincerely

Hi there!

To rotate an object around an arbitary position in 3D space - the best way of doing that is to consider the concept of Local and World coordinate systems and order of operations inside a pushed matrix.

Let’s look at what you should have.

a DrawSphere() function that draws a sphere with radius r around the local coordinate system point (0,0)

glTranslatef(); function that we will use to move the sphere to a given point

and glRotate3f(); function that will rotate the object at a specified angle in any of the three axis.

It’s simply a matter of putting those functions as a correct combination inside it’s own transformation Matrix, remembering that we do this in REVERSE order to how we could conceptually see it:

So the code for your problem:

  
glPushMatrix();
glTranslatef(sphereMid.x, sphereMid.y, sphereMid.z);
glRotatef(angle, xAxis, yAxis, zAxis);
glTranslatef(rotpoint.x, rotpoint.y, rotpoint.z);
DrawSphere();
glPopMatrix();

Looking at this we have firstly:

  1. Draw Sphere in local coordinate space.

  2. Translate the sphere to the desired offset from 0,0.

  3. Perform the rotation, as essentially the world coordinate origin now represents out rotation point.

  4. Translate the sphere to it’s actual position in 3D space.

You can move the rotation axis to any point using glTranslate.

You must remember that matrix calls work in reverse, and all rotations are done from 0,0,0.

glTranslate(…); Where the new axis of rotation is the be, example if we wanted to orbit around 10,10,10 in stead of 0,0,0
glRotate(…); Rotate sphere from some Radius (or think of orbit of a planet around the sun, the sun beening 0,0,0)
glTranslate(…); Radius of rotation, from 0,0,0
glRotate(…); Rotate sphere on it’s axis
Draw_sphere();

I tried the code u given, it ends up he object is rotating around distant point’s AXIS rather than the around the distant point itself.
:frowning:

My code or the other posters?

Just to clear things up do you want to object to spin on its axis at some point or orbit like a moon around the earth at that point?

Originally posted by Fier:
I tried the code u given, it ends up he object is rotating around distant point’s AXIS rather than the around the distant point itself.
:frowning:

ok, I got it figured out.
I want it like moon rotate the earth, but in different path instead of a single path.