i'm going ahead with my little game "Space Tiger".

Now this is my trouble:

I have 5 kind of monsters that come out random and proceed from right to left of the screen. On the left there is mt starship that have to shoot'em :)
I allow a maximum of 20 monsters of every kind. (totals 20*5 could be out, normally are out less than 50)
I could have till 10 shots out, shooted from my starship.

**** How is the better way to check at every gamecycle if some monster is shooted ? ***

I tought to compare every monster X and Y coordinate with every shot X and Y coordinate, but i think that mean that at every cycle i have to compare 20*5*10 times. It will come too slow i think. And i think that game programmers do it in a better way :alright: .

Sorry for the worst english. Hope you'll understand.
If some help... THX A
Posted on 2001-10-16 12:55:13 by Bit7
No need to appologise for your English Angelo, the fact that you make the effort to post in English for the benifit of those on the board who only speak English like myself is commendable and I'm everyone else appreciates it as much as I do. :alright:

As for your problem. The method I'd use, indeed the method I do use, is the one you suggested. You say there could be 20*5*10, well even assuming 40*10*20 your only talking about 8000 checks. Ok It sound like alot but , highly optimised loops are where assembly kicks the ass of others languages. :grin:

One way you could cut down on branching inside the loop would be to use the xor technique to combine test. I.e. (MonsterLeft - ShotX) XOR (MonsterRight - ShotX). Then the sign flag will be set if MonsterLeft < ShotX < MonsterRight. A similar method can be used for vertical. You can even combine the Horizontal and Vertical test with an OR. Again the sign flag will be set if the shot is inside the Monster's Rectagle.

You could also do a circular Hit Area by testing the distace of the Shot from the ceneter of each monster.
Dis = Sqrt( (MonsterCenX - ShotX)^2 + MonsterCenY - ShotY)^2 )Store the radi of the monsters as the square rather than the actual vale, i.e. R^2 as opposed to R. Then you would need to get the Square Root. I.e compare (MonsterCenX - ShotX)^2 + MonsterCenY - ShotY)^2 to R^2

As for the most optimal coding of those techniques it depends on how you're storing the coordinate (float or integer). Note that the XOR technique only works for Ints. You can use a multiplication trick to achieve similar results if your dealing in floats. :)
Posted on 2001-10-16 14:42:13 by Eóin
If you have very many comparisons then you might be able to partition the area into sub-areas to reduce the the number of comparisons. This method usually means changing code somewhere else to produce and maintain the partitions, but if the comparisons are costly you can gain some speed this way.
Posted on 2001-10-16 17:41:45 by bitRAKE
Afternoon, Angelo.

I guess it depends a lot on what you're using for the graphics.

If you're you using bitmaps, then Eoins' 'circular hit area' idea would be better (I used a similar technique in a 'tank attack' proggy a few years ago - and it worked *very* well).

If you're using 3D graphic objects ( even though the playing-field is 2D) then you can still use Eions' idea.

If you want to have the *shots* hit individual *Faces*, then you could use the *pick* algorithm and modify it so that is uses a ray from the *shot* to the monsters *mesh*.

However, there'd be a lot of calculations involved, so you may wish to use Bitrakes' idea, and offset the processing of *hits* into time-slices. This could mean that in one frame you process the *hits* for shot#1, the next frame you process the *hits* for shot#2, etc. With a maximum of only ten shots on the screen at a time, this would mean that all *hits* would be calculated over ten frames.

Another idea would be to use *D3DXComputeBoundingSphere* on both the monsters and the shots - and use the *pCenter* and *pRadius* info to see if a *shot* hits a monster (however I haven't used that one, yet ;) ) .

I could blab on for ages, however I still don't know what type of graphics you're using.

Cheers,
Scronty
Posted on 2001-10-17 02:13:42 by Scronty
Scronty,

i'm using bitmaps, for a simple game "all windows platform compatible " - no DX used.


Couse to my delay on reading this message (they stuck me @ job.. :() For now, i've already coded all as the Eoine rectangle method.

I will think very well about all your suggestions.
As first step, if i have a gain in speed, i will change all in circle method :)

Thanks all
Posted on 2001-10-18 17:09:44 by Bit7