Archived

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

Rotate with respect to a distant point

This topic is 5009 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 am trying to rotate an object, say sphere, with respect to a distant point. Bascially, it''s like the distant point is the center of a circle, and the object is travel on the circle''s edge. This has to be done in 3D. If anyone can show me how to do, I would be very very very very thankful.

Share this post


Link to post
Share on other sites
No, I tried that already, this would just make the object rotate around the distant point''s axis, not the distant point itself.

Share this post


Link to post
Share on other sites
If the object you want to rotate around is at 0,0,0 and the object that must rotated around is ten units away, then first move to 0,0,0. Next rotate however much you want. Next translate 10 units away and draw orbiting. This certainly -should- work.

Share this post


Link to post
Share on other sites
ok, i set move to the rotate point at (0,0,0). Rotate (degree+changingX), where changeX is constatntly incrase 1 degree per frame, with respect to z axis. Then translate to the piont at (3,3,3). What happens is the object is rotateing around (0,0,0)''s z axis instead of the point (0,0,0) itself. So it''s not working.

Share this post


Link to post
Share on other sites
transform your object into the other object''s world-space (i.e. NOT to it''s position but make your coordinates offsets of the other objects center). then do your rotate around the other object''s axis. then re-transform back to world coordinates. just a couple of simple matrix operations.

-me

Share this post


Link to post
Share on other sites
Dear Palidine, can you please be a little more specific. Thanks a lot.
Please correct me if I am wrong. I think what you saying is:

//move to distant point
glTranslatef(0,0,0);
//save position
glPushMatrix();
//rotate angle, constantly increasing
glRotatef(changingXY, 0,0,1);
glRotatef(changingZY, 0,1,0);
//move the the distant obj
glTranslatef(distant obj''s position - distant point);
DrawObject;
//restore to world view
glPopMatrix();

Did I understant correctly?

Share this post


Link to post
Share on other sites
no.

the first step is wrong. you need to transform your model into the local coordinate system of the distant object. this is NOT the same as moving it to that location.

say i have a point at 1,1,1 and another point at 3,3,3. the first step is to transform 3,3,3 into (1,1,1)'s worldspace. from the perspective of (1,1,1), (3,3,3) is really (2,2,2). make sense? you basically just subtract (1,1,1) from (3,3,3) to get (3,3,3)'s relative position with respect to (1,1,1).

THEN do your rotation.

then transform back to world space (mult by the inverse [is that the right word?] of the first matrix you multiplied by i believe).

offhand i can't tell you which matrix you use for that first part as matrices are currently not my strong point

-me

[edited by - Palidine on March 30, 2004 2:36:35 PM]

Share this post


Link to post
Share on other sites
ok, What I want to do is rotate an object(say at 3,3,3) around a point(say at 1,1,1). I think what you saying is this.

//save current position
glPushMatrix();
//move to point
glTranslatef(1,1,1);
//save position again;
glPushMatrix();
//Find out the relative position
newPosition = 3,3,3 - 1,1,1;
glTranslatef(newposition);
//go back to the point
glPopMatrix();
//rotate the point
glRotatef(angle, 0,0,1);
glRotatef(angle, 0,1,0);
glRotatef(angle, 1,0,0);
//restore to original position
glPopMatrx();


Is it right or I screwed up again?
I understand the relative position part. I draw those no problem. I think the hard part is to get the object to rotate aroudn the point and control what direction the object is oging to rotate.

sincerely

Share this post


Link to post
Share on other sites
quote:
Original post by Fier
Is it right or I screwed up again?



i don''t know, does it work? ;P

anyway, i think this is unnecessary:
//move to point
glTranslatef(1,1,1);

but i''m not really the guy to help you out anymore since we''re at the boundaries of my practical knowledge.

anyway,

Share this post


Link to post
Share on other sites
Nope, putting the rotate after all those drawing caused nothing to rotate at all

Do you know someone really good at this? plz plz help :''(

Share this post


Link to post
Share on other sites
Fier i think your last example was abit closer. The order is in reverse when you are doing matrix stuff. Like if you want something to rotate about an axis but its position is xyz then the order is translate rotate draw. but if you want it to move to a position, then rotate, its rotate translate draw then pop your matrix. So just re order your last code posting and in theory it should work, sorry not at my workstation rightnow to test it. now for how you translate, that should be rotation point + the distance you are rotating around.

If for example you are doing a planet and star: star is at 1,0,0; distance to rotate around is 10; call would be glTranslate(star.x + 10, 0, 0); now call your draw function; When you do all these matrix opperations you are changing the world coordinates briefly. But as far as your model is concerned, or the planet in our example, it is being drawn the same as if you just called your draw function by itself.

Now the way all this would work as a rotating object, since your actual position of the planet is not moving, is to rotate in your loop. You would have to change the angle for each iteration. so on 1st pass angle is 5 2nd pass angle is 10 etc. I hope this helps, if not, I''m kiinda stumped.

Share this post


Link to post
Share on other sites
You have to translate everything such that the point to rotate about goes to the origin. If your point is at (1,1,1), do a Translatef(-1,-1,-1), rotate, and Translate(1,1,1) again.

--nico

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
What you are doing is rotating a location vector about the world origin. To simulate what you are currently doing try this: (image that your fist is a sphere or whatever object you want) now stand up, make a fist and extend your arm. Now spin yourself around and around in a circle with your arm extended while at the same time retracting and extending your fist to and from your chest (bend you elbow). (Dumb example~)

WHAT YOU ARE DOING:

Given a world origin <0,0,0> rotate an object X around the Z axis. Object X is at location L <10, 10, 10>.

1. Translate Object X to the Origin.
2. Rotate The Object X Location Vector using a Z axis rotation matrix.
3. Translate Object X back to the Origin.

EXPLANATION:

The key here is that you are currently rotating the objects Location Vector and not the actual vertices or points that make up the object. ****What you want to do is rotate the actual point or vertices that make up the object**** NOT the point that represents the objects location.

WHAT YOU NEED TO DO:

1. Translate all vertices of Object X to the origin.
2. Rotate all vertices of Object X accordingly.
3. Translate all vertices of Object X back their Location.

See what I''m saying?

Share this post


Link to post
Share on other sites
The code below is a replacement DrawGLScene function for Nehe''s Lesson 2. It does exactly what we''re describing.

int DrawGLScene ()
{
static float s_fRot; // current rotation;


// clear buffers

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// reset modelview matrix

glLoadIdentity ();

// SETUP CAMERA POSITION AND ORIENTATION


// move the camera back 30 units so we can see everything

glTranslatef (0.0f, 0.0f, -30.0f);
// tilt the camera so that we''re not looking straight down on the scene

glRotatef (-60.0f, 1.0f, 0.0f, 0.0f);


// START DRAWING THE SCENE


// this push-pop block represents the scene''s position,

// the centre object being at 1,1,1 -- just because :)

glPushMatrix ();
glTranslatef (1.0f, 1.0f, 1.0f);

// draw the centre object

glBegin (GL_TRIANGLES);
glVertex3f ( 0.0f, 1.0f, 0.0f);
glVertex3f (-1.0f, -1.0f, 0.0f);
glVertex3f ( 1.0f, -1.0f, 0.0f);
glEnd ();


// DRAW THE ORBITTING OBJECT


// This push-pop block represents the orbiting object''s matrix

// relative to the centre object. The orbiting object is 10

// units away from the centre.

glPushMatrix ();
// rotate the matrix

glRotatef (s_fRot, 0.0f, 0.0f, 1.0f);

// The orbiting object has an orbit of 10 units, so

// translate 10 points along the x-axis.

glTranslatef (10.0f, 0.0f, 0.0f);

// draw another triangle -- this is the orbiting object

glBegin (GL_TRIANGLES);
glVertex3f ( 0.0f, 1.0f, 0.0f);
glVertex3f (-1.0f, -1.0f, 0.0f);
glVertex3f ( 1.0f, -1.0f, 0.0f);
glEnd ();
glPopMatrix ();
glPopMatrix ();

// increment the rotation

s_fRot += 0.01f;

// make sure the rotation never goes beyond 360°

if (s_fRot >= 360.0f) s_fRot -= 360.0f;

return TRUE;
}

Share this post


Link to post
Share on other sites
quote:
Original post by _the_phantom_
which is what i said in the 2nd post in this thread...

Indeed, but Fier still seems to be having some difficulty. As such, it''s probably best to demonstrate with working code that does exactly what we''re describing.

Share this post


Link to post
Share on other sites
First of all, I sincerely appreciate everyone''s help. Especially Ostsol, for showing me the acutal code. I understand what Ostsol and _the_phantom_ saying. I also believe you guys are doing the right way. Nevertheless, I need to figure out how to change the way object is rotating around a point in DIFFERENT DIRECTION, instead of just axis.

In the code Ostel post.
**************
// rotate the matrix
glRotatef (s_fRot, 0.0f, 0.0f, 1.0f); <----how to manipulate this one

// The orbiting object has an orbit of 10 units, so
// translate 10 points along the x-axis.
glTranslatef (10.0f, 0.0f, 0.0f);
******************************

This rotation would only made the object(say sphere) rotate around the centre point in centre point''s Z axis. That''s not enough, the requirement wants me to rotate around in point in any direction, not just distant point''s axis. Say change above code''s Translatef(10,0,0) to Translate(10,10,0), we''ll see the object is sphere is rotating around (1,10,1) on y=10 plane instead of the acutal centre point (1,1,1). This is not gonan work. Try those code and you''ll see what I am saying.

I think the real hard part is how to use that glRotate() function to define the direction the object can rotate. I don''t know how, I tried combine different axis, it doesn''t work out right. Really stuck now.



Nico, I have no idea what should I do that, please elaborated.


Anonymous Poster, my object sphere is define by the midpoint, which is the point that define the position. So what you saying has no effect on what I am doing. Thanks for help though.


Share this post


Link to post
Share on other sites
glRotatef takes four numbers, an angle, and then a normalized vector to rotate about.

So change it to glRotatef(angle, arbitraryVectorX, arbitraryVectorY, arbitraryVectorZ).

Remember to normalize it, though... so divide each component (x, y and z) of the vector by the length of the vector (square root(x^2+y^2+z^2) ).

Share this post


Link to post
Share on other sites
ok, guys, I finally figure out how to control the direction of the rotation path. Thank you all very very much. I''ve learn a lot. Especially about the matrix =)

Share this post


Link to post
Share on other sites