Tuesday, 17 April 2012

Pixel Collision

Today I implemented pixel collision to the program. It will be usedfor checking if the beam has hit a mirror and if so,what kind of mirror. I'd been working on it for a while but today I fixed all the bugs in it. I created a bool function called pixelcollision which is passed in values which are then checked to seeif they are colliding with a certain colour. If the pixel is colliding, it will return the value true The code for the function is:

bool pixelcollision(BITMAP * backBuffer, int x, int y, int width, int height, int color)
{
if (getpixel(backBuffer, x - 1, y) == color || getpixel(backBuffer, x, y -1 ) == color ||
getpixel(backBuffer, x + width + 1, y) == color || getpixel(backBuffer, x, y + height + 1) == color)
return true;
return false;
}



So if the function returns true, the direction of the beam will change. I'm still trying to work out the trigonometry for the angle of changing the direction, so for the moment I'm just reversing the x and y coordinates of it.




if(pixelcollision(backBuffer, X, Y,width,height,makecol(0,255,0)))
{


directionx = directionx*(-1);
  directiony = directiony*( -1);

}



This just makes the beam hit a green object and then reverse the direction it's heading in.


Starting points


Blue Box moving at x = -1 y = 0



Blue box has hit the green box, so now the direction is x = 1, y = -1




No comments:

Post a Comment