Thread: Tetris block rotates wrong way?

  1. #1
    Registered User Kenoriga's Avatar
    Join Date
    Dec 2006
    Location
    Singapore
    Posts
    5

    Angry Tetris block rotates wrong way?

    This is how a normal block should rotate, right?
    Code:
     xx
    xx
    
    becomes
    
    x
    xx
     x
    but mine becomes
    Code:
     x
    xx
    x
    instead.

    This is the code
    Code:
    void display()
    {float xmov = 3.0f;
    float ymov = 3.0f;
    float anrot = 0.0f;
    float yrot = 0.0f;
    float xrot = 0.0f;
    
    
    	glClear(GL_COLOR_BUFFER_BIT);
    	
        
        glPushMatrix();
    	    glScalef(0.2f, 0.2f, 0.0f);
            glTranslatef(xmov, ymov, 0.0f);
            glRotatef(anrot, xrot, yrot, 1.0f);
    		glColor3f(0.5f, 0.5f, 1.0f);
    		
    		glBegin(GL_QUADS);          
                glVertex2f(-0.1f, -0.1f);
                glVertex2f(-0.1f, 0.0f);
                glVertex2f(0.0f, 0.0f);
                glVertex2f(0.0f, -0.1f);
            glEnd();
            
            glBegin(GL_QUADS);
                glVertex2f(-0.1f, 0.0f);
                glVertex2f(-0.1f, 0.1f);
                glVertex2f(0.0f, 0.1f);
                glVertex2f(0.0f, 0.0f);
            glEnd();
            
            glBegin(GL_QUADS);
                glVertex2f(0.0f, 0.0f);
                glVertex2f(0.0f, 0.2f);
                glVertex2f(0.1f, 0.2f);
                glVertex2f(0.1f, 0.0f);
            glEnd();
            
            
        glPopMatrix();
            
    	glFlush();
    
    }
    
    void idle()
    {   if (xmov < 0.0f)
        {xmov = 5.0f;}
        if (ymov < 0.0f)
        {ymov = 5.0f;}
    }
    
    LRESULT OpenGL::WndProc(HWND hwnd, UINT msg, 
    			WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    
    	case WM_ACTIVATE :
    		{
    			active = HIWORD(wParam) ? false : true;
    
    			return 0;
    		}
    
    	case WM_CLOSE :
    		{
    			PostQuitMessage(0);
    			return 0;
    		}
    
    	case WM_SIZE :
    		{
    			width = LOWORD(lParam);
    			height = HIWORD(lParam);
    			
    			if (resize)
    				resize(LOWORD(lParam), HIWORD(lParam));
    
    			return 0;
    		}
    		
        case WM_KEYDOWN :
    		if (wParam == VK_RIGHT)
               {xmov += 0.1f;}
            if (wParam == VK_LEFT)
               {xmov -= 0.1f;}
            if (wParam == VK_UP)
               {ymov += 0.1f;}
            if (wParam == VK_DOWN)
               {ymov -= 0.1f;}
            if (wParam == VK_SPACE)
               {anrot += 180.0f;
                xrot += 45.0f;
                yrot += 45.0f;}
            }
    
    	return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    I don't think you need anymore of the code. If you need anymore, please tell me.

    I know this seems stupid to belong in Game Programming... >.<

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Perhaps the code that rotates it should just be reversed. it would make sense, sometimes things that you don't expect happen, maybe in your code you have got your directions mixed up.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    ok try change glRotatef(anrot, xrot, yrot, 1.0f); to glRotatef(anrot, 0.0, 0.0, 1.0f); and change
    Code:
    if (wParam == VK_SPACE)
               {anrot += 180.0f;
                xrot += 45.0f;
                yrot += 45.0f;}
            }
    to

    Code:
    if (wParam == VK_SPACE)
               {
                    anrot += 90.0f;
               }
            }
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Tetris uses 90 degree rotations as Shakti has pointed out. And unless this is 3D tetris, you are going about this the wrong way.

    It appears you are doing a 2D tetris. If so you should be using a different method to present the graphics.

    1. Tetris is a grid.
    2. Numbers on the grid represent colors or empty space.
    3. Blocks are composed of numbers representing colors and are built via offset tables.
    4. Your renderer simply scans an array from TOP to BOTTOM and draws blocks.
    5. Your board is setup so that pieces occupy the correct blocks to make them 'appear' as pieces
    6. TOP changes as the board changes.
    Last edited by VirtualAce; 01-15-2007 at 02:56 AM.

  5. #5
    Registered User Kenoriga's Avatar
    Join Date
    Dec 2006
    Location
    Singapore
    Posts
    5
    Whoop. Thanks for the help everyone! Shakti's solution worked!

    How come it doesn't work if I add 0.1f to xrot and yrot?

    Thanks again woohoo. I got stuck like almost one month... I feel so stupid now.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    because you rotate around the wrong axis. xrot, yrot and zrot determines the vector around which you rotate the object. the z-axis goes in and out of the screen and is the only one you want to rotate around if you do 2D games. I suggest reading up on the function glRotatef to get a better understanding of how it works.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error on in algorithim
    By v3dant in forum C Programming
    Replies: 1
    Last Post: 11-08-2004, 11:03 AM
  2. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  4. Creating dimensions of a 'block' from a file
    By funkydude9 in forum C++ Programming
    Replies: 8
    Last Post: 07-03-2003, 02:43 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM