Thursday, 12 April 2012

Collision Detection

Today I finally began putting in collision detection to the program.

It took me a long-time to get it right but I now use collisions to enable the user to click and drag the mirrors around the screen to be placed in the path of the laser.

The algorithm works by detecting if the mouse pointer is inside the same area as the mirror's bitmap, and then when the left mouse button is pressed down, the mirror will follow the mouse's coordinates.


if (mouse_x > (position.x - 20) && mouse_x < (position.x + 20) && mouse_y > (position.y - 20) && mouse_y < (position.y + 20) && mouse_b & 1)
{

if (position.x < mouse_x)
position.x += 10;


if (position.x > mouse_x)
position.x -= 10;


if (position.y < mouse_y)
position.y+= 10;


if (position.y > mouse_y)
position.y -= 10;







Dragged by mouse:




New placement:





The next step is to work out the collision detection for the laser beam to reflect off the mirrors














No comments:

Post a Comment