Jump to content
  • Advertisement
Sign in to follow this  
Son of Cain

[java] Pixel perfect collision detection

This topic is 4646 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Quickie: If I have the byte arrays of alpha values of both sprites for whom I'm checking for a collision detection:
byte[] myAlpha = me.getSprite().getAlpha();
byte[] hisAlpha = him.getSprite().getAlpha();
How do I use these arrays to check for a collision? I know, I'm a noob, but I couldn't find help on google... =/ BTW, I'm getting these byte arrays using the following code:

// texImage is a BufferedImage

byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData();
byte[] alpha = new byte [texImage.getWidth() * texImage.getHeight()];
int count = 0;
for ( int i = 0 ; i < data.length ; i++ ) {
    if(i % 4 == 0 && i != 0) {
        alpha [ count ] = data [ i - 1 ];
            count++;
        }
    }
}
Thanks in advance, Son Of Cain

Share this post


Link to post
Share on other sites
Advertisement
Forgive my ignorance, but why would you use the alpha values to check for collision? Wouldn't that cause weird results if the values happened to be the same but were on different sides of the screen?

Share this post


Link to post
Share on other sites
Dumb me, you're right.

I mistook what to sample. It's not the alpha values I need, but pixels on the region where the both bounding rectangles intersect.

Dumb dumb me.. Gotta study more this stuff!

Sorry!

Son Of Cain

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!