gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Pixel collsion problems

#41117 - Strobe - Mon Apr 25, 2005 2:09 pm

Hi All,

I've only just started off coding for the GBA and so far i've made some great progress thanks to the wealth of onfo available on this forum. I'm going to stick my neck out though and ask for help on a problem that i haven't been able to find any previous postings for.

Please excuse me if this doesn't make any sense.

I've been playing aroud with collision detection for a sprite against a text background. I created a collision map with MapEd and have been able to get bounding box detection working with thanks to staringmonkey and his RPG demo code. However I've also been wanting to do some some per pixel detection and tried playing around with the function in staringmonkeys code. This seems to work when i use a sprite that is only 1 tile wide and however many high but doesn't if the sprite is 2 or more tiles wide. I'm trying to do a 16 x 16 sprite with some transparency around the edges. After using pcx2gba to create the sprite data and setting the transparent pixels to value 0 i can display the sprite with no problems however i can't get the collision detection routine to work correctly.

I'm using 1D mapping for the sprites and the problems seems to be with indexing the current pixel being processed for collision against the sprite data to see if it is transparent(solid or not). I'm checking the sprite data just as below

for(y = 0; y < height; y++) //Height is 16
{
for(x = 0; x < width; x++) //Width is 16
{
if(sprite[y*width+x])

I can see from looking at the sprite data produced by pcx2gba that it is arranged tile by tile and not in a linear fashion like a bitmap. If that makes sense.

In other words if the value checked evaluates to

if(sprite[10])

then this will check the 10th pixel of the 1st tile rather than the 2nd pixel of the secod tile along the top.

I'm guessing i need to calculate some kind of offset based on the number and format of the tiles in the sprite?

This probably makes no sense as i've gone round in circles and got my head too far into the problem to step back. Either that or there is a much better way of doing pixel level collision?

However, if anyone can understand the issue i'm having and can offer a solution i would be most grateful.

Thanks again

#41126 - sajiimori - Mon Apr 25, 2005 5:47 pm

A better way is to store collision data seperately, using 1 bit per pixel in a linear array. Then you can collide pairs of rows with just a bitwise 'and'. If the result is nonzero, there was a collision.

#41127 - Strobe - Mon Apr 25, 2005 6:00 pm

Thanks Sajiimori,

I'd liketo give it a go, However.....

What method would you use to create the collision data. Can this be created by reading in the sprite data in a certain way so that it is formated in a linear fashion?

Sorry if i seem a little sloooow, kinda new to this.

Thanks

#41130 - sajiimori - Mon Apr 25, 2005 8:03 pm

Well, you could read back the tiled graphics into a linear array, but if you've got your original bitmap files, you could use a library that can read that format directly.

Beyond that, if you know how to read and write files, and you know how to do bitwise operations, that's all you should need.