• 9
• 10
• 10
• 9
• 10

# Tetris collision detection (java)

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

## Recommended Posts

Hi! I'm currently writing a Tetris game, just for some practice and also, because it's fun. I figured out how I want to do collision detection, I'm aware of that there are probably thousand other ways to do it, that are better than how I do it. But I'll stick with my way of doing it for now, and if I run in to problems later that will be a lesson learned. So please don't comment on ineffective coding/bad coding, because I already know that it is. So I just wrote the part that's supposed to check for a collision when a piece is falling. I'm using 2 2d arrays, one for the location of the falling piece, and one for the location of the "inactive" pieces. To check if there's a collision between a falling piece and an inactive piece I do this:

for(int i = 0; i < width - 1; i++)
{
for(int n = 0; i < height - 1; n++)
{
if(fallingpiece[n] > 0)
{
if(inactivepiece[n + 1] > 0)
{
}
else
{
}
}
}
}

Before you say I'm stupid, I know that I don't actually do anything. But as soon as I run that code the program just doesn't continue, to the point after the code. width = 10 height = 20 the 2d arrays are both [10][20]. I'm probably just missing something obvious here, but I can't see where or what. The only thing I can think of is that I'm doing something with 2d arrays that I can't do. I never worked with 2d arrays before. If you need more information just say so, and I'll tell you what you want to know :). Thanks for any help in advance! PS. Forgot to mention the reason that I check if fallingpiece[n] is greater than 0, is because it can be any integer between 1 and 7, and each number is one of the colors. [Edited by - Bluestream on March 8, 2008 7:17:25 AM]

##### Share on other sites
Quote:
 Original post by Bluestreamwidth = 10height = 20the 2d arrays are both [10][20].

Aren't arrays constructed as array[height][width]? If so, you might be having some odd results from switching these.

##### Share on other sites
Quote:
 Original post by ShakedownAren't arrays constructed as array[height][width]? If so, you might be having some odd results from switching these.

I don't see the difference. Please, correct me if I'm wrong but the array in itself is just 10 * 20 storage places, I just chose to use it the way I did. And as far as I understand it wouldn't make any difference the other way around(?). What I mean is that you, instead of creating an array like int[200] you can create a 2d array like, int[10][20]. Just because it's easier to work with.

##### Share on other sites
Hello,

It's a classic error and very easy to miss ;-)

for(int n = 0; i < height - 1; n++)

you're checking i insteed of n

kind regards
Uncle

##### Share on other sites
You have created an infinite loop, because the condition for the inner most loop is checking the value of "i", but this variable is never incremented.

for(int i = 0; i &lt; width - 1; i++)        {           // Problem is here: "i &lt; (height - 1)"           for(int n = 0; i &lt; height - 1; n++)            {                if(fallingpiece[n] &gt; 0)                {                    if(inactivepiece[n + 1] &gt; 0)                    {                    }                    else                    {                    }                }            }        }

##### Share on other sites
Quote:
 Original post by UncleRemusHello,It's a classic error and very easy to miss ;-)for(int n = 0; i < height - 1; n++)you're checking i insteed of nkind regardsUncle

Ah, of course! Thanks a lot! Thought it was odd :).

##### Share on other sites
Quote:
 Original post by Bluestreamfor(int i = 0; i < width - 1; i++)width = 10height = 20the 2d arrays are both [10][20].

Valid array indices range from 0 to size-1, but your loop only iterates from 0 to width-2 (because width-1 is not smaller than width-1). This is the correct idiom:

for(int i = 0; i < width; i++)

This will iterate all valid indices, from 0 to size-1.

##### Share on other sites
Another one:

The line

if(inactivepiece[n + 1] > 0)

will get an ArrayIndexOutOfBoundsException if n ever reaches height - 1. Array indices start by 0 and go up to array length - 1. +1 is one too much.

You might put a safety check there.