Basic Collisions

[ LiB ]

Before we learn how to check for collisions of objects (images, shapes , and so on), let's go over basic pixel collisions. To see if a pixel collision has occurred, you just check the pixel you are tracking and make sure that its x and y values are not the same as the object you are testing it against. See Figure 9.1 for an example.

Figure 9.1. Difference between a collision and no collision.


For the following program, demo09-01.bb, we will allow the player to control a single pixel that can be moved up, down, left, or right. If the pixel hits a wall (the wall being the edge of the screen), the pixel position will be reset and the collision counter will be updated. Following is the source for demo09-01.bb

 ;demo09-01.bb - Demonstrates Pixel Collisions Graphics 400,300 ;create variables that define coordinate position of pixel Global x = 200 Global y = 150    ;This variable contains the number of times a collision has occurred collisions = 0 ;CONSTANTS ;These are the key code constants Const UPKEY = 200, DOWNKEY = 208, LEFTKEY = 203, RIGHTKEY = 205 ;MAIN LOOP While Not KeyDown (1) ;Print text in upper-left corner Locate 0,0 Print "Press the arrow keys to move the pixel around." ;Print the number of collisions Print "Collisions: " + collisions ;Move player around depending on the key he pressed If KeyDown(UPKEY)         y = y - 5 ElseIf KeyDown(DOWNKEY)         y = y + 5 ElseIf KeyDown(LEFTKEY)         x = x - 5 ElseIf KeyDown(RIGHTKEY)         x = x + 5 EndIf ;Call the CheckForCollisions function and determine if a collision occurred collisions = CheckForCollisions(collisions) ;Draw the pixel on the screen Plot x,y ;wait a (fraction of a)sec Delay 100 Wend ;END OF MAIN LOOP ;FUNCTIONS ;Function CheckForCollisions(collisions) - Returns number of total collisions, ; tests for new ones  ;collisions: the number of collisions at the time of calling the function Function CheckForCollisions(collisions) ;If the pixel is offscreen, report a collision If x <= 0 Or x >= 400 Or y <= 0 Or y >= 300         collisions = collisions + 1     ;increment collisions         Cls     ;clear the screen         Text 100,150,"A Collision Has Occured"         Delay 1000     ;wait a sec         Cls     ;clear screen again         x = 200     ;reset x         y = 150     ;reset y EndIf ;return the number of collisions Return collisions End Function 

This program works pretty much as you would expect it to. It begins by setting the graphics and creating the variables x , y , and collisions . It then enters the main loop.

Inside the main loop, the program checks to see if any arrow keys have been pressed. If so, it increments the x and y variables accordingly . The program also displays the number of collisions at the top of the screen.

Near the end of the loop, the program calls the function CheckForCollisions() . It includes collisions as a parameter. It also sets collisions equal to the return value of the function.

We now go into the CheckForCollisions() function. The first and hardest part of the function to understand is the test. The test looks like this:

 If x <= 0 Or x >= 400 Or y <= 0 Or y >= 300 

This test checks to see if the point has gone offscreen. Referring to Figure 9.2, you see that the x tests pertain to the right and left walls of the screen and the y tests pertain to the upper and lower walls.

Figure 9.2. The wall tests.


Now, if the program finds that the point has hit one of the walls, it begins its reset procedure. First, it adds 1 to collisions , which increases the collision counter by 1. It then displays "A Collision Has Occurred" on the screen. The x and y coordinates are then reset.

Whether or not a collision occurs, the function returns the value of collisions to the main loop. If there was no collision, collisions will remain the same; if there was a collision, collisions will be increased by 1.

The rest of the main loop draws the pixel on the screen and delays the program for 1/10th of a second. That's it for demo09-01.bb.

[ LiB ]


Game Programming for Teens
Game Programming for Teens
ISBN: 1598635182
EAN: 2147483647
Year: 2004
Pages: 94
Authors: Maneesh Sethi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net