Sign in to follow this  
Bluestream

Tetris collision detection (java)

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[i][n] > 0)
                {
                    if(inactivepiece[i][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[i][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 this post


Link to post
Share on other sites
Quote:
Original post by Bluestream
width = 10
height = 20

the 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 this post


Link to post
Share on other sites
Quote:
Original post by Shakedown
Aren'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 this post


Link to post
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[i][n] &gt; 0)
{
if(inactivepiece[i][n + 1] &gt; 0)
{
}
else
{
}
}
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by UncleRemus
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


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

Share this post


Link to post
Share on other sites
Quote:
Original post by Bluestream

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

width = 10
height = 20

the 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 this post


Link to post
Share on other sites
Another one:

The line

if(inactivepiece[i][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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this