# opengl -problem at rotating invidual object

## Recommended Posts

##### Share on other sites
I don't know if I entirely understand what you mean...Do you want things to orbit around a certain point? If so, just translate to the point that the object is orbiting about, rotate about the vector that is normal to the plane of orbit, translate to the radial distance of the orbit, and draw your object.

##### Share on other sites
Quote:
 Original post by CrimsonSunI don't know if I entirely understand what you mean...Do you want things to orbit around a certain point? If so, just translate to the point that the object is orbiting about, rotate about the vector that is normal to the plane of orbit, translate to the radial distance of the orbit, and draw your object.

translating before rotating to this point is a disaster because it screws up even more. so your method didn't work

##### Share on other sites
I think that your problem might be two-fold.

First, try translating first to the negation of the centre of the current quad, then rotating, and then translating back to the position of the quad.

Second, encase the section within the loop "while (counter != quad.size())" in a pushMatrix()/popPatrix() pair to isolate the changes that you make to one quad from other geometry - such as the next quad. Currently I would expect your rotations to be concatenated, so that the rotation of a given quad is its own rotation plus the rotation of every quad before it.

Thus I would suggest:

while (counter != quad.size()){glPushMatrix() //Save the current state of the transformation matrix.glBindTexture(GL_TEXTURE_2D, quad[counter].texture);//Move the quad back to its position after the rotation has been performed.// I have used the coordinates that you gave for the centre of your quads.glTranslatef(((quad[counter].x1+quad[counter].x2)/2),             ((quad[counter].y1+quad[counter].y2)/2),             quad[counter].z);glRotatef(quad[counter].thisangle, 0, 0, 1);//Move the quad to the origin.// Note the negation of each element.glTranslatef(-(quad[counter].x1+quad[counter].x2)/2),             -((quad[counter].y1+quad[counter].y2)/2),             -quad[counter].z);glBegin(GL_QUADS);glTexCoord2f(0,0);glVertex3f(quad[counter].x1, quad[counter].y2, quad[counter].z);glTexCoord2f(1,0);glVertex3f(quad[counter].x2, quad[counter].y2, quad[counter].z);glTexCoord2f(1,1);glVertex3f( quad[counter].x2, quad[counter].y1, quad[counter].z);glTexCoord2f(0,1);glVertex3f(quad[counter].x1, quad[counter].y1, quad[counter].z);glEnd();glPopMatrix(); //Load the saved transformation matrix so that the changes that               // we made for this quad don't affect anything else.++counter;}

##### Share on other sites
So as I understand, your quads are rotating around the world origin and you want them to revolve around their individual z axes?

If this is the case, then let me revise... If you're displaying multiple objects, you need to preserve your current world state while also altering all of your quads individually. You can push and pop matrices onto the stack to accomplish this...

while(counter != quad.size() ){  glBindTexture(GL_TEXTURE_2D, quad[counter].texture);  glPushMatrix();  glTranslatef( (quad[counter].x1 + quad[counter.x2) / 2.0,                (quad[counter].y1 + quad[counter.y2) / 2.0,                0);  glRotatef(quad[counter].thisangle, 0, 0, 1);  glBegin(GL_QUADS);    glTexCoord2f(0,0);    glVertex3f(quad[counter].x1, quad[counter].y2, quad[counter].z);    glTexCoord2f(1,0);    glVertex3f(quad[counter].x2, quad[counter].y2, quad[counter].z);    glTexCoord2f(1,1);    glVertex3f( quad[counter].x2, quad[counter].y1, quad[counter].z);    glTexCoord2f(0,1);    glVertex3f(quad[counter].x1, quad[counter].y1, quad[counter].z);  glEnd();  glPopMatrix();  ++counter;}

EDIT: Let me actually explain what this does. You have a nice neat stack of matrices in your OpenGL implementation (even if you don't know it). When you glPushMatrix, you're essentially copying the current matrix into another matrix and putting this copy on top of the stack. Now any points you render are going to be multiplied by this new matrix. Any glRotates or glTranslates you apply here are going to affect the topmost stack only. When you glPopMatrix, you are taking that copy and throwing it away, leaving the original matrix on top of the stack unaltered.

##### Share on other sites
thanks guys, to both of you. i got it solved

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628333
• Total Posts
2982139

• 9
• 24
• 9
• 9
• 13