Rotating an object

Started by
3 comments, last by Klownterfit 18 years, 1 month ago
How would you go about rotating an object 20 degrees, without the object moving any. I mean, the 3 vertices of a triangle would all rotate around the center 20 degrees. This is the code right now. glRotatef(20.0f,0.0f,0.0f,1.0f) It rotates the triange correctly, but also moves it. http://imagecloset.com/8/03111010-movement.JPG Top right is where it starts, and then the bottom left is where it ends up after glRotate The code looks like this

void DrawTri(float x,float y,float r,float g,float b,float a,float size,float angle)
{
	glPushMatrix();
	glLoadIdentity();
	glDisable(GL_TEXTURE_2D);
	glRotatef(20.0f,0.0f,0.0f,1.0f); // Z rotates its like a 'fan' ?
	glBegin(GL_TRIANGLES);
	glColor4f(r, g, b, a);
	glVertex3f((x+size/2),(y - (size*2)), 0.0f);
	glVertex3f(x,y, 0.0f);
	glVertex3f(x+size,y, 0.0f);
	glEnd();
	glEnable(GL_TEXTURE_2D);
	glPopMatrix();
}
All of the variables work correctly, when there is no glRotate it's fine. Any suggestions?
Advertisement
Take a look at this thread
http://www.gamedev.net/community/forums/topic.asp?topic_id=377897
and/or at this thread
http://www.gamedev.net/community/forums/topic.asp?topic_id=380493
which both deal with the same problem (although in another context).

Now think about the correct origin of your rotation, so that the tri isn't translated (dozens of possibilities exist; I have a link somewhere, but I don't find it ...). I suggest to use
O := 0.3 * ( v1 + v2 + v3 )
as the center for a first attempt, where vi denote the 3 vertices of your tri.
I really don't understand what you're saying.

From those topics, it looks like I need to translate the object to (0,0), then rotate it, then translate it back?

from the function above, I'm using the command.

DrawTri(500,500,1.0f,0.0f,0.0f,1.0f,10.0f,30.0f);

So it is drawing the triangle at 500,500.

The game has downwards resolution, (0,0) is at the top left corner, and moving right or down is both positive. I'm not sure if that's having any effect.

But, I have tried this.

glTranslatef(-500.0f,-500.0f,0.0f)
glRotatef(20.0f,0.0f,0.0f,1.0f)
glTranslate(500.0f,500.0f,0.0f)

And it no longer draws it when I use that.
I didn't read the whole thread, but try changing this:
glTranslatef(-500.0f,-500.0f,0.0f)glRotatef(20.0f,0.0f,0.0f,1.0f)glTranslate(500.0f,500.0f,0.0f)
To this:
glTranslatef(500.0f,500.0f,0.0f)glRotatef(20.0f,0.0f,0.0f,1.0f)glTranslate(-500.0f,-500.0f,0.0f)
Or better yet, store your geometry in local coordinates and centered at the origin. This is how it's usually done, and will avoid the translate-rotate-translate problem.
Ok, we got the issue resolved by translating to the correct vector and drawing the vertex at the translated position instead of drawing the vertex without translating.

Thanks for your help.

This topic is closed to new replies.

Advertisement