Sign in to follow this  
LAURENT*

Seemed easy, is really hard, How do you program bullets?

Recommended Posts

What kind of bullets? Bullets that move a certain distance per frame? Bullets that instantly hit whatever you point at? Bullets affected by gravity and other external forces? Bullets that travel straight no matter what?

 

A bullet for a 2D shooter. The projectile isn't effected by gravity and moves at somewhat faster than the average speed of the character.

Share this post


Link to post
Share on other sites

OK. The typical process for something like that is (pseudocode):
 


bullet = bulletPool.NewBullet(); // or you could say "new Bullet" if you want a quick-and-dirty implementation.

bullet.orientation = firingObject.Orientation;

bullet.velocity = firingObject.Forward * desiredProjectileSpeed + firingObject.Velocity;
// adding firingObject.Velocity is optional - some games do this, some don't.
If you don't have a Forward vector for objects, but you do have an angle, you can get the forward vector like so:
 
object.Forward.X = cos(orientationAngleInRadians);
object.Forward.Y = sin(orientationAngleInRadians);
Simple bullet update is something like this:
 
position += velocity * deltaTime;

// Check for collisions with other objects (use circle-circle test only for example simplicity)
// Normally you would want to have some kind of broad-phase collision checking as well, but this will work fine for a thousand objects or so.
foreach (object2 in allObjects)
{
    // don't collide with self or the firing object
    if (object2 == bullet || object2 == firingObject)
        continue;

    var offset = object2.position - bullet.position;

    if (offset.lengthSquared <= bullet.radiusSquared + object2.radiusSquared)
    {
        Cleanup(bullet);

        ApplyDamageTo(object2);
    }
}

Should I really be allocating and deallocating memory like that?

Share this post


Link to post
Share on other sites

And then for detecting collision usually a raycast (swept point) or capsule (swept sphere) is used for representing the space that the bullet travels in one game frame.


Randy is correct - for very fast travelling objects, the simple circle-circle test that I've got in my example can allow very fast projectiles to "jump" through other objects. To prevent that, you should check the entire path that the bullet travels each frame to see if it hits anything.

Use a line segment check if your bullet's size is not important for purposes of collision. Use a capsule if it is. Edited by Nypyren

Share this post


Link to post
Share on other sites


Should I really be allocating and deallocating memory like that?

Most systems like this, like particle systems and other rapidly created/destroyed objects, live in a memory pool.

 

An appropriate size pool of objects is allocated and initialized, and then marked as not being active.  When you need one it is marked as active, when you are done with it the item is marked as inactive.

 

The details of an appropriate size depends on your needs.  I've seen high-performance particle systems that have space for several hundred thousand particles. 

Share this post


Link to post
Share on other sites
Are you using any physics engine? Maybe you could consider using one, I know that box2D has special behaviors to deal with bullets.


The only thing I can add to the discussion is that you need destroy the bullets even in case they don't collide with any other object (for instance, the player start shooting into the sky and there is no ceiling in your game). If you don't do this you will have performance issues sooner or later for handling hundreds of bullets that are traveling "to the infinity and beyond" =)

Share this post


Link to post
Share on other sites

 


Should I really be allocating and deallocating memory like that?

Most systems like this, like particle systems and other rapidly created/destroyed objects, live in a memory pool.

 

An appropriate size pool of objects is allocated and initialized, and then marked as not being active.  When you need one it is marked as active, when you are done with it the item is marked as inactive.

 

The details of an appropriate size depends on your needs.  I've seen high-performance particle systems that have space for several hundred thousand particles. 

 

 

Do you have any general info on the object pool technique? I feel like I should know this. 

Edited by LAURENT*

Share this post


Link to post
Share on other sites


Do you have any general info on the object pool technique? I feel like I should know this. 

 

Scroll back up, read Nypyren's messages more carefully. Follow that link. If you need more information, use that for search terms.

Share this post


Link to post
Share on other sites

Heres a simple javascript example of an object pool in case you still need help:

function Bullet(alive) {
    this.alive = alive;
    // ... all your other stuff here
}

// maxSize is optional and is only there to 
// protect you from allocating to many objects
function Pool (maxSize) {
    this.objects = [];
    this.maxSize = maxSize;
}

Pool.prototype.get = function() {
    // first check for any objects that aren't being used
    for(var i = 0; i < this.objects.length; i++) {
        if(this.objects[i].alive === false) {
           objects[i].alive = true;
           return objects[i];
        }
    }

    // only create a new object if the pool allows for it
    if(this.objects.length < this.maxSize) {
        this.objects.push(new Bullet(true));
        return this.objects[this.objects.length - 1];
    }

    // this means there are no more bullets available, either set 
    // maxSize to a higher value or stop requesting so many bullets!
    return null;
}
Edited by xDarkShadowKnightx

Share this post


Link to post
Share on other sites

It's works, I created an array of pointers to handle turning the bullet on/off individually. Then I directly modified the bullet class's variables from my button input class. All I need to do is create conditions to turn the bullets off and my prototype is done. 

 

Excellent...  

Share this post


Link to post
Share on other sites


Do you have any general info on the object pool technique? I feel like I should know this.

 

This is a good pattern of design to use to help in memory management.  Game Programming Patterns by Robert Nystrom goes into several really good patterns that you might also find helpful.

Share this post


Link to post
Share on other sites

Do you have any general info on the object pool technique? I feel like I should know this.

 
This is a good pattern of design to use to help in memory management.  Game Programming Patterns by Robert Nystrom goes into several really good patterns that you might also find helpful.

In general you'll find the word "cache" or "pool" used a lot in game dev, almost of buzzword quality simply to refer to the idea of a design that favors keeping objects in memory vs destroying and recreating them on demand.

You technically could create and destroy each bullet as needed, and it would work, the only real negative to that is that things like bullets usually are an obvious candidate for pooling.

Share this post


Link to post
Share on other sites

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