Jump to content
  • Advertisement

Archived

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

adam_

Rotating in 2D

This topic is 5247 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

Hi, I am writing a simple tile engine in opengl, I can render tiles all fine but now I want to be able to rotate tiles and charachters. I am working in ortho mode and I use this code to set it up: glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(0, 640, 480, 0, -1, 1); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); and when I want to rotate something I do this glRotatef(ang, 0, 0, 1); // where ang is degrees to rotate The problem is that the tile wont rotate around its center point but in a big circle around the (0,0) point wich is the top left corner of the screen. How can I change it so that the tile rotates around its center point? Will it be better to dump ortho mode and work in 3d, using gluLookAt(), wich I have seen some people doing. If you need any more info on how I do things just ask. Thanks..

Share this post


Link to post
Share on other sites
Advertisement
Hi!

You have to rotate the tile first, then move (translate) it to its position on the map.

Hope that helps...

cya,
Drag0n

-----------------------------
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning..." -- Rich Cook

My future web presence: Dave''s Programming Resources

Share this post


Link to post
Share on other sites
You gota give the matrix changes in reverse though so translate first, then rotate, which will really rotate, then translate.

Since you are only rotating this can''t be the problem, show us the code where you draw what ever it s that you draw. Maybe its not centered around the origin.

Share this post


Link to post
Share on other sites
Okey here is the code that I use for drawing the tiles.


// Loop for each row and column
for(Row = 0; Row < NumRows; Row++) {
for(Column = 0; Column < NumColumns; Column++) {
GLfloat x = XPos*(-1)+(Column * WIDTH);
GLfloat y = YPos*(-1)+(Row * HEIGHT);

glPushMatrix();
glTranslatef(x, y, 0.0f);
glRotatef(rot, 0, 0, 1);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(x-64, y-64);
glTexCoord2f(1.0f, 0.0f); glVertex2f(x-64+WIDTH, y-64);
glTexCoord2f(1.0f, 1.0f); glVertex2f(x-64+WIDTH, y-64+HEIGHT);
glTexCoord2f(0.0f, 1.0f); glVertex2f(x-64, y-64+HEIGHT);
glEnd();
glPopMatrix();
}
}


substracting 64 from the x and y coords should make the tile centered on the orgin. (Tiles are 128*128 right now)

And of course I wont rotate all the tiles all the time in the game I am just testing stuff right now =)

Thanks..

Share this post


Link to post
Share on other sites
okey I found my mistake ;P
change

glTexCoord2f(0.0f, 0.0f); glVertex2f(x-64, y-64);
glTexCoord2f(1.0f, 0.0f); glVertex2f(x-64+WIDTH, y-64);
glTexCoord2f(1.0f, 1.0f); glVertex2f(x-64+WIDTH, y-64+HEIGHT);
glTexCoord2f(0.0f, 1.0f); glVertex2f(x-64, y-64+HEIGHT);


to

glTexCoord2f(0.0f, 0.0f); glVertex2f(-64, -64);
glTexCoord2f(1.0f, 0.0f); glVertex2f(-64+WIDTH, -64);
glTexCoord2f(1.0f, 1.0f); glVertex2f(-64+WIDTH, -64+HEIGHT);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-64, -64+HEIGHT);


thanks for the fast replies

[edited by - adam_ on January 11, 2004 4:47:50 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!