對於一個項目,我們給了一個遊戲引擎來創建遊戲。作爲其一部分,我們必須在通過邊界框檢測方法發現可能的碰撞後實施像素級碰撞檢測。我已經實現了這兩個,但是我的像素級測試對於小物體(本例中爲子彈)失敗。我檢查了它是否適用於慢速子彈,但也失敗了。對於小物體,像素級碰撞檢測失敗。 (Java)
對於我的像素級實現,我使用可用的IntBuffer爲每個紋理創建位掩碼(ByteBuffer也可用?)。 IntBuffer是RGBA格式,其大小是寬*高,我把它放在一個二維數組中,並用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;
}
我一直在爲此而努力了幾天,任何幫助將不勝感激。
從'文件:/// C:/文件%20於是%20Settings/SER /桌面/ Java3D的/鞋帶%20formula%20-%20Wikipedia,第二十條%%20free%20encyclopedia.htm'你可以找到區其中一種形狀........如果它們的併合面積小於不同形狀的面積的總和,則它們正在碰撞。這是O(n + m)比O(n * n)好得多 –
感謝您的建議,但我必須實施這種測試。 – Mafro34