PDA

View Full Version : Rotate an object around a distant point.

Fier
03-30-2004, 11:35 AM
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

strik
03-30-2004, 03:14 PM
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.

nexusone
03-30-2004, 03:18 PM
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();

Fier
03-31-2004, 11:06 AM
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.
:(

nexusone
03-31-2004, 03:50 PM
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.
:(

Fier
04-01-2004, 12:10 PM
ok, I got it figured out.
I want it like moon rotate the earth, but in different path instead of a single path.