プロジェクトでは、ゲームを作成するためのゲーム エンジンが与えられました。その一環として、バウンディング ボックス検出方法によって衝突の可能性が見つかった後、ピクセル レベルの衝突検出を実装する必要があります。私は両方を実装しましたが、ピクセル レベルのテストは小さなオブジェクト (この場合は弾丸) では失敗します。遅い弾丸で機能するかどうかを確認しましたが、それも失敗します。
ピクセル レベルの実装では、使用可能な IntBuffer を使用して各テクスチャのビットマスクを作成します (ByteBuffer も使用できますか?)。IntBuffer は RGBA 形式で、そのサイズは width*height です。これを 2D 配列に配置し、ゼロ以外のすべての数値を 1 に置き換えてマスクを作成しました。バウンディング ボックスの衝突の後、(.createIntersection を使用して) オーバーラップによって表される四角形を見つけ、ビットごとの AND を使用して、この交差内の両方のスプライトのマップをチェックし、ゼロ以外のピクセルを探します。
ピクセルレベルテストのコードは次のとおりです。
/**
* Pixel level test
*
* @param rect the rectangle representing the intersection of the bounding
* boxes
* @param index1 the index at which the first objects texture is stored
* @param index the index at which the second objects texture is stored
*/
public static boolean isBitCollision(Rectangle2D rect, int index1, int index2)
{
int height = (int) rect.getHeight();
int width = (int) rect.getWidth();
long mask1 = 0;
long mask2 = 0;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
mask1 = mask1 + bitmaskArr[index1].bitmask[i][j];//add up the current column of "pixels"
mask2 = mask2 + bitmaskArr[index2].bitmask[i][j];
if (((mask1) & (mask2)) != 0)//bitwise and, if both are nonzero there is a collsion
{
return true;
}
mask1 = 0;
mask2 = 0;
}
}
return false;
}
私はこれに何日も苦労してきましたが、どんな助けも大歓迎です。