glRotate around arbitrary point?

This topic is 5039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Fiddling about with my little Asteroids clone. I have the vertices for each asteroid type (LG, MD, SM) stored in display lists. I am using straight up ortho mode, so 2D only. I'm not mucking about with the Z axis. I want the asteroids to tumble as they move, and have defined a point in each shape that's the "center" of them to rotate about, but I'm not sure I understand how to get them to rotate around that point. If I rotate with a plain: glRotatef(rotAngle, 0.0f, 0.0f, 1.0); It rotates around (0,0) in local coords, which is more like the "corner" of the asteroid. Also according to the docs, specifying different params for x, y, and z rotates around the vector (x, y, z). I guess I'm not sure how to compute that vector. If (rx, ry) is a point in the "center" of the asteroid, and I want to rotate around the Z-axis centered on that point, how do I compute the vector for that? I tried glRotatef(rotAngle, rx, ry, 1.0f); That gives some bizarre results. It seems the vector is computed from the origin in local coords, but I guess I don't understand how to do that. So uh...help? Edit: clarity.

Share on other sites
glRotatef(rotAngle,x,y,z);

x,y,z is the vector of rotation. So (0,0,1) is the rotation around the Z axis.

To rotate around anypoint..
glPushMatrix();   glRotatef(rotAngle,0.0f, 0.0f, 1.0f);   glTranslate2f(rx, ry);   glDrawFunction();glPopMatrix();

I believe that's how it'll work, but I haven't tested it and I just woke up. XD

I rotate the object when the object's center is 0.0f, then I move the object to it's approprate location and draw it. glPush & glPop keeps my rotation and translation from changing any other objects.

Share on other sites
Quote:
 Original post by BinomineglRotatef(rotAngle,x,y,z);I rotate the object when the object's center is 0.0f, then I move the object to it's approprate location and draw it. glPush & glPop keeps my rotation and translation from changing any other objects.

The object's center is never 0,0, tho. Using your code, it rotates around the screen origin. Here's the code for the display list:

// large asteroidaLarge = glGenLists(1);glNewList(aLarge, GL_COMPILE);	glBegin(GL_POINTS);		glVertex2i(48, 47);	glEnd();	glBegin(GL_LINE_LOOP);		glVertex2i(12, 13);		glVertex2i(58, 4);		glVertex2i(95, 39);		glVertex2i(68, 90);		glVertex2i(35, 90);		glVertex2i(6, 57);	glEnd();glEndList();

The lone GL_POINTS vertex is the "center" or (rx, ry). I am drawing it for debugging purposes. And as I said in the OP, if I translate to the asteroids (x, y) position first, and then rotate, it rotates around (0,0) in local coords which is (x, y).

Share on other sites
glTranslatef(centreX, centreY, 0);glRotatef(angle, 0, 0, 1);glTranslatef(-centerX, -centerY, 0);// render

Enigma

Share on other sites
Quote:
 Original post by EnigmaglTranslatef(centreX, centreY, 0);glRotatef(angle, 0, 0, 1);glTranslatef(-centerX, -centerY, 0);// renderEnigma

This doesn't do it either. It's still rotating around (0,0) in local coords. I tried just translating all my vertex values so that (0,0) IS in the center, and that works. Doesn't really matter that some of the coords are negative.

Thanks for the input, gentlemen.

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 12
• 10
• Forum Statistics

• Total Topics
632578
• Total Posts
3007160

×