This time we'll take about collisions. There are multiple types of
collisions, but the one we'll talk about here is the collision between 2
sprites. First of all some explanation about a few words I'm going to use:
- raw-edge = the edge of the complete shape (rectangle)
- real-edge = the actual "edge" of the image.
So if the image would be a circle the raw-edge would still be a cube that
surounds the image (the sprite), and the real-edge would be the circle edges
How to Do It
Collision detection in games is one of the most important things to get
right, because you don't want the player to get hit while there's no
alien actually touching him... on the other side you don't want the player
to be able to go "thru" another object. There are a few ways of collision
detection, but not all of them are very effective in terms of speed, and
pixel-perfection. A good collision detection would check on all the real-
edge pixels of an image, but that would be very S-L-O-W! Just think about
it, if you would use 32x32 sprite-images, and you had about 50 sprites on
screen, you would have to check 51200 pixels, that's almost a complete
screen! ofcourse this could be done faster by adding some extra image
information about where the real-edges are, but it would still be very slow
to check it completely.
Another way of checking is a little bit more math-coordinated, you could
make some triangles, or maybe even rectangled areas that "surround" the
sprites real-edge, this is medium-fast, and medium-perfect so it might be
worth checking into that...BUT we need speed! so,
The Fast Way
A faster way is not as pixel-perfect as should, but close enough to
fool the player. The fast way would be to check on collision inside of the
raw-edge only. This is fast enough because you'll only have to check on 4
co"rdinates per sprite. Ofcourse this isn't that perfect because
you're image's may not be completely filled to the raw-edge. For a more
effective working of this idea, you'll have to draw you're real-edges as
close to the raw-edges as possible, leaving a minimum of "empty-ness" at the
edge of the images. Those few bytes that are empty, won't get in the way of
the gameplay because the player won't see it, and doesn't know about it!
And you could also make the collision-rectangle a bit smaller then the true
sprite-rectangle (leaving some pixels for player-errors).
No example file is attached, but I'll give a simple collision detection
Rough check if 2 shapes are colliding. This works faster as a
pixel by pixel check for a collision, and as long as the shapes don't
have to much "space" on the edges the collision will be almost
Expects: X+Y position and the WIDTH + HEIGHT of the first sprite,
X+Y position and the WIDTH + HEIGHT of the second sprite.
Returns: True if collision has been detected.
FUNCTION collision(x1,y1,w1,h1, x2,y2,w2,h2 :integer):boolean;
VAR xd,yd : boolean;
if ((x1 <=x2) and (x1+w1>=x2)) then xd:=true else
if ((x1 <=x2+w2) and (x1+w1>=x2+w2)) then xd:=true else
if ((x1 >=x2) and (x1+w1<=x2+w2)) then xd:=true;
if ((y1 <=y2) and (y1+h1>=y2)) then yd:=true else
if ((y1 <=y2+h2) and (y1+h1>=y2+h2)) then yd:=true else
if ((y1 >=y2) and (y1+h1<=y2+h2)) then yd:=true;
collision:=xd and yd;
The example shown above was taken from my SuperFX Engine, it work's
fine and it looks like a pixel-perfect collision detection.
Well that's it for now, The next topic is brand new, and is also new for on
the web-site, so keep reading for:
Next Tech topic: Demo recording/playing