Pixel-Perfect Collisions

[ LiB ]

Pixel-Perfect Collisions

So far, we have been doing all of our collision tests with pixel-imperfect calculations. Do you understand the difference between pixel-imperfect collisions and pixel-perfect collisions? Let me help you understand.

When using pixel-imperfect collisions, every collision that occurs is approximate. This means that while the collision may not actually occur, usually it is so close that it seems like it did. This creates a problem, sometimes, when the collision obviously should not have happened . Referring to Figure 9.10, you can see how a bounding box collision can be extremely inaccurate. In this figure, the bounding boxes have slightly overlapped , yet the actual objects are still very far apart from one another.

Figure 9.10. The problems with pixel-imperfect collisions.


However, Blitz Basic provides a very easy way to fix this problem. Simply use the function

ImagesCollide() instead of ImagesOverlap() .

ImagesCollide() is declared like this:

 ImagesCollide (image1,x1,y1,frame1,image2,x2,y2,frame2) 

Table 9.4 summarizes ImagesCollide 's parameters.

Table 9.4. ImagesCollide()'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.

frame1

The frame of the first image you want to check, unless using animation, set to 0.

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.

frame2

The frame of the second image you want to check, unless using animation, set to 0.


ImagesCollide() checks every non-transparent pixel of the first image to see if it is overlapping with a non-transparent pixel of the second pixel. If so, a collision is reported . However, this checking means that your program will run a bit slower using ImagesCollide() than when using ImagesOverlap() .

When using ImagesCollide() , collisions will look more like those in Figure 9.11.

Figure 9.11. Using ImagesCollide() .


I rewrote demo09-05.bb to make demo09-06.bb. The only change I made was in the main loop.

 ;If player and enemy collide, increment collisions and reset player and enemy If (ImagesCollide(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 

Play through and see if you notice the differences.

Before we finish up this chapter, I just want to warn you about overusing ImagesCollide() . When using ImagesCollide() a lot, your program can experience a drastic slowdown . Unless you are sure it is necessary, a lot of times it is better to stick with ImagesOverlap() .

[ 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