For a project we were given a game engine off which to create a game. We, as part of this, have to implement pixel level collision detection after a possible collision has been found via a bounding box detection method. I have implemented both but my pixel level test fails for small objects (bullets in this case). I have checked if it works for slow bullets but that fails too.
For my pixel level implementation I create bitmasks for each texture using an the available IntBuffer (a ByteBuffer is available too?). The IntBuffer is in RGBA format and its size is width*height, I placed this in a 2D array and replaced all non-zero numbers with 1's to create the mask. After a collision of bounding boxes I find the rectangle represented by the overlap (using .createIntersection) and then check the maps of both sprites within this intersection for a nonzero pixel from both using bitwise AND.
Here is my code for the pixel level test:
/**
* 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;
}
I've been struggling with this for days and any help will be greatly appreciated.
file:///C:/Documents%20and%20Settings/ser/Desktop/java3d/Shoelace%20formula%20-%20Wikipedia,%20the%20free%20encyclopedia.htmyou can find area of one of the shapes........ If area of union-of-them is smaller than sum of areas of distinct shapes, then they are colliding. This is O(n+m) way better than O(n*n) - huseyin tugrul buyukisik