When I was doing this (which is a *long* time ago), I used to draw all my 2D shapes (sprites) twice: Once for display and once in a collision map. The collision map was a bitmap w/ 8 bits depth. Each sprite had a 1-bit collision map, so I didn't have to worry about transparency and all that. The sprites were numbered from 1 to 255, so I could handle only 255 sprites.
For each bit in the collision bitmap of a sprite, I wrote the sprite-number into the collision map, so I would know which sprite collided with which (or which one was hit by a bullet). You could increase the number of sprites by increasing the bit-depth of the collision map.
After handling all the sprites, I started projecting the bullet's trails (collision trails) through the collision map using Bresenham's algorithm as stated above by fl0yd. I treated bullets as vectors with a length according to their speed. If a bullet moved 10 pixels/frame, it's collision trail would simply have a length of 10 pixels. The collision trails were drawn in xor-mode with a 0-value, so I could detect which sprite was hit.
When you've detected a collision in the collision map, you would render an explosion or whatever instead of the actual sprite in the display map.
As I've stated, this was a *long* time ago, so this doesn't make use of any hardware acceleration features.
Hope that helps,
- michael
Main Topics
Browse All Topics





by: fl0ydPosted on 2003-08-08 at 05:19:31ID: 9107478
I think you are on the right track by check 2 rectangles for intersection. My suggestion would be a 2-staged approach, the first one checking very quickly for a possible collision while the second stage actually performing pixel-perfect detection:
Stage 1:
* calculate a bounding box enclosing the covered area by the moving character -- this will probably be a bit larger than the bounding box of the character itself
* calculate a respective bounding box for the area covered by the projectile
* perform the intersection test
If both bounding boxes overlap, calculate the intersection box and perform
Stage 2:
* move along the projectile trail that's inside the overlapping region (check out Bresenham's line drawing algorithm for a fast way of doing that)
* apply appropriate offsetting of coordinates to account for the moving character, i.e. adjust the character's position to where it was at the time the bullet travelled across a specific pixel)
* check, if the pixel on the trail is inside the character -- you probably use alpha transparency for your character so checking against a non-zero alpha value will get you going.
I don't think that you need to perform collision detection at a higher resolution than you're rendering. In fact, Quake ]|[ only performs collision detection about 10 times a second, even if it's running at 200fps. I hope this information gets you going.
.f