Rotate around center of four objects
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
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.
Anyway,just setup the matrix first,and then render your blocks.
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
Darrell
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).
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).
So do I need to translate my object so that centerX and centerY are 0,0 before I perform the rotation?
Darrell
Darrell
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);
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);
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
Darrell
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.
Any ideas?
Darrell
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement