Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [AGK] Pixel-perfect sprite collision

DBPro Master
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 18th Apr 2017 21:53 Edited at: 18th Apr 2017 21:55
This code was already posted in a thread started by Damo about pixel collision. It peaked my interested and I started the task myself and inadvertently hijacked his thread. But he didn't get a chance to posting any code before he disappeared again so all is good. I'm reposting it here in code snippets so hopefully others can find it more easily.

Currently, this works on any non-rotated sprites. I've posted the library code you need to include as well as a small demo. The general concept of my method is from blink0k's idea. I've written a short tutorial explaining the method and how it works. In short, a bit mask is created for each sprite (non-animated for now). It detects the overlapping area of the two sprites being checked and looks to see if any pixels in the bitmasks match up. If both pixels are a 1 in the mask at the overlapping position, then they've touched. The bitmask is just an array of simplified pixel data of the alpha channel; 0 is transparent, 1 is anything with an alpha value greater than 0. As we only need a single bit to represent each pixel in the mask, we store 32 pixels of information into a single integer. This cuts down big time on the memory usage and allows this method to be much more viable.

I'm currently working on rotation, so hopefully that will be an added feature in the near future.




Whole package with example sprites:

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Login to post a reply

Server time is: 2024-03-02 06:10:31
Your offset time is: 2024-03-02 06:10:31