Advertisement

Tracking multiple bullets

Started by January 01, 2003 07:30 PM
7 comments, last by Dark_Halmut 21 years, 10 months ago
What is the best way to track all the bullets and to see if their hitting something? Make a massive stack as they are created and run through them each movement so see if they've hit a possible target? Is there some faster method used in games like Galaga or Ikaruga? Dark_Halmut http://www.geocities.com/Dark_Halmut/home.html [edited by - Dark_Halmut on January 1, 2003 8:31:58 PM]
I''m really new to game programming and for my first game I just made an array of a fixed size and had a counter that incremented every time I fired. If the array was in use or the bullet was in the fired state than it would try the next spot in the array. Once it got to the number of elements in the array it went back to zero. Then to keep track go through the array and check those bullets which are currently active. If they hit something then they are simply no longer active and you can deal with the code then. Seemed to work for me and I think its fairly fast. Anyone who knows otherwise please feel free to give a better alternative.
Advertisement
Why don''t you make a small structure for the bullets and make an array from that? eg...

struct BULLET
{
int x,y,direction,speed;
BOOL HasHitAnything();
void Create(int x, int y, int direction, int speed);
void move();
void destroy();
}
[\code]
Then have an array of these and update as follows:
<br>BULLET bullets[MAX_BULLETS];<br><br>//...<br>for(int i = 0;i < MAX_BULLETS;i++)<br> {<br> if(bullets<i>.HasHitAnything()) Do Whatever is needed<br> }<br>[\code]<br>Hope that helps. Wait... now that I look at it, it looks kinda messy. </i> </pre> <br><br>The past was unknown, the future was predicted.
the future is just like the past, just later. - TANSTAAFL
Use a linked list. =)
Well ya thats what I was thinking. I would make a class to handle the projectile and it behavior, graphic model, coordinate. Then just make a stack of them off my already done template stack class.
quote: Original post by rm3
Use a linked list. =)


Use an STL Vector Container


  #include <vector>using namespace stdvector<BULLET> Clip;AddBullet(){	BULLET newBullet;	Clip.insert(newBullet);}ProcessAmmo(){	for(int i = 0; i < Clip.size; ++i)	{		if(bullets.HasHitAnything())		{			//Do Whatever		}    	}}  

Advantages: No predefined size (same as a linked list) and better memory management.
Advertisement
errr...

  ProcessAmmo(){		for(int i = 0; i < Clip.size; ++i)	{		if(CLip[i].HasHitAnything())		{			//Do Whatever		}    	}}  
quote:
Is there some faster method used in games like Galaga or Ikaruga?


The older shooters pretty much exclusively used arrays...as an array offers both stable and faster performance over linked lists which must deal with heap allocation/deallocation/fragmentation and an increased risk of cache misses... but then again modern PCs are hundreds of times faster then the processors such games had to work with....so it''s really up to you.

as for collision checking...it really helps to reduce the number of checks needed by reduceing the number of objects that need to be checked (if that makes sense)...here is a very simple way to do that:

** split the screen up into quadrants..upper left, upper right, lower left, and lower right...with the player''s craft in the center...then store an array (or linked list) of indexes (or pointers) for each quadrant...then when you are processing the bullets...check if they are to the left/right, above/below the current player location and add them to the appropriate list...then when processing the enemies check it''s current possision aganst the players and only do collision tests with the bullets listed with that quadrant.




Thanks,

Im using a method similar to that in a game Im wrapping up for the TI-86 graphic Calculator.

Dark_Halmut

[edited by - Dark_Halmut on January 2, 2003 1:30:00 AM]

This topic is closed to new replies.

Advertisement