Question

I'm working on a XNA platform game and would need some help with the collision. The game takes place in a cave and the problem is that the art style will be sketchy and therefore the terrain( the caves) will differ a LOT, so I can't use tiles. But I need to check pixel perfect collision on the character and the cave but I can't figure out how to do so when I can't place rectangles around every tiles since there are none.

I have thought about it a lot came up with some ideas:

-One big rectangle around the whole level and one around the character and use pixel perfect collision. But I don't think that will work since the rectangle will include the background as well.

-Place out rectangles manually. Very ugly code and may cause a lot of bugs and errors.

-Use tiles anyway and have hundreds of tile types. Again, very ugly code and it just seems wrong.

-Use a collision engine. I would preferably make the game from scratch.

I'm sorry if I've explained badly, but it's a rather complicated problem(for me at least) and I can't find any solution around the net. Would be very glad for any ideas, thank you.

Was it helpful?

Solution

I think you should use per-pixel collision detection to do what you're trying to. You could have your character, and make his texture transparent (using the image's alpha) except for the actual character. Then you could have the terrain texture which would be your cave. Make the parts of the cave the character should be able to move transparent, so that you can have another texture which will be the background of the whole level. Here's a rough example:

Character (pink BG is transparent:)

enter image description here

Cave (white is transparent)

enter image description here

Background:

enter image description here

All three added together:

enter image description here

That's just to give you a very rough idea (since i just googled the background and drew the cave in paint). Then you could use alpha pixel collision detection between the cave texture (NOT the cave background) and the character texture. See this for a tutorial on how you could easily use per-pixel collision. HTH. Here is some collision code you could use (rectangleA should be the character's rectangle, dataA the character's pixel Data, rectangleB the rectangle of the cave (which will probably be the whole screen), and dataB the pixel data of the cave (NOT THE BACKGROUND)) Since you aren't using the pixel data of the background image, the collision won't be checked with this data:

    /// <param name="rectangleA">Bounding rectangle of the first sprite</param>
    /// <param name="dataA">Pixel data of the first sprite</param>
    /// <param name="rectangleB">Bouding rectangle of the second sprite</param>
    /// <param name="dataB">Pixel data of the second sprite</param>
    /// <returns>True if non-transparent pixels overlap; false otherwise</returns>
    static bool IntersectPixels(Rectangle rectangleA, Color[] dataA,
                                Rectangle rectangleB, Color[] dataB)
    {
        // Find the bounds of the rectangle intersection
        int top = Math.Max(rectangleA.Top, rectangleB.Top);
        int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
        int left = Math.Max(rectangleA.Left, rectangleB.Left);
        int right = Math.Min(rectangleA.Right, rectangleB.Right);

        // Check every point within the intersection bounds
        for (int y = top; y < bottom; y++)
        {
            for (int x = left; x < right; x++)
            {
                // Get the color of both pixels at this point
                Color colorA = dataA[(x - rectangleA.Left) +
                                     (y - rectangleA.Top) * rectangleA.Width];
                Color colorB = dataB[(x - rectangleB.Left) +
                                     (y - rectangleB.Top) * rectangleB.Width];

                // If both pixels are not completely transparent,
                if (colorA.A != 0 && colorB.A != 0)
                {
                    // then an intersection has been found
                    return true;
                }
            }
        }

        // No intersection found
        return false;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top