Jump to content
  • Advertisement
Sign in to follow this  
LAURENT*

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

This topic is 1254 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

I'm having a difficult time getting my game's bullets to work. I think I understand conceptually but my coding solutions may be lacking. Does anyone have experience with bullets in video games? Maybe an example?

Share this post


Link to post
Share on other sites
Advertisement

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!