The technique you use depends on the game. For some games, bounding boxes
are enough. For wireframe games there are algorythms to check whether lines
cross (which can in some cases give you better-than-pixel collision
detection). In sprite-based games, a pixel-by pixel check is the ultimate
collision test, although it’s very expensive.
The best way to resolve collisions IMHO is to use three stages - first, only
check collisions among those sprite whose collision has a game effect (e.g.
in a platform game enemies don’t usually affect each other, so just test
whether they collide with the player, not with each other). Second, use a
cheap, rough test - like checking bounding boxes or assigning a series of
zones to the map area - sprites in different zones aren’t tested for
collisions. Finally, when the simple methods all indicate a collision, use a
pixel-perfect method.
I’ve attached a C++ class file and source for the CollisionMask class I use.
There are two important functions - one, the creator, makes a collision mask
from an SDL_Surface with an alpha channel. I create the sprites I use as
PNG’s with alpha data, and this function creates a pixel mask where 0
represents a pixel in the orignal image with alpha <= 128, 1 where alpha >
128. (Note - send SDL_Surfaces to this routine before you convert them to
the display format).
The other routine does stages 2 and 3 of my above list - given another mask
and the x,y coordinates of both mask’s top left corners, it checks the
bounding boxes of the collision masks and does a pixel-perfect test in the
event that it needs to.
Feel free to use, share, modify the code - if you make any great
improvements or find any serious bugs I’d like to hear about them. There is
one bug outstanding (too minor to make the top of my todo list, although I
will hunt it down soon), whereby the create routine fails for some images
which are opaque in the top left corner. But the other routine works 100%,
as far as I can tell, and the create routine works 95% of the time, and
always fails reliably. (i.e. no random fails), so it’s safe to use.
Keith Lawrence
@Keith_Lawrence> ----- Original Message -----
From: viller.koskinen@iobox.com (Ville Koskinen)
To:
Sent: Thursday, January 17, 2002 10:00 AM
Subject: [SDL] Sprite collisions
What is the best way to test if two sprites collide with each other? It’s
a bit too rough to just check where their
bounding boxes are (i.e. if they overlap). Should I try to test the colors
on the overlapping regions (so that if
the pixels are not the transparent , “do a collision”)?
– Ville Koskinen
viller.koskinen at iobox.com
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
-------------- next part --------------
A non-text attachment was scrubbed…
Name: CollisionMask.h
Type: application/octet-stream
Size: 822 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020118/05e6b36a/attachment.obj
-------------- next part --------------
A non-text attachment was scrubbed…
Name: CollisionMask.cpp
Type: application/octet-stream
Size: 4546 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020118/05e6b36a/attachment-0001.obj