basic collision detection

Started by
52 comments, last by cwl157 15 years, 10 months ago
yea the y value is wrong. I could add 2 to it but that only works for the bottom row but the y value when a collision is detected is 2 rows short. But that doesn't work when its not the bottom row, i have the 2nd from bottom row and 3rd from bottom row should have been detected as full and were not. so do i need to check not just 4 rows up from the down collision row but some of the rows down as well...
Advertisement
So i have 2 things i need to figure out before i start working on graphics.

1. arranging the rows.
a. because of y = y-1 in arrangeRows it goes through the next row up instead of down but it starts from the top most row of the shape, so if a straight line of 4 completes the shape then it will start counting at 16-4 which is 12 and check 12 and then 11 and then 10 when it should either start at 12 and check 12, 13, 14, 15 or start at 15 and check 15, 14, 13, 12. I can not just make it y = y+1 because that messes up the collision detection and pieces go past the bottom. Here are the functions that are involved in detecting full rows and rearranging them:

// unfill 1 row
void unFillOneRow(int row)
{
for (int col = 1; col < 16; col++)
gameBoard[row][col] = 0;
} // end unFillOneRow

void moveRowsDown(int n)
{
int row;
for (row = n; row > 1; row--)
{
for (int col = 1; col < 16; col++)
{
//cout << "the row is : " << row << endl;
//cout << "the col is : " << col << endl;
gameBoard[row][col] = gameBoard[row-1][col];
} // end for
} // end for
cout << "the row is " << row << endl;
// this sets the first row to 0's
unFillOneRow(1);
} // end moveRowsDown

void arrangeRows(int y)
{
for (int i = 0; i < 4; i++)
{
if (isRowFull(y))
{
moveRowsDown(y);
lines++;
cout << "line count is " << lines << endl;
cout << "level is " << level << endl;
} // end if
else
y = y -1;
} // end for
} // end arrangeRows

// return true if the row is full and needs to be deleted, false otherwise
bool isRowFull(int row)
{
int rowFull = 0;
for (int col = 0; col < 17; col++)
{
if (gameBoard[row][col] != 0)
{
rowFull++;
cout << "Row full is " << rowFull << endl;
cout << "row is : " << row << endl;
} // end if
} // end for
if (rowFull == 17)
{
cout << "row is full\n";
return true;
} // end if
else
{
cout << "row is not full\n";
return false;
} // end else
} // end isRowFull

2. the 2nd issue is detecting game over which i have no clue how to do this i cant just check if a whole column is non 0 because a column can reach the top with holes in the spaces. I also can not see if there is anything in the top row of 8's because the pieces just stop at 1 not 0 and write over themselves when they hit the top. If you could please help me with these last 2 issues and then i think i am ready for graphics and all of that.
Quote:Original post by cwl157
yea the y value is wrong. I could add 2 to it but that only works for the bottom row but the y value when a collision is detected is 2 rows short.

I'm not quite following you, but what you could do as a temporary workaround is to check the complete board instead of just four lines depending on the last piece position.

Quote:Original post by cwl157
the 2nd issue is detecting game over which i have no clue how to do this

When you spawn the next piece and it immediately collides with the board, the game is over.
void nextPiece(){    shape = rand() % 7;    rotation = 0;    x = 5; //or wherever the middle of your board is    y = 0;    if (checkCollision())    {        // game over    }}
Quote:
I'm not quite following you, but what you could do as a temporary workaround is to check the complete board instead of just four lines depending on the last piece position.


i don't know if even this will work because it appears that it starts checking from the first row of when the piece settles, not the last. So for example if i put down a

line 12: 1
line 13: 1
line 14: 1
line 15: 1

and the bottom 1 is at the last row of the board and it gets filled then it should check line 15, 14, 13, 12 because that would go from the bottom of the piece to the top but instead it starts at 12 and counts up from that so it checks lines 12, 11, 10, 9. So if the filled lines are below 12 then they never get checked.

This topic is closed to new replies.

Advertisement