1

xnaで小さな2Dゲームを作ろうとしています。

さて、問題は次のとおりです。

衝突している 2 つのコンポーネントを検出する方法については、以下の図を参照してください。

ここに画像の説明を入力 上記のコンポーネントは 2 つの PNG コンポーネント形式です

これらのコンポーネントをゲームに配置して移動することに成功しました。

これらのコンポーネントが衝突したときにこれらのコンポーネントを検出したいと思います。私も衝突コードを作成しましたが、それはすべて写真の寸法に基づいているため、ピクセルごとに衝突している場合は色が付けられていませんが、写真間の寸法が含まれています.(写真の寸法に基づいた衝突半径と同じことを意味します)それらは、ピクセル色の間の衝突の前に衝突と見なされます

さて、どうすればそれを行うことができますか?

4

1 に答える 1

2

衝突コードを変更して、衝突が発生した可能性のある四角形を返し、その領域を使用して各ピクセルの各アルファ値を確認します。より詳細に調べたい場合は、ピクセルごとの衝突検出と呼ばれます。

編集:

//Load the texture from the content pipeline
Texture2D texture = Content.Load<Texture2D>("Your Texture Name and Directory");

//Convert the 1D array, to a 2D array for accessing data easily (Much easier to do            
Colors[x,y] than Colors[i],because it specifies an easy to read pixel)
Color[,] Colors = TextureTo2DArray(texture);

そして TextureTo2DArray() は

Color[,] TextureTo2DArray(Texture2D texture)
{
    Color[] colors1D = new Color[texture.Width * texture.Height]; //The hard to read,1D array
    texture.GetData(colors1D); //Get the colors and add them to the array

    Color[,] colors2D = new Color[texture.Width, texture.Height]; //The new, easy to read 2D array
    for (int x = 0; x < texture.Width; x++) //Convert
        for (int y = 0; y < texture.Height; y++)
            colors2D[x, y] = colors1D[x + y * texture.Width];

    return colors2D;
}
于 2012-12-27T15:06:29.483 に答える