Projectiles

Projectiles Guide

Projectiles in Game Programming

A projectile is a game object thrown or shot from another game object. For example, a turret shoots a bullet, an airplane shoots missiles, vampire hunters throw axes, and elves throw boomerangs (depending on the elf, of course.) There are several types of projectiles and each of them are programmed in a slightly different way.

Issues

The issues to be solved with almost all projectiles are :

  1. The projectile must be created at a point near the object "firing" the projectile.
    1. The projectile must be created in the direction the object firing the projectile is facing.
      1. This is usually in 2 directions (for platform games), 4 or 8 directions, or 360 degrees.
      2. Also, the projectile may be fired at the mouse, known as "mouse aiming".
  2. The projectile may be subject to a timer, as there are different kinds of bullet streams and other limits of projectiles.
  3. The projectile must be destroyed after it exits the screen or so the thousands of left over projectiles do not slow down your game.
  4. Projetiles may be subject to ammo considerations.

Ammo Models

There are two primary kinds of Ammo models :

  1. Where only the ammo itself is tracked. When the player runs out of ammo, he is simply out of ammo.
  2. Where a total amount of ammo is tracked — in units of bullets and clips. The player first runs out of ammo in the clip before running out of total ammo and therefore must reload.

Reloading

Reloading can be instant or it can happen over time to simulate putting bullets in a gun one by one.

Ricochet

Projectiles can ricochet off of objects. The ricochet can be harmless as if the bullet loses all its momentum and bounces a small distance away from the object it struck, or it can ricochet with slightly reduced force.

Charged Shot

A charged shot is where the player holds down the fire button to "charge" the shot. This causes it to do more damage and the charged shot is usually represented by another sprite than the regular projectile.

Bullet

A bullet is anything that travels in a straight line after being fired. They are the easiest of projectiles to program. Simple bullets, energy blasts, arrows and unguided missiles are all bullets.

Bullet Direction

bullet direction hspeed vspeed
up 0 -1
down 0 1
left -1 0
right 1 0
up / left -0.707 -0.707
up / right 0.707 -0.707
down / left -0.707 0.707
down / right 0.707 0.707

After a bullet has been created, you can set its direction by simply setting it's hspeed, vspeed, or both, depending on the direction you want to travel. Here is a table of the variable values for each direction, assuming that the speed you want the bullet to travel is 1 pixel per game loop :

Notice how the diagonal directions have values of 0.707. This is because when traveling in a diagonal direction the bullet moves a bit slower in the horizontal and vertical direction. However these two values will sum up to a bullet speed of 1 pixel per loopin in the diagonal direction. See Pythagorean theorem for more information.

Bullet Aiming

  • Dumb bullet - always is fired in the same direction from the shooting object.
  • Aimed bullet - is fired depending on the location of another point or object, for example, the location of the mouse.

Notes

  • Bullets fired by the player should always be much faster than those fired by enemies.

Bullet Patterns

Bullet Stream

A bullet stream may be achieved with the code below, however, you may want to slow the stream down a bit with a shot_timer. At this rate, the object will shoot bullets at one per step. If your game is running at 60 steps per second, that is 60 bullets per second.

if (keyboard_check(vk_space))
{
    instance_create(x,y,obj_bullet);
}

Short Burst

A short burst allows the player to fire a rapid fire burst of bullets. The player can keep the burst going by repeatedly pressing the fire button.

// remember to set the burst_timer to 60 and shot_timer to 5 in the Create event.
if(keyboard_check(vk_space))
{
    if (burst_timer > 0)
    {
        burst_timer -= 1;
        if(shot_timer <= 0)
        {
            instance_create(x,y,obj_bullet);
            shot_timer = 5; 
        }
        else
        {
        shot_timer -= 1;
        }
    }
}
 
if(keyboard_check_released(vk_space))
{
    burst_timer = 60;
}

Boomerang

A boomerang is a projectile that moves for in one direction after it is thrown and then returns the way it came. If the boomerang reaches the thrower, it disappears as if the thrower caught it. Otherwise, it continues in that direction until it reaches its range and the reverses direction again.

Boomerangs come in two general varieties : Normal and Magical.

Normal Boomerangs

A normal boomerang acts like you would expect a normal boomerang to act. The maximum range for a normal boomerang is measured as a distance from the point it was thrown. If the thrower fails to "catch it" on it's return trip, it continues on in the reverse direction and does not return.

For the most part, the 2D Castlevania series uses normal boomerangs.

How It Works

When the thrower throws a boomerang, store the location of where the boomerang appears.

boomerang.startX = boomerang.x

Then check to see if the boomerang has reached or exceeded the maximum distance every loopin. If it has, reverse it's direction.

if (boomerang.startX + maxDist > boomerang.x)
{
    boomberang.hSpeed * -1;
}

Magical Boomerangs

The maximum distance for a magical boomerang is measured as a maximum distance from the thrower. If the thrower moves forwards after throwing the boomerang, the maximum distance will seem to increase. Naturally, this is not infinite, as the boomerang always travels faster than the thrower and thus will soon hit its maximum distance.

If the thrower does not catch the boomerang, the boomerang will travel it's maximum distance again and return. It will keep doing this until it is either caught by the thrower, is destroyed by a collision with another game object, or a finite timer runs out.

The boomerang from the Legend of Zelda and the large throwing star from the NES version of Ninja Gaiden are good examples of magical boomerangs.

Axe

An axe is a projectile that travels in an arc due to the force of gravity. Surprisingly, this requires no advanced math. All you have to do is set the vSpeed and hSpeed of the axe at an upwards and forwards angle and let gravity do it's work.

Setting the hSpeed is set to 5 and vSpeed is set to -5, normally would send the axe on an upward diagonal path, but since gravity is acting on the vSpeed, the axe travels in an arc instead.

Bomb

A bomb is a projectile that is thrown like an axe (but usually with less force), but remains on screen when it hits a platform. A timer is set at the moment of it's creation, and after a specified time has elapsed, the bomb explodes.

Homing Missile

A homing missile locks onto a target (usually the nearest) and peruses that target until it expires (and explodes), it goes off screen, hits something else or hits the target.

Laser

A laser can be represented by sprites or by drawing a line. When represented as a sprite, it is actually a set of sprites that crumple at the impact point (segments are destroyed at the front end first). If a laser is represented by a drawn line, the direction and length of the line is used to "crumple" the laser.

Projectile Trails

  • write me!

To Do

  • Depth Charge
Categories: Game Programming : Action Games
page_revision: 3, last_edited: 1267957265|%e %b %Y, %H:%M %Z (%O ago)