Pixel-Imperfect Collisions

[ LiB ]

Pixel-Imperfect Collisions

One thing you may have noticed is that we have been testing the ship only against single pixels. What if we want to test an image against another image? Say, a bullet against a ship, or a missile crash? Blitz Basic provides an excellent way to do this.

There is a function provided by Blitz Basic called ImagesOverlap() . It is defined like this:

 ImagesOverlap (image1,x1,y1,image2,x2,y2) 

Table 9.3 explains all the parameters.

Table 9.3. ImageOverlap()'s Parameters

Parameter

Description

image1

The handle to the first image you want to test for collision.

x1

The x coordinate of the first image.

y1

The y coordinate of the first image.

image2

The handle to the second image you want to test for collision.

x2

The x coordinate of the second image.

y2

The y coordinate of the second image.


Now, let's write a program that uses this. The following, demo09-05.bb, allows you to control a ship. If you hit the randomly moving ship that is also onscreen, a collision occurs.

The program is pretty easy to follow, so I am just going to list some important parts . Following are the types from the program, and the initial values for these types.

 ;TYPES ;The enemy ship is a randomly moving object on the screen Type enemyship         Field x,y    ;the x and y coordinates         Field xv,yv    ;The velocity          Field image   ;the image End Type ;The playership type defines the player Type playership         Field x,y   ;The x and y coordinate position         Field collisions    ;How many collisions have occurred         Field image    ;The player's image End Type ;Create the enemy and assign it default variables Global enemy.enemyship = New enemyship enemy\x = 400 enemy\y = 200 enemy\xv = Rand(-5,5) enemy\yv = Rand(-5,5) enemy\image = LoadImage("enemyship.bmp") ;Create player and assign it default and random variables Global player.playership = New playership player\x = 400 player\y = 400 player\collisions = 0 player\image = LoadImage("ship.bmp") 

The only major difference between the player and the enemy is that the player has velocity fields, xv and yv . These velocity values are added to the x and y coordinates of the enemy each frame and serve as movement values.

Following is the main loop. Notice how little it actually does.

 ;MAIN LOOP While Not KeyDown(1) ;Clear the screen Cls ;Make sure all text appears in top left corner Locate 0,0 Print "Collisions: " + player\collisions ;Find out if enemy hit a wall TestEnemyCollisions() ;Test keyboard TestKeys()  ;If player and enemy overlap, increment collisions and reset player and enemy If (ImagesOverlap(player\image,player\x,player\y,enemy\image,enemy\x,enemy\y))         player\collisions = player\collisions + 1         player\x = 400         player\y = 400         enemy\x = 400         enemy\y = 200 EndIf ;Move the enemy enemy\x = enemy\x + enemy\xv enemy\y = enemy\y + enemy\yv ;Draw the player and the enemy DrawImage enemy\image,enemy\x,enemy\y DrawImage player\image,player\x,player\y Flip Wend ;END OF MAIN LOOP 

The main loop does this: it prints out how many collisions have occurred, it calls TestEnemyKeys() and TestKeys() , it checks if any collisions have occurred, it moves the enemy, and it draws the two ships. The only major line of interest is the test.

 If (ImagesOverlap(player\image,player\x,player\y,enemy\image,enemy\x,enemy\y)) 

This tests if the player's image and the enemy's image have overlapped .

That's just about it for pixel-imperfect collisions. Now, let's learn how to find out if collisions have actually occurred, using pixel-perfect collisions.

[ 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