Mail Archives: djgpp/1997/05/05/07:23:34
> I've slowly been working my way through learning C and DJGPP, and I was
> wondering if it's possible to use a Case statement to check for a range of
> numbers - not just one....
>
> eg..
>
> Switch (bullet_x)
>
> Case (alien1_x to alien1_x + 16)
> then alien_hit = 1
> Case (alien2_x to alien2_x + 16)
> then alien_hit = 2
> Default
> alien_hit = 0
>
> I guess I'm trying to do a 'bounding box' (?) check to see if a player
> bullet has hit an alien sprite anywhere along it's width. I'm doing a
> space invaders game where the players bullet just goes straight up the
> screen.
Ok, doing cases of ranges with DJGPP is actually possible. As someone
on the list mentioned some time ago, GCC has some sort of "extended C"
which has a provision for switch syntax like this:
switch (somenum)
{
case 1 ... 10: blah(); break;
case 20 ... 30: moo(); break;
default: alien_hit = 0;
}
Note that you must have spaces on the sides of the ellipses (...).
However, I think for collision detection this is the wrong approach
(can you even use variables after the case keyword? hmm). The usual
approach (?) is to subtract the values of the coordinates, take the
absolute value, and then check to see if it's under the range value.
For example, here is a simplified bit of code from the game I'm working
on:
if ((abs(proj[i].x - player.x) < 12)) // 12 is the range
if ((abs(proj[i].y - player.y) < 12))
// collision has occured, handle it
You may need to add in offsets to get the collision detection
working more exactly, if your player and projectile coordinates
don't represent the -centers- of their respective objects. For
example, my coordinates represent the upper-left of the player
and projectiles, so I have to do things like:
if ((abs((proj[i].x + 4) - (player.x + 16)) < 12))
if ((abs((proj[i].y + 4) - (player.y + 16)) < 12))
// collision has occured, handle it
This effectively corrects the coordinates so that they
represent the centers of the objects, giving more accurate
collision detection.
Hope this helps,
Tom Grandgent
tgrand AT canvaslink DOT com
Canvas Link, Inc.
- Raw text -