やろうとしていることを行うには、ピクセルごとの衝突検出を使用する必要があると思います。alpha
キャラクターを作成し、実際のキャラクターを除いて、(画像の を使用して) テクスチャを透明にすることができます。次にterrain
、洞窟となるテクスチャを作成できます。キャラクターが移動できる洞窟の部分を透明にして、レベル全体の背景となる別のテクスチャを使用できるようにします。大まかな例を次に示します。
キャラクター(ピンク色のBGは透明です:)

洞窟(白は透明)

バックグラウンド:

3 つすべてを合計すると、次のようになります。

これは、非常に大まかなアイデアを提供するためのものです (背景をググって、洞窟をペイントで描いたからです)。次に、洞窟のテクスチャ (洞窟の背景ではない) とキャラクターのテクスチャの間でアルファ ピクセルの衝突検出を使用できます。ピクセルごとのコリジョンを簡単に使用する方法については、こちらのチュートリアルを参照してください。HTH。使用できる衝突コードを次に示します (rectangleA
文字の四角形、dataA
文字のピクセル データ、rectangleB
洞窟の四角形 (おそらく画面全体)、およびdataB
洞窟のピクセル データ (背景ではありません) である必要があります)。背景画像のピクセル データを使用していない場合、衝突はこのデータではチェックされません。
/// <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;
}