I'm trying to make a simple 3d tetris game, but I'm having trouble rotating my blocks. The blocks are made up of 4 cubes (drawn with glutSolidCube()). I can't figure out how I can rotate them and still have the axes point in the right direction for the cubes' movement.

This is the simplest form of what I've tried that clearly illustrates the problem when I run it

for(int i=0; i<4; i++){
    glRotatef(90.0, 1,0,0);
    glTranslatef(cubes[i]->xPos, cubes[i]->yPos, cubes[i]->zPos);

And this not only rotates the block, but rotates the whole system in the sense that now, the blocks are rotated in the right direction, but move in from the side rather than the top as if the axes have been rotated as well. I've tried first translating to the point where they start falling, rotating and translating back, but they still just go sideways. (not included in the sample because the start location isn't the main problem here)

I sort of understand why this happens, but I don't know how to fix it.


1 Answer 1


What you aren't showing is how the cubes' falling translation is applied. From your description of the effects, it is occurring after the rotation; so you are either translating in ->draw() or updating the yPos etc. coordinates. The "graphics" fix to your problem is to apply the falling motion before your glRotate.

However, this would be setting yourself up for bigger problems. You are eventually going to want to collide your cubes against the rest of the objects in the playfield so as to know when to stop them and remove rows etc. Just rotating in the rendering isn't sufficient to handle that.

Therefore, you are going to want to compute the rotation yourself, not delegate it to OpenGL. My recommendation would be to actually change the configuration of the cubes — that is, change xPos, yPos, and zPos according to the rotation. Since you are only doing 90° rotations, this is especially simple. This means your collision tests do not have to think about rotations.

(If this does not seem to be an appropriate answer, then please update your question with details of how you are animating the falling.)


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .