Note that there are some explanatory texts on larger screens.

plurals
  1. POPixel level collision detection fails for small objects. (Java)
    primarykey
    data
    text
    <p>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. </p> <p>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.</p> <p>Here is my code for the pixel level test:</p> <pre><code>/** * 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 &lt; width; i++) { for (int j = 0; j &lt; 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) &amp; (mask2)) != 0)//bitwise and, if both are nonzero there is a collsion { return true; } mask1 = 0; mask2 = 0; } } return false; } </code></pre> <p>I've been struggling with this for days and any help will be greatly appreciated. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload