# Bullets?

## Recommended Posts

ecr0n    120
y0, On the creation of my first windows game i have come up with the problem of creating bullets for two users (just rectangles currently) to shoot at eachother. How could that be done?

##### Share on other sites
__filip    218
If you could clarify your problem it would be easier to help you. For example are your problem drawing the bullets, storing the bullets, moving the bullets etc?

##### Share on other sites
ecr0n    120
Those are the problems. I know how to draw one bullet and move one bullet and erase one bullet because it is just a pixel to begin with. The problem is that i know HOW to draw an array of bullets and move and erase them but what happens if you create an array of 100 bullets. and both users shoot 100 times... how would you start the initialising of the bullets back to 0 again. understand?

##### Share on other sites
s_p_oneil    443
Don't use a fixed-size array of bullets. Use either a linked list or an array that automatically grows when you need more. If you're using C++, the STL list<> and vector<> template classes can do this for you.

##### Share on other sites
ecr0n    120
do you know a site or something that i could get more info on non fixed lists? I've never had to deal with them before...

##### Share on other sites
s_p_oneil    443
Sorry, I should've explained a bit more.

1) If you use a dynamically allocated list, then you can simply remove the bullet from the list and delete it when it goes out of scope. When a new bullet is fired, it is allocated dynamically and added to the list.

2) If you use an array that automatically grows, then you will end up reusing bullets that have already been allocated. In this case, each bullet can have a flag to indicate whether it is currently in play or not. Then you can loop through them to render the bullets that are in play. When a new bullet is fired, you can also loop through them to find one to reuse when a new bullet is fired.

It can be slow to loop through the array to find unused indices when new bullets are fired. It may be better to keep a stack of "unused" elements in the array. When a bullet is fired, you pop an array index off the stack. When a bullet goes out of play, you push its index back onto the stack. If the stack is empty when you try to pop, then you need to grow the array (and the stack if it is a fixed size).

##### Share on other sites
whaleyboy    122
What are you using to program your game? C++/DirectX I'm assuming, in which case I would definetely use STL vectors as s_p_oneil suggested. I'm currently making a shooter but when I started I didn't know anything about STL but I'm glad I decided to learn about it because it is very powerful and actually quite easy to use. Basically what I did was set up a class for my bullets which stored xy position, xy velocity, etc and then each time a bullet is needed I 'push' a new one onto the end of the vector. You can then create an iterator that cycles through all the bullets currently created and moves them and checks for collisions, etc. My bullet class also has a bool variable called 'alive' which, if set to false with tell the iterator to clear the bullet from the vector. If you don't know what an iterator is or anything about vectors check out some STL tutorials on the net (don't bother reading an entire book on STL as it would probably be an overkill at this stage)

Hope this helps a bit...:)

##### Share on other sites
ecr0n    120
thx alot! :). i get the idea of what you guys are saying, of course i don't understand some of it it because i don't know STL. i'll check that stuff out. and i am doing it with windows/c++ first, then hopefully i'll switch it to directX. i have a book on it... it's just complicating and i don't have all that much time to really "learn" it and all :(. eventually i will. well thx again i'll check out some STL tutorials.

##### Share on other sites
s_p_oneil    443
This one doesn't look very in-depth, but it's a start:
http://www.cprogramming.com/tutorial/stl/vector.html

Do a web search for more. In your case, you'd want something like this:

#include <vector>using namespace std;#include "Bullet.h"vector<CBullet> vBullets; // Use it like it's a normal arrayvoid InitBullets(){  vBullets.resize(100);     // Start off with 100 bullets}void RenderBullets(){  // Update positions and render the bullets.  for(int i=0; i<(int)vBullets.size(); i++)  {    if(vBullets[i].IsInPlay())  // Make sure this bullet is in play    {      if(vBullets[i].Update()) // Return false if bullet is no longer in play        vBullets[i].Render();    }  }}void FireBullet(CVector vPosition, CVector vVelocity){  // Get a new bullet  for(int i=0; i<(int)vBullets.size(); i++)  {    if(!vBullets[i].IsInPlay())      break; // i is now equal to the first unused index  }  // If there are no unused indices, increase the size of the list  if(i == vBullets.size())    vBullets.resize(vBullets.size() + 100);  vBullets[i].SetupBullet(vPosition, vVelocity);}

This code is terrible, please don't use it as it is. It's just to show you, in the fewest number of lines possible, one way that you could use the STL vector class to manage your bullets.

##### Share on other sites
mikeman    2942
I believe a linked list is more appropriate. That's what I use for bullets and other things, like particles. I think it's more flexible than a resizable array.

##### Share on other sites
Palidine    1315
Quote:
 Original post by mikemanI believe a linked list is more appropriate. That's what I use for bullets and other things, like particles. I think it's more flexible than a resizable array.

how is a linked list more flexible? std::vector acts almost exactly like a linked list but with the access times of an array. it's not the right solution for everything b/c there are occasional memory hits when the array needs to resize but it's perfect for this. especially since the max number of bullets alive at any given time is a finite and relatively predictable number.

-me

##### Share on other sites
mikeman    2942
Quote:
Original post by Palidine
Quote:
 Original post by mikemanI believe a linked list is more appropriate. That's what I use for bullets and other things, like particles. I think it's more flexible than a resizable array.

how is a linked list more flexible? std::vector acts almost exactly like a linked list but with the access times of an array. it's not the right solution for everything b/c there are occasional memory hits when the array needs to resize but it's perfect for this. especially since the max number of bullets alive at any given time is a finite and relatively predictable number.

-me

I just like the fact that you can add and remove any object at any time, without having to check for unused objects, or resizing the array appropriately. I find it easier to implement it that way.

##### Share on other sites
whaleyboy    122
Quote:
Original post by mikeman
Quote:
Original post by Palidine
Quote:
 Original post by mikemanI believe a linked list is more appropriate. That's what I use for bullets and other things, like particles. I think it's more flexible than a resizable array.

how is a linked list more flexible? std::vector acts almost exactly like a linked list but with the access times of an array. it's not the right solution for everything b/c there are occasional memory hits when the array needs to resize but it's perfect for this. especially since the max number of bullets alive at any given time is a finite and relatively predictable number.

-me

I just like the fact that you can add and remove any object at any time, without having to check for unused objects, or resizing the array appropriately. I find it easier to implement it that way.

With vectors you can add and remove any object at any time as far as I'm aware and the size is handled automatically. I agree that linked lists are probably more flexible as you are setting them up yourself and have control of exactly what they do, but for this situation I think its more complicated than it needs to be. Just my two pents, I mean cents. ;)

##### Share on other sites
Ravuya    135
I use linked lists for just about everything. I suppose you could use an STL vector as well, but I don't see very much of a point in it.