Jump to content
  • Advertisement

Archived

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

redneon

Rotate around center of four objects

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

To teach myself OpenGL I''m making a Tetris game. In my game I have a RNG which determins the shape and then depending which shape was chosen it draws four squares on the screen to create the shape. The problem I''m having is that I can''t get my program to rotate the objects around the center of the four objects. It always rotates around the bottom left hand corner. This is my renderShape() method... void Shape::renderShape(void) { float center = 0.0f; switch (shapeType) { case 0: //Square glColor3f(0.8f, 0.0f, 0.0f); renderBlock(center + 0.06f, center + 0.06f, 0.058f); renderBlock(center, center + 0.06f, 0.058f); renderBlock(center + 0.06f, center, 0.058f); break; case 1: //T glColor3f(0.0f, 0.8f, 0.0f); renderBlock(center + 0.12f, center, 0.058f); renderBlock(center + 0.06f, center + 0.06f, 0.058f); renderBlock(center + 0.06f, center, 0.058f); break; case 2: //Long glColor3f(0.0f, 0.0f, 0.8f); renderBlock(center, center + 0.18f, 0.058f); renderBlock(center, center + 0.12f, 0.058f); renderBlock(center, center + 0.06f, 0.058f); break; case 3: //Right L glColor3f(0.8f, 0.8f, 0.0f); renderBlock(center + 0.06f, center + 0.12f, 0.058f); renderBlock(center, center + 0.12f, 0.058f); renderBlock(center, center + 0.06f, 0.058f); break; case 4: //Left L glColor3f(0.8f, 0.0f, 0.8f); renderBlock(center - 0.06f, center + 0.12f, 0.058f); renderBlock(center, center + 0.12f, 0.058f); renderBlock(center, center + 0.06f, 0.058f); break; } renderBlock(center, center, 0.058f); float centerX = posX - (width / 2); float centerY = posY - (length / 2); glLoadIdentity(); glTranslatef(centerX, centerY, 0.0f); glRotatef(rotateAmount, 0.0f, 0.0f, 1.0f); } Any ideas? Darrell

Share this post


Link to post
Share on other sites
Advertisement
I see that you first render the object and then setup the matrix.This is of course wrong:The matrices are used to transform the coords of the objects rendered,so you must always first setup the matrix and then render your objects.Get the redbook and learn well at least the fundamentals of GL,you can''t just start making games without understanding them.
Anyway,just setup the matrix first,and then render your blocks.

Share this post


Link to post
Share on other sites
Originally it was before the render. I changed it but I can''t remember why. I''ve put it back though but am still having the same problem. I have the red book and the blue book. I havn''t just jumped into programming I''ve been DirectX programming for a while but I wanted to switch to OpenGL.

Darrell

Share this post


Link to post
Share on other sites
Then there''s something wrong with the way you center your obejcts.For example,see that piece of code:

case 2: //Long
glColor3f(0.0f, 0.0f, 0.8f);
renderBlock(center, center + 0.18f, 0.058f);
renderBlock(center, center + 0.12f, 0.058f);
renderBlock(center, center + 0.06f, 0.058f);
break;

as always,transformations will be applied in the reverse order:
first the object will be rotated around the z-axis and then it will move to (CenterX,CenterY).The problem is,the center of that object,is not (0,0),as it should(we''re talking object coordinates here).

Share this post


Link to post
Share on other sites
So do I need to translate my object so that centerX and centerY are 0,0 before I perform the rotation?

Darrell

Share this post


Link to post
Share on other sites
Yes.Assuming (CenterX,CenterY) is in fact the center of the object in world coordinates,you have to:

1)Translate so the object''s center is at (0,0).
2)Rotate
3)Translate back to (CenterX,CenterY).

These must be done in reverse,so:

glLoadIdentity();
glTranslatef(CenterX,CenterY);
glRotatef(...)
glTranslatef(-CenterX,-CenterY);

Share this post


Link to post
Share on other sites
Ok cheers, I''ll have a go =o) Yes centerX and centerY are the center co-ords of the object. The center variable is the center of the screen (0,0). Just out of interest, would I need to run glLoadIdentity() before the second translate? I''m still getting my head around resetting the matrices. It''s not something I really had to do in DirectX.

Darrell

Share this post


Link to post
Share on other sites
Not really. What''s happening now is that it''s rotating around 0,0 no matter what distance away from 0,0 it is. This is my renderShape() method in case I''ve missed anything.


void Shape::renderShape(void)
{
float center = 0.0f;
//float centerX = posX - (width / 2);
//float centerY = posY - (length / 2);

glLoadIdentity();
glTranslatef(0.0f - (width / 2), 0.0f - (length / 2), 0.0f);
glRotatef(rotateAmount, 0.0f, 0.0f, 1.0f);
glTranslatef(posX, posY, 0.0f);

switch (shapeType)
{
case 0: //Square
glColor3f(0.8f, 0.0f, 0.0f);
renderBlock(center + 0.06f, center + 0.06f, 0.058f);
renderBlock(center, center + 0.06f, 0.058f);
renderBlock(center + 0.06f, center, 0.058f);
break;
case 1: //T
glColor3f(0.0f, 0.8f, 0.0f);
renderBlock(center + 0.12f, center, 0.058f);
renderBlock(center + 0.06f, center + 0.06f, 0.058f);
renderBlock(center + 0.06f, center, 0.058f);
break;
case 2: //Long
glColor3f(0.0f, 0.0f, 0.8f);
renderBlock(center, center + 0.18f, 0.058f);
renderBlock(center, center + 0.12f, 0.058f);
renderBlock(center, center + 0.06f, 0.058f);
break;
case 3: //Right L
glColor3f(0.8f, 0.8f, 0.0f);
renderBlock(center + 0.06f, center + 0.12f, 0.058f);
renderBlock(center, center + 0.12f, 0.058f);
renderBlock(center, center + 0.06f, 0.058f);
break;
case 4: //Left L
glColor3f(0.8f, 0.0f, 0.8f);
renderBlock(center - 0.06f, center + 0.12f, 0.058f);
renderBlock(center, center + 0.12f, 0.058f);
renderBlock(center, center + 0.06f, 0.058f);
break;
}

renderBlock(center, center, 0.058f);
}


Any ideas?

Darrell

Share this post


Link to post
Share on other sites
Sorry, I put the translates the wrong way around. Duh! Now it''s working. Cheers mate.

Darrell

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!