Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8509647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:29:27+00:00 2026-06-11T03:29:27+00:00

For a project we were given a game engine off which to create a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:29:29+00:00Added an answer on June 11, 2026 at 3:29 am

    I managed to solve my own issue and now it works properly. For anyone interested what I did was find the rectangle created by the overlap of the two bounding boxes of the two sprites. I then dropped each object to the origin along with it, relatively, the rectangle of intersection. It should be noted that I dropped each object to a “separate” origin – ie I effectively had two rectangle of intersection afterwards – one for each. The co-ordinates of each rectangle of intersection, now in bounds of the bitmask 2D arrays for both objects, were used to check the correct regions for overlap of both objects:

    I loop bottom to top left to right through the bitmask as the image data provided in upside – apparently this is the norm for image data.

     /**
     * My Pixel level test - 2D
     *
     * @param rect the rectangle representing the intersection of the bounding
     * boxes
     * @param index1 the index at which the first objects texture is stored
     * @param index2 the index at which the second objects texture is stored
     * @param p1 the position of object 1
     * @param p2 the position of object 2
     * @return true if there is a collision at a pixel level false if not
     */
    //public static boolean isPixelCollision(Rectangle2D rect, Point2D.Float p1, Bitmask bm1, Point2D.Float p2, Bitmask bm2)
    public static boolean isPixelCollision(Rectangle2D rect, Point2D.Float p1, int index1, Point2D.Float p2, int index2)
    {
        int height = (int) rect.getHeight();
        int width = (int) rect.getWidth();
    
        byte mask1 = 0;
        byte mask2 = 0;
    
        //drop both objects to the origin and drop a rectangle of intersection for each along with them
        //this allows for us to have the co-ords of the rect on intersection within them at number that are inbounds.
        Point2D.Float origP1 = new Point2D.Float((float) Math.abs(rect.getX() - p1.x), (float) Math.abs(rect.getY() - p1.y));//rect for object one
        Point2D.Float origP2 = new Point2D.Float((float) Math.abs(rect.getX() - p2.x), (float) Math.abs(rect.getY() - p2.y));//rect for object two
    
        //to avoid casting with every iteration
        int start1y = (int) origP1.y;
        int start1x = (int) origP1.x;
        int start2y = (int) origP2.y;
        int start2x = (int) origP2.x;
    
        //we need to loop within the rect of intersection
        //goind bottom up and left to right
        for (int i = height - 1; i > 0; i--)
        {
            for (int j = 0; j < width; j++)
            {
                mask1 = bitmaskArr[index1].bitmask[start1y + i][start1x + j];
                mask2 = bitmaskArr[index2].bitmask[start2y + i][start2x + j];
                if ((mask1 & mask2) > 0)
                {
                    return true;
                }
            }
    
        }
        //no collsion was found
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been given a new project module which involves fuzzy logic. It is
I have a Django project. Given a url, how can I know which view
This basic app which was given to me as an project should run quite
I have a GitHub project which does the same thing (a simple RogueLike game
I have been working on an opencv project for iOS. I was given a
I have been designing a game for a University project, and I thought it
I am making a battleship game for a project. While I have completed the
I'm working on a state based game engine and have something I'm happy with
Just additional information: this is for a game project I'm working on. I'm trying
I have recently begun coding a 3d computer game as part of my IB

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.