Jump to content
  • Advertisement
Sign in to follow this  
cwl157

basic collision detection

This topic is 3710 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am making a tetris game. I am using a 2d int array to store the shapes, a different number represents a different piece. I have functions that create each shape and erase each shape. Now i want to work on moving the shapes and collision detection. All open spaces on the board are marked by 0 so if a space is marked 0 then it is available to be moved to. So the square shape looks like this: 22 22 Lets say i try to move that down 1 so to do that i have to check if the one below it is a 0 but it doesn't check 1 below the whole shape it just checks 1 below the first row and find a 2 and says there is a collision. How would i get it to check below the whole shape. I thought about passing into the function something that says ignore this so it would ignore the 2 but then if your placing a square on top of another square it wont work, it has to detect the end of that piece only. Is the only way to do this to have a different collision detection function for each piece since there is not a constant beginning and ending spot for the pieces?

Share this post


Link to post
Share on other sites
Advertisement
Make another array which contains all the pieces that have settled down and check that array for collision with the piece.

Share this post


Link to post
Share on other sites
o, thats a good idea. So then when there is a collision moving a piece down the array then i would update the array that has all the pieces that are settled down. So then to check for collision right i would have a function like this:

// return 1 if you can move right 0 otherwise
int canMoveRight(int xPos, int yPos)
{
if (gameBoardSet[yPos][xPos+1] == 0)
{
return TRUE;
} // end if
else
return FALSE;
} // end canMoveRight

this returns true if you can move right false otherwise, you pass it the current x and y positions and then it checks 1 over i also need to add to that to check the bounds to make sure that it is not going off the right side of the playing field. Then to update the version of the gameBoard that has the settled pieces would i need another function for each piece that takes the final x position and y position of the piece and draws it on there? Also, i am going to have the same problem with the bounds detection. I think it has something to do with checking the first x and y position on the piece and not the 2nd. Like on the square that is this:
22
22
it checks the position of the 2 where the piece starts to move it right not where it ends. So i would need to check where it ends but since each tetris piece is different then i think i need to have a function for each shape right? Unless i made the game board have 1 more row and column and just made them all 1's or something and never made it be displayed so then running into the last row and last column would be the same logic as any other collision but you would not see them so its like the end of the playing field.

[Edited by - cwl157 on June 21, 2008 11:20:38 AM]

Share this post


Link to post
Share on other sites
Yeah, make a function that is called right before a new piece is added to the board that updates the settled board array.

Share this post


Link to post
Share on other sites
And would i need a separate function for each piece to update this or could i just copy the gameboard array into it to update it? I am playing around with it though and i still have some trouble with the square being 2 columns long and it looking at the first column where it starts and not the 2nd column. Then with the bounds if i add an extra column to the left and right ends and 1 at the bottom that is all 1's or something this logic could be the same. And i think part of my problem is now the gameBoard does not go all the way. It doesn't go from 0 to 17 it goes from 1-16 left to right and from 0 - 16 down because of the ends.

Share this post


Link to post
Share on other sites
You don't have to have a separate function for each piece. When I made Tetris, I did the collision detection a bit differently. I checked for collision after the piece has moved but before the array was drawn onto the screen. If the piece overlapped with the settled board array, I moved the piece back. That way, I would need only one function to do right, left, and bottom collisions.

Share this post


Link to post
Share on other sites
i dont know how that method would work with mine because i have a separate draw and erase function for each piece. So i was just going to check if the piece could be moved there and then move it there. The collision detection works for one piece now it will take it to the bottom and set it but i am having problems updated the array with the set piece. It's basically just setting the 2 arrays equal and i made a function that does that but it does not work right i don't think i can do it the way i think i can. How do i edit this function to set the gameBoardSet array equal to the gameBoard array.

// this sets the gameBoard = to the gameBoardSet
void setBoards()
{
for (int row = 0; row < 17; row++)
{
for (int col = 0; col < 17; col++)
{
gameBoardSet[row][col] = gameBoard[row][col];
} // end for
} // end for
} // end setBoards

if i run this function after placing a piece on the board all i get are 0's like nothing was set i dont know whats wrong

Share this post


Link to post
Share on other sites
ok i see what my error is. Its not how i set the boards equal but how i create the pieces. To create the piece i check for a collision if there is no collision i erase the piece that was there and re-draw it one line down. However, the erase comes after the draw so it completely erases the piece from the game board.

This is the code that sends the piece from top to bottom. It works but it does not keep the old piece there.

while (canMoveDown(xPos, yPos))
{
createSquare(xPos, yPos);
displayGameBoard();
setBoards();
cout << endl << endl;
eraseSquare(xPos, yPos);
yPos++;
} // end while

xPos = MIDDLE_X;
yPos = 0;
while (canMoveDown(xPos, yPos))
{
createLongShankVert(xPos, yPos);
displayGameBoard();
cout << endl << endl;
eraseLongShankVert(xPos, yPos);
yPos++;
} // end while

Share this post


Link to post
Share on other sites
forget all this stuff i just posted. A generic move down, move left, move right function can not work because to detect a collision moving down you have to use the bottom most block of the tetris piece to check and that varies by piece. To go right you have to use the right most block of that piece and that varies by piece. To move left you have to use the left move block of that piece and that varies by piece. So because of that i don't see how you can use the same block position on all of them to detect if its a collision because it all varies.

[Edited by - cwl157 on June 21, 2008 1:56:17 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!