Per Pixel Collision Detection

Posted by CJ Cohorst on Game Development See other posts from Game Development or by CJ Cohorst
Published on 2012-03-16T19:56:17Z Indexed on 2012/10/17 5:27 UTC
Read the original article Hit count: 454

Filed under:
|

Just a quick question, I have this collision detection code:

public bool PerPixelCollision(Player player, Game1 dog)
{
    Matrix atob = player.Transform * Matrix.Invert(dog.Transform);

    Vector2 stepX = Vector2.TransformNormal(Vector2.UnitX, atob);
    Vector2 stepY = Vector2.TransformNormal(Vector2.UnitY, atob);

    Vector2 iBPos = Vector2.Transform(Vector2.Zero, atob);

    for(int deltax = 0; deltax < player.playerTexture.Width; deltax++)
    {
        Vector2 bpos = iBPos;
        for (int deltay = 0; deltay < player.playerTexture.Height; deltay++)
        {
            int bx = (int)bpos.X;
            int by = (int)bpos.Y;

            if (bx >= 0 && bx < dog.dogTexture.Width && by >= 0 && by < dog.dogTexture.Height)
            {
                if (player.TextureData[deltax + deltay * player.playerTexture.Width].A > 150 && dog.TextureData[bx + by * dog.Texture.Width].A > 150)
                {
                    return true;
                }
            }
            bpos += stepY;
        }
        iBPos += stepX;
    }
    return false;
}

What I want to know is where to put in the code where something happens. For example, I want to put in player.playerPosition.X -= 200 just as a test, but I don't know where to put it. I tried putting it under the return true and above it, but under it, it said unreachable code, and above it nothing happened. I also tried putting it by bpos += stepY; but that didn't work either. Where do I put the code? Any help is appreciated. Thanks in advance!

© Game Development or respective owner

Related posts about XNA

Related posts about collision-detection