Friday, October 13, 2006

XeO3: Background Collisions!


Well, I decided tonight was the night. I was going to do the background collision detection. I've been thinking about it for a while and I'm finally happy with the method I've chosen. The problem you see is that I dont have anywhere near the CPU time left to do pixel-perfect collision. This is in fact, like drawing another sprite; and even if I had the CPU time to do that, I'd still rather draw another sprite!

So we're stuck with detecting characters under the player. This is nice in that its very quick, but bad because its terribly crude! If you look at the image shown, number 1 shows the sprite sitting in its 3x3 character block. Now although it's only a 16x16 sprite (2x2 characters), the sprite has to scroll through these to move around - such is the lot of a software sprite. Now, looking at image 2, you can see that if the sprite has move say 4 pixels along, and 4 down we are now sitting right back smack in the middle of a 3x3. So the question becomes - what do you hit?

If we were to simple use ANY chars from the 3x3, then you could be a whole character away, and still get hit. This isn't good. But, if you use a 2x2, then by the time the sprite has scrolled 8 pixels along, and 8 down, then we again are hitting the block above this time, but we aren't actually anywhere near it! It would also mean we could go completely into the block below, and not get hit! Neither of these is desirable.

The solution? Well, looking at image 3 - we'll only deal with Y movement for now. if we move 3 pixels down, what we'll actually do is use the TOP 2x2 (as shown in image 4), then if we move past that and go 4 or more down, we'll switch and start using the lower characters (as shown in the final image). We then apply the same theory to the X coordinate - simple!

This actually works out great! It gives us a natural 4 pixel boundary around the ship where we can "edge" into a block without getting hit, and it keeps us from hitting things miles away! All in all, a pretty nice solution.

So, the big question! What does this mean for the game? Well, the game's a foot...and pretty smelly too! You now have to avoid being hit by aliens, the turrets now shoot at you - and hit! - and you now have to move around the scenery! All we really need to do now, is add all the baddies in Luca's been working on, and do the weapon systems!

4 comments:

Anonymous said...

This entry is really well illustrated :) I wondered if pixel perfect collision detection is possible with all that soft sprite magic eating CPU time.

How much faster is this method compared to the pixel perfect one?

I guess the four pixel boundary even adds "realism" (yeah... as if we had space ships ;) ), just like a car hitting curbs etc without exploding :)

Later,
Chicken

Mike said...

This method is almost free and probably only takes a couple of scanlines. "pixel-perfect" collision would take almost as long as a sprite would take to draw.

Not only that, but I would have made the player mask a bit smaller than the player shit anyway so that it wasn't too brutal. This method gives almost identical results.

Anonymous said...

Ok, this time I had to laugh because the "t" is not anywhere near the "p", so I really wonder where that came from ;)

Cheers,
Chicken

Mike said...

hahahaha...oh yeah...didn't spot that one :)