handy.gif (1372 bytes)


Maybe this collision detection is handy but maybe you already knew 'cause it's very simple. I'll will fully explain it and give the required code. You can only use this type of detection on let's say a menu for a game. The program starts, you display a logo and than the mainmenu. You've several buttons like 'New game', 'Load game', 'Credits', 'Exit game', .... and you've also placed a mousepointer on the screen which the player can move to make his choice. This tutorial is divided into several steps to make it easier to follow.

Step 1: Information needed.

It is very important that you know the exact position of all your buttons and there height and width. Let me give you an example:tut1.gif (2860 bytes)

Let's say that X = 20 and Y = 60.  Now we want to detect if the mousepointer touches the button.

Step 2: the actual code     

To detect the touch, you can write a little function like 'int CheckCollision()'; and it should be placed after your other inits in the mainfunction.

Now the only thing we've to do is to detect whether or not the mousepointer is in the square of the button. Therefor you need the height and width of the button and put all this information together in the 'Collision function'. It should be something like this:

int DetectCollision()

{

    if(start.x <= mouse.x <= start.x+35 && start.y <= mouse.y <= start.y+28)
    {

    //start is from the GsSPRITE struct. Let's give the button an other colour. Default is 155, 155, 155(=grey) and now we set the values to 200, 200,     //255.(=some sort of blue/purple)     
   start.r = 200;
    start.g = 200;
    start.b = 255;
    PadStatus = PadRead();
   
   if(PadStatus & PADcross)
    {
    //whatever you wanna call
    }

}    

Step 3:Explanation

Line 1:

Start.x = x position of the startbutton

mouse.x = x position of the mousepointer. Note: if you make a mousepointer you'll have to paint the point in the above left corner of your                      drawingsquare to make the collision as detailed as possible.

start.x+35 = the x position of the startbutton plus the width of the button. In place of 35 you must fill in the width of your picture.

start.y = y position of the startbutton

mouse.y = y position of the mousepointer

mouse.y+28 = y position of the mousepointer + the height of the button. In the place of 28 you must fill in the height of your button.

Line 2, 3, 4:

start.r = 200; defines the amount of red colour when collision is detected

start.b = 200; defines the amount of blue colour when collision is detected

start.g = 255; defines the amount of green colour when collision is detected

You can fill in what you want here. (0-255) This RGB values define the change of colour after collision detection. It needs some experimenting before finding the good colour.

Line 5:

PadStatus = PadRead(); This assigns the controller return values to PadStatus which is a U_LONG type.

Line 6-9:

if(PadStatus  & PADcross) = this waits until the player presses the crossbutton on the controller and than launches any function you want. Note: PADcross depends from how you defined it in your controller header file. But if you change it here, don't forget to change it in the headerfile too.


Et voila, your collision detection is ready and so is my tutorial. You can expect new tutorials soon!! (Mostly about 2D)