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.

Coding > Collision Detection Half Working

#36159 - Zenith - Tue Feb 15, 2005 2:33 pm

I've been using StarringMonkeys RPG demo to try and understand his collision detection. I've created map data with Mappy but the problem is the collisions work fine for the Y axis but for the X axis the collisions seem to be squashed.

Can anyone see an obviuos error in the following function and data that I'm missing?

Thanks in advance for any help :)

Code:
void PerPixelCollision(u16 *sprite, int width, int height)
{
//Variables
int currentXTile;
int currentYTile;
int currentTile;
int x,y;

//Search each y pixel
for(y = 0; y < height; y++)
{
    //Search each x pixel
    for(x = 0; x < width; x++)
    {
        if(sprite[y*height+x] != 0x0)
        {
            //Set current pixel
            currentXTile = (charX + x)/ 8;
            currentYTile = (charY + y)/ 8;
            currentTile = currentYTile * 32 + currentXTile;

            //Check current pixel
            if(Map_map0[currentTile/2] != 6)
            {
                spriteX = lastSpriteX;
                spriteY = lastSpriteY;
                screenX = lastScreenX;
                screenY = lastScreenY;
                charX = spriteX + screenX;
                charY = spriteY + screenY;

                return;
            }
         }
      }
   }
}


Code:

const unsigned char Map_map0[1024] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
,
1, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 1
,
1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
,
1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1
,
1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 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 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1
,
1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1
,
1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1
,
1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1
,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2

};

#36162 - Zenith - Tue Feb 15, 2005 3:08 pm

Ignore the post I've just realised I was dividing by 8 when I should of been dividing by 16.

/em slaps self.

#36182 - Kyoufu Kawa - Tue Feb 15, 2005 7:46 pm

You're using StaringMonkey's code too eh? At least it's better than Final Fantasy style gridlocked movement.

#36344 - peebrain - Thu Feb 17, 2005 9:27 pm

Zenith wrote:
Code:
        if(sprite[y*height+x] != 0x0)



Shouldn't that be y * width + x ?

~Sean
_________________
http://www.pbwhere.com

#36368 - sgeos - Fri Feb 18, 2005 5:05 am

Kyoufu Kawa wrote:
You're using StaringMonkey's code too eh? At least it's better than Final Fantasy style gridlocked movement.

I like gridlocked games. =P

-Brendan