PDA

View Full Version : Bloxorz game . how to move the rectangular block?

srikanth chitturi
06-08-2012, 02:09 AM
hello ,
I am developing the bloxorz game :

see this link :
http://www.albinoblacksheep.com/games/bloxorz

I have some how managed to draw the floor and a block on it. by taking two functions like this :

drawFloor present in Floor class outside main :

void Floor::drawFloor()
{

glRotatef(45.5f,1.0f,1.0f,0.0f);

//Draw floor
for(int i = 1;i<6;i++)
{

for(int j = 1;j<6;j++)
{

//For hole to appear at 3,3
if(i == 3 && j==3)
{
glTranslatef(1.0f,0.0f,0.0f);
continue;
}

glBegin(GL_QUADS);

// Top Face
glColor3f(1.0f,0.0f,0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.500f,0.500f,-0.500f); //top left
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.500f,0.500f,0.500f); //bottom left
glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.500f,0.50f,0.50f); //bottom right
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.50f,0.50f,-0.50f);//top right

//Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.50f, 0.50f, -0.50f); //Top front
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.50f, 0.30f, -0.50f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.50f, 0.30f, 0.50f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.50f, 0.50f, 0.50f);

// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.50f,0.50f, 0.50f); //top right
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.50f,0.50f,0.50f); // top left
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.50f,0.30f, 0.50f); //bottom left
glTexCoord2f(0.0f, 1.0f); glVertex3f(0.50f,0.30f, 0.50f); //bottom right

// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.50f,0.30f,0.50f); // top right
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.50f,0.30f,0.50f); // top left
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.50f,0.30f,-0.50f); // bottom left
glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.50f,0.30f, -0.50f); //bottom right

// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.50f,0.50f,-0.50f); // top left
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.50f, 0.50f,-0.50f); //top right
glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.50f, 0.30f,-0.50f); //bottom right
glTexCoord2f(0.0f, 0.0f); glVertex3f( -0.50f,0.30f, -0.50f);//bottom left

// Right face

glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.50f, 0.50f, -0.50f); // Top right
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.50f, 0.50f, 0.50f); // top left
glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.50f, 0.30f, 0.50f); //bottom right
glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.50f, 0.30f, -0.50f); //bottom left

glEnd();

glTranslatef(1.0f,0.0f,0.0f);

}
glTranslatef(-5.0f,0.0f,-1.0f);

}

drawCube present in Cube class :

void Cube::drawCube()
{

glBegin(GL_QUADS);

// Top Face
glColor3f(0.0f,1.0f,0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.500f,0.500f,-0.500f); //top left
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.500f,0.500f,0.500f); //bottom left
glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.500f,0.50f,0.50f); //bottom right
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.50f,0.50f,-0.50f);//top right

//Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.50f, 0.50f, -0.50f); //Top right
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.50f,-0.5f, -0.50f); //top left
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.50f,-0.5f, 0.50f); //bottom left
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.50f, 0.50f, 0.50f); //bottom right

// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.50f,0.50f, 0.50f); //top right
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.50f,0.50f,0.50f); // top left
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.50f,-0.5f, 0.50f); //bottom left
glTexCoord2f(0.0f, 1.0f); glVertex3f(0.50f,-0.5f, 0.5f); //bottom right

// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.50f,-0.50f,0.50f); // top right
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.50f,-0.50f,0.50f); // top left
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.50f,-0.50f,-0.50f); // bottom left
glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.50f,-0.50f, -0.50f); //bottom right

// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.50f,0.50f,-0.50f); // top left
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.50f, 0.50f,-0.50f); //top right
glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.50f,-0.50f,-0.50f); //bottom right
glTexCoord2f(0.0f, 0.0f); glVertex3f( -0.50f,-0.50f, -0.50f);//bottom left

// Right face

glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.50f, 0.50f, -0.50f); // Top left
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.50f, 0.50f, 0.50f); // bottom left
glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.50f, -0.50f, 0.50f); //bottom right
glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.50f, -0.5f, -0.50f); //top right

glEnd();

}

I have placed the hole at 3,3 position look on drawing floor.
My main problem is moving the block left and right . For that I have taken a structure called

typedef struct
{
public:

float x_pos;
float y_pos;
float z_pos;
float radian;
}cube_param;

I created an object like cube_param cube_pos;
while left key is pressed :

case WM_KEYDOWN: // Is A Key Being Held Down?
{
keys[wParam] = TRUE; // If So, Mark It As TRUE

switch(wParam)
{
case VK_LEFT:
{

cube_pos.x_pos = - 1.0f ;
cube_pos.y_pos = -1.0f;
cube_pos.z_pos = 0.0f;
cube_pos.radian = -90.0f;

cube->drawCube(cube_pos);

}
}

return 0;

}

where drawCube(cube_pos); contains the same code as drawCube() but it has to move it.
Here does the structure cube_pos goes away with values to drawCube(cube_param cube_pos)

Look at it :

// For movement of cube
void Cube::drawCube(cube_param cube_pos)
{

float x_pos = cube_pos.x_pos;
float y_pos = cube_pos.y_pos;
float z_pos = cube_pos.z_pos;
float radian = cube_pos.radian;

//glTranslatef(1.0f,1.0f,0.0f);

glRotatef(radian,x_pos,0.0f,0.0f);

glBegin(GL_QUADS);

// Top Face
glColor3f(0.0f,1.0f,0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f + x_pos,0.5f + y_pos,-0.5f); //top left
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f + x_pos,0.5f + y_pos,0.5f); //bottom left
glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f + x_pos,0.5f + y_pos,0.5f); //bottom right
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f + x_pos,0.5f + y_pos,-0.5f);//top right

//Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.50f, 0.50f, -0.50f); //Top right
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.50f,-0.5f, -0.50f); //top left
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.50f,-0.5f, 0.50f); //bottom left
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.50f, 0.50f, 0.50f); //bottom right

// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.50f,0.50f, 0.50f); //top right
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.50f,0.50f,0.50f); // top left
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.50f,-0.5f, 0.50f); //bottom left
glTexCoord2f(0.0f, 1.0f); glVertex3f(0.50f,-0.5f, 0.5f); //bottom right

// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.50f,-0.50f,0.50f); // top right
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.50f,-0.50f,0.50f); // top left
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.50f,-0.50f,-0.50f); // bottom left
glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.50f,-0.50f, -0.50f); //bottom right

// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.50f,0.50f,-0.50f); // top left
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.50f, 0.50f,-0.50f); //top right
glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.50f,-0.50f,-0.50f); //bottom right
glTexCoord2f(0.0f, 0.0f); glVertex3f( -0.50f,-0.50f, -0.50f);//bottom left

// Right face

glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.50f, 0.50f, -0.50f); // Top left
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.50f, 0.50f, 0.50f); // bottom left
glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.50f, -0.50f, 0.50f); //bottom right
glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.50f, -0.5f, -0.50f); //top right

glEnd();

}

My idea is when left key is pressed i want to translate each and every vertex by adding or subtracting the x_pos,y_pos,z_pos present in cube_param structure
and then translate and rotate it. will it ultimately work for each and every move ?
If so how to do it.