public Boolean CollisionCheck(float x1, float y1, float width1, float height1, float x2, float y2, float width2, float height2)
        {
            float left1 = x1;
            float left2 = x2;
            float right1 = x1 + width1;
            float right2 = x2 + width2;
            float top1 = y1;
            float top2 = y2;
            float bottom1 = y1 + height1;
            float bottom2 = y2 + height2;

            if (bottom1 < top2)
                return false;
            if (top1 > bottom2)
                return false;
            if (right1 < left2)
                return false;
            if (left1 > right2)
                return false;

            return true;
        }

That's my collision detection code, which should return true is 2 boxes are touching in any direction.

It works if I'm comparing 2 different images (Ie. the floor tiles and the character (Pacman - if Pacman isn't touching the floor tile he falls, dies, and respaws:

for (int i = 0; i < noOfTiles; i++)
                {
                    if (CollisionCheck(pacmanPos.X, pacmanPos.Y, frameSize.X, frameSize.Y, tilePos[i].X, tilePos[i].Y, frameSize2.X, frameSize2.Y))
                    {
                        //continue as normal
                    }
                    else
                    {
                        pacmanPos.Y ++;
                    }

                }

However, I have an array of enemies (munchies) and trying to stop them spawning on top of each other is proving difficult (They re-spawn in a random x and y coordinate):

for (int i = 0; i < noOfMunchies; i++)
                    if (CollisionCheck(munchiePos[i].X, munchiePos[i].Y, munchieSize, munchieSize, munchiePos[i].X, munchiePos[i].Y, munchieSize, munchieSize))
                    {
                        // change the position of the munchie that collided
                        munchiePos[i].X = Math.Max(0, rand.Next(gameWidth) - munchieSize);
                        munchiePos[i].Y = Math.Max(-500, rand.Next(gameHeight) - gameHeight);
                    }
                }

I understand the problem, it's just checking a collision with the munchie at position i, with the same munchie at position i... but I don't understand how to fix it :/
Thank you to anyone who can help, i'm truly stuck T_T

I have an array of enemies (munchies) and trying to stop them spawning on top of each other is proving difficult (They re-spawn in a random x and y coordinate):

for (int i = 0; i < noOfMunchies; i++)
                    if (CollisionCheck(munchiePos[i].X, munchiePos[i].Y, munchieSize, munchieSize, munchiePos[i].X, munchiePos[i].Y, munchieSize, munchieSize))
                    {
                        // change the position of the munchie that collided
                        munchiePos[i].X = Math.Max(0, rand.Next(gameWidth) - munchieSize);
                        munchiePos[i].Y = Math.Max(-500, rand.Next(gameHeight) - gameHeight);
                    }
                }

I understand the problem, it's just checking a collision with the munchie at position i, with the same munchie at position i... but I don't understand how to fix it :/
Thank you to anyone who can help, i'm truly stuck T_T

If you want to check for collisions between munchies, one way to do it is to use two loops, along these lines:

for(int i = 0; i < noOfMunchies - 1; i++)
{
    for(int j = i; j < noOfMunchies; j++)
    {
        // Check for collision between munchies i and j here.
    }
}

This is a very simple, inefficient technique, but it serves to illustrate a point: One loop isn't enough to detect all of the collisions.

If you use this method or something like it, beware of modifying positions during the loop. The safe way to do it would be to only modify the munchie at position j , and in such a way that it can't collide with any of the munchies at positions 0..j-1 .

You also might look into rejection sampling when you respawn a munchie--just generate random coordinates until the new one doesn't collide with any of the existing ones. This technique results in simple code, but it can be inefficient.

I've just tried that, and I have tried a variation of that in previous attempts; it just seems to keep the munchies constantly re-spawning at the top of the screen as if they're constantly colliding with one another... =S

I've just tried that, and I have tried a variation of that in previous attempts; it just seems to keep the munchies constantly re-spawning at the top of the screen as if they're constantly colliding with one another... =S

Can you post the new code?

If you want to check for collisions between munchies, one way to do it is to use two loops, along these lines:

for(int i = 0; i < noOfMunchies - 1; i++)
{
    for(int j = i; j < noOfMunchies; j++)
    {
        // Check for collision between munchies i and j here.
    }
}

This is a very simple, inefficient technique, but it serves to illustrate a point: One loop isn't enough to detect all of the collisions.

If you use this method or something like it, beware of modifying positions during the loop. The safe way to do it would be to only modify the munchie at position j , and in such a way that it can't collide with any of the munchies at positions 0..j-1 .

You also might look into rejection sampling when you respawn a munchie--just generate random coordinates until the new one doesn't collide with any of the existing ones. This technique results in simple code, but it can be inefficient.

Just a side note, make sure you don't check for a collision with itself. (ie if i != j then do the check).

Just a side note, make sure you don't check for a collision with itself. (ie if i != j then do the check).

Ah, that got it! Thank you, and thank you gusano79 for the help :)

// Munchie colliding with the munchies
                for (int i = 0; i < noOfMunchies - 1; i++)
                {
                    for (int j = i; j < noOfMunchies; j++)
                    {
                        if (i != j)
                        {
                            if (CollisionCheck(munchiePos[i].X, munchiePos[i].Y, munchieSize, munchieSize, munchiePos[j].X, munchiePos[j].Y, munchieSize, munchieSize))
                            {
                                munchiePos[j].X = Math.Max(0, rand.Next(gameWidth) - munchieSize);
                                munchiePos[j].Y = Math.Max(-500, rand.Next(0) - munchieSize);
                            }
                        }
                    }
                }

Oh right.

Also, instead of this:

for (int i = 0; i < noOfMunchies - 1; i++)
{
    for (int j = i; j < noOfMunchies; j++)
    {
        if (i != j)
        {
            // ...
        }
    }
}

You can do this:

for (int i = 0; i < noOfMunchies - 1; i++)
{
    for (int j = [B]i + 1[/B]; j < noOfMunchies; j++)
    {
        // ...
    }
}

Saves you time in the inner loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.