# Where should I place my reaction code in Per-Pixel Collision Detection?

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?

## 1 Answer

It sounds like you need to learn how to use the debugger. I found a dubugger tutorial for XNA in parts One and Two. It says advanced, but it starts out pretty basic with breakpoints and ramps up from there.

If you want to test your collision code, you can set a break point at the top and step through it, or you can set breakpoints at the return case you want to look at. I recommend starting with setting the breakpoint at the top and stepping though your code. I think you're at a pretty beginner level, and it would be very valuable to see how your code is actually executed. Once you look at this enough I think you'll be able to figure out why putting code after a return statement will never be reached.

• I put the System.Diagnostics.Debug.WriteLine("This is my debug message."); in every spot possible and put a breakpoint at the top. Nothing wrote out. – CJ Cohorst Mar 17 '12 at 2:21
• Then function isn't getting called. Or you're not running in debug mode. – MichaelHouse Mar 17 '12 at 2:32