Archive Forums (lite version).  Gaming, Game Development, and Anime community!


PDA

View Full Version : Bullets.


neozf
02-12-2010, 09:25 PM
Lots of games have bullets, scrolling shooters for example, but I've always wondered how they were handled, are they stored in a linked list, then a function just goes though the linked list, moving each of the bullets along and checking for collision, etc?

Google
--

TyranicMoron
02-13-2010, 08:45 AM
That's pretty much how I've done it in the past. A linked list is pretty much the best structure you can use for things like bullets, being able to freely loop, add, and remove with little performance overhead.
For quickness (mainly of coding, lol) I've also used vectors in C++. The equivalent in C# is the List structure. But I don't know what language you're using :P

neozf
02-13-2010, 09:56 AM
I'm using C, since that's what allgero uses. Though I'd rather use a libary that used C++, since I'm more used to it.

Allegro can use C++, it's just I started my project and told it to use the C compiler...
Oh well, managed to make a snake game =D

Walrii
02-14-2010, 08:19 PM
The linked list is a pretty decent data structure for something like bullets.

There are also some data structures that take into account the positions of the objects: http://en.wikipedia.org/wiki/Quadtree

I've never used/studied quadtrees, but I know they exist. They can help speed up collision detection by letting you ignore checking for collisions between bullets that couldn't possibly collide.

ElyCyan
02-14-2010, 10:48 PM
Bullets are usually never handled individually. Most bullets are raytracers. You calculate the trajectory from the gun and make a line from the shooter along the trajectory. If you hit a target, calculate damage; otherwise, apply a bullethole texture to the wall.

TyranicMoron
02-15-2010, 04:22 AM
Bullets are usually never handled individually. Most bullets are raytracers. You calculate the trajectory from the gun and make a line from the shooter along the trajectory. If you hit a target, calculate damage; otherwise, apply a bullethole texture to the wall.

Unless you're making a SHMUP, of course. Then you can never have too many bullets :D

neozf
02-15-2010, 10:22 AM
I seem to be having an odd problem with my bullet manager. If you shoot a single bullet, that bullet will travel as normal till its life runs out (which slowly decreases over time). But then suddenly, the bullet returns to where it was shot from, and repeats itself.

Also, if you shoot a bullet, then move somewhere else and shoot again, a bullet from where you last shot appears. But if there's only the ghost bullet and its life is positive, then the next fired bullet is normal, and if you fire a bullet while the ghost bullet's life is negative, the ghost bullet vanishes.

I uploaded a video showing the bug in more detail.
http://www.youtube.com/watch?v=gkCVBtDc_Ps

void BulletManager::updateBullet()
{
if(head!=NULL)
{
node *temp, *temp2;


temp = head;
do
{
temp->bullet.update();
if(temp->next !=NULL)
{
temp=temp->next;
}
}while(temp->next != NULL);

temp = head;

if(temp->bullet.getLife()<0)
{
if(head->next==NULL)
{
delete head;
temp = new node;
temp = NULL;
head = temp;
}
else
{
/*if(head->next=head)
{
head->bullet.setX(20);
}*/
temp = head->next;
temp2 = temp;
delete head;
head = new node;
head = temp;
}
}
}
}Is the piece of code where the bug is, I think.
I have no idea what's causing it to repeat itself.

Edit: Managed to get it so it works properly the first time, but any time after the first once all the bullets are gone another magically appears.

Update, turns out for some reason it loads the bullets values into the array, then fires all of them, except one which it does at the end, which is why it looks like there's one randomly spawning, now to work out what's causing it..

Problem solved, bloody do while loop, it'd miss out the end of the linked list.

Sim9
02-15-2010, 06:32 PM
Yeah, depends on speed, really. If they're pretty much instantaneously moving, ray-tracing is the way to go. If they're slower (like missiles), then track them via your game's entity tracking system. :)