• 13
• 18
• 19
• 27
• 10

# simple rotation

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

## Recommended Posts

I want a triangle to rotate around itself, not around (0,0,0), how do I do this?

##### Share on other sites
First you need to show some initiative.
Also, it's all relative, where is your camera, where is the triangle? At least give us the 3 vertexes of the triangle.

Joe
P.S. look into glRotatef(float angle, float x, float y, float z); // :)

##### Share on other sites
I dont know how to use glRotatef in a way that does not rotate the whole world, right now my triangle is happily rotating around 0,0,0.

If I get it right (and Im not that keen with the transformationstuff right now) I need to rotate the matrix while the triangle is drawn and then glPopMatrix(); again.

I think an uglier way is to apply the transformation myself in glBegin(GL_TRIANGLES).

the vertexvariables dont have static values because the triangle is moveable.

if it is important in some way: glOrtho (0, 1024, 0, 768, -1,1); and I think my camera is at 0 0 0, I did not move it around in anyway

##### Share on other sites
Quote:
 Original post by schattenmaennleinIf I get it right (and Im not that keen with the transformationstuff right now) I need to rotate the matrix while the triangle is drawn and then glPopMatrix(); again.
It sounds like you're on the right track. The glPushMatrix function pushes the current matrix stack down by one, duplicating the current matrix. The glPopMatrix function pops the current matrix stack, replacing the current matrix with the one below it on the stack. Any transformations that you make in between pushing and popping the matrix are effectively local rather that global transformations, and are reverted when you pop the matrix.

//This code draws a triangle in the center of the screen and rotates it 10 degrees:glPushMatrix(); //Push the matrix stack down a level.   glRotatef(10,0,0,1); //Rotate the matrix.   glTranslatef(512, 384, 0); //Translate the matrix if necessary.   glBegin(GL_TRIANGLES); //Draw the triangle at the new origin.      glVertex3f(0, 10, 0);      glVertex3f(10, -10, 0);      glVertex3f(-10, -10, 0);   glEnd();glPopMatrix(); //Pop the matrix stack up a level.

##### Share on other sites
thank you, my transformation is now local.
Still, the triangle is spinning around 0 0 0.
I was thinking I could set the midpoint of the triangle to be 0 0 0 and then apply the transformation but this does not seem to be correct since that also changes the position of the triangle on the screen(maybe I need to translate it 2 times?? feels ugly).

heroangle = atan2(mousey-heroy , mousex-herox)* 180 / PI;

glPushMatrix();
<<< glTranslatef(herox,heroy,0);
glRotatef(heroangle,0,0,1);
<<< glTranslatef(-herox,-heroy,0);
glBegin(GL_TRIANGLES);
glColor3f(15.0f, 0.0f, 0.0f);
glVertex2f(-30.0+herox, 0+heroy);
glColor3f(15.0f, 0.0f, 0.0f);
glVertex2f(30+herox, 0.0+heroy);
glColor3f(0.0f, 0.0f, 15.0f);
glVertex2f(0+herox, 40+heroy);
glEnd();

this way it starts at the correct point but it does not spin correctly, any ideas?

[Edited by - schattenmaennlein on September 27, 2007 8:02:30 PM]

##### Share on other sites
ah, I think I have to make the first translation so it is at 0 0 0 and the second to wherever it needs to go. *implement*

##### Share on other sites
I think now its just a problem of numbers if no one comes up with a neater solution. does arctan2 take the variables (x,y) or (y,x)? the compiler says (y,x), if thats true why?

##### Share on other sites
That's a good question, I don't know why, all languages I've used have had that also (y before x in ArcTan2 parameters). I think it's because Tan is opposite/adjacent, opposite being y. I always thought it was just because opposite came first in the equation, so they put y first in the parameters...

##### Share on other sites
kind of uncool of them, i bet many coders have spent a night bugsearching just to find out that x and y is the other way around in arctan2();. Anyways,

im having trouble here:

heroangle = 90; //atan2((mousey-heroy),(mousex-herox) )* 180 / PI;

glPushMatrix();

glTranslatef(-herox,-heroy,0); <- Here!
glRotatef(heroangle,0,0,-1);
//glTranslatef(herox,heroy,0);
glBegin(GL_TRIANGLES);
glColor3f(15.0f, 0.0f, 0.0f);
glVertex2f(-30.0+herox, 0+heroy);
glColor3f(15.0f, 0.0f, 0.0f);
glVertex2f(30+herox, 0.0+heroy);
glColor3f(0.0f, 0.0f, 15.0f);
glVertex2f(0+herox, 40+heroy);
glEnd();

glPopMatrix();

You can move herox and heroy with the keyboard. As I read my code now it should negate any movement of the triangle with glTranslatef(-herox,-heroy,0);. And well it does. But only if the angle is 0. Else you can still move the triangle and it does not move straight but diagonal.

Explanations?

##### Share on other sites
I think the whole 2transformations in one matrix-thing is faulted, and I suspect that

glTranslatef(herox,heroy,0);
glRotatef(heroangle,0,0,1);
glTranslatef(-herox,-heroy,0);

is the same as

glRotatef(heroangle,0,0,1);