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:)
Cave (white is transparent)
Background:
All three added together:
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;
}