Sign in to follow this  

What's the best way to create Bullets in C++

This topic is 4198 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok...I'm currently working on a top-down 2D game...and I cannot figure out the best way to create bullets! The last time I've actually used bullets was when I programmed in BASIC..and in BASIC there was a for each next loop that I used for my "types"...which made things much much easier. But in C++...it's almost as if it's impossible at first sight. I've done a couple of things..but they don't really do well in performance (makes my game lag too much). Heres what I've done so far.. I made a class for bullets and I created 20 bullets for each player (including AI) from the start. The bullets are stored hidden until they are shot out of the gun. Once they get off the screen...they would be re-located back to the hidden spot, so they could be used again. So basically, I created reusable bullets...but that quickly resulted into a very laggy game. My game consists of more than 100 people on the screen at once...and with all those people with all those bullets...it isn't pretty..lol. So what can I do? I don't like how I reuse the bullets.. I want to be able to create a bullet when the gun is fired, and then delete it when it hits something or goes off the screen. You would think that there would be tutorials on this because guns and bullets are the most popular things in games...but I can't find a single one! Maybe I'm just trying to hard.. If anybody has some code to share or some advice to give...either would be greatly appreciated. Thankyou so much!

Share this post


Link to post
Share on other sites
Wouldn't you be looking for something like...

Bullet* bullet = new Bullet(x,y,velocity,angle);
//shooting code
delete bullet;

? It seems almost too simple, so this probably isn't what you're looking for... but it was worth a shot just incase. :p

--Aternus

Edit: Just wanted to add, maybe you could have a vector of "live" bullets, and iterate through them to see where to animate next, if they hit anyone, etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by BenDrummin58
Ok...I'm currently working on a top-down 2D game...and I cannot figure out the best way to create bullets! The last time I've actually used bullets was when I programmed in BASIC..and in BASIC there was a for each next loop that I used for my "types"...which made things much much easier. But in C++...it's almost as if it's impossible at first sight. I've done a couple of things..but they don't really do well in performance (makes my game lag too much).

Heres what I've done so far.. I made a class for bullets and I created 20 bullets for each player (including AI) from the start. The bullets are stored hidden until they are shot out of the gun. Once they get off the screen...they would be re-located back to the hidden spot, so they could be used again. So basically, I created reusable bullets...but that quickly resulted into a very laggy game. My game consists of more than 100 people on the screen at once...and with all those people with all those bullets...it isn't pretty..lol.

So what can I do? I don't like how I reuse the bullets.. I want to be able to create a bullet when the gun is fired, and then delete it when it hits something or goes off the screen. You would think that there would be tutorials on this because guns and bullets are the most popular things in games...but I can't find a single one! Maybe I'm just trying to hard..

If anybody has some code to share or some advice to give...either would be greatly appreciated. Thankyou so much!
There's nothing inherently 'wrong' with C++ that would make what you describe laggy or otherwise perform poorly; if you're not getting the results you want, it's probably your implementation that's at fault.

The method you describe above is not at all unreasonable in theory. I wouldn't think 100 entities X 20 bullets each would cause a performance problem unless something is wrong with the implementation.

Your later suggestion ('new-ing' and deleting the bullets on demand) is also reasonable, but (at least in the absence of a custom allocator), this gets into more difficult areas of memory management, such as avoiding fragmentation and the performance hit of dynamic allocation. So I'd hold off on that method until you get your current method working.

Perhaps better than 20 bullets for each player would be a global pool of bullets. It could resize when needed (or simply be capped to a reasonable max value), but you would also keep the 'used' and 'unused' bullets organized (perhaps all 'used' first) for easy updating and getting a new bullet when needed.

Anyway, you might post some code and/or describe what sort of performance problems you're running into. What API are you using? Maybe the lagginess simply has to do with how many objects you're rendering (remember, profiling is usually more useful than casual observation).

Share this post


Link to post
Share on other sites
Firstly your bullets probably share properties with the other entities so you need some hierarchy of objects in your game. You may have something like this:

Entity ( has velocity and position)
class Entity { };

Bullet derived from Entity ( also has owner )
class Bullet : public Entity { };

Enemy dervied from Entity ( also has Type, AI state etc)
class Enemy : public Entity { } ;

Player derived from Entity ( also has health etc)
class Player : public Entity { };

All of the objects will have an update function which takes the elapsed time. The bullet simply travels according to its velocity vector. If it intercepts anything then it hits it.
void Update ( float fElapsedTime ) { };

Now you'll want a list of all entities in your game so how about:
std::vector<Entity *> Ents;

then you can iterate over the container and do a Update call:
for ( std::vector<Entity *>::iterator it = Ents.begin(); it != Ents.end(); ++it) { it->Update(elapsedTime); }

When the player fires you create a new entity and drop it in that list and it'll automatically be updated in the game loop, like your basic for each next loop as you say.

You may want to create a cache and re-use them like you suggested if you think object creation/destruction is going to take a lot of time. Alternatively stick an "alive" member variable on the Entity base class. When the object needs to be remove set alive to be false. Each update loop check for entities that are dead, e.g. the bullet that just hit the enemy, then remove them from the container.


Some basic code to give you an idea, I haven't bothered writing the constructors etc... thrown together in a few secs to give you the idea.


class Entity
{
public:
Entity() {};
~Entity() {};

virtual void Kill();

virtual void Update(float fElaspedTime);

private:

bool alive;
vec2 position;
vec2 velocity;

}

class Bullet : public Entity
{
public:
Bullet() {};
~Bullet() {};

void Update(float fElapsedTime);

}

void Bullet::Update(float fElapsedTime)
{
vec2 newPosition = position + velocity * fElapsedTime;

//Do Collision detection
if ( collides )
{
collisionobject->Kill();
alive = false;
}
}

std::vector<Entity *> Ents;

//Game Loop

while (!finished)
{
//Update Ents
for ( std::vector<Entity *>::iterator it = Ents.begin(); it != Ents.end(); ++it) { it->Update(elapsedTime); }
//Render Graphics
//Process Input
}




Share this post


Link to post
Share on other sites

This topic is 4198 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this