✚ Game development

Raycasts and projectiles: instant hits versus physical bullets

Instant raycast hits versus simulated projectiles: the DDA grid algorithm, continuous collisions, projectile gravity and the design choices behind them.

When a character fires, the game has to determine what gets hit. There are two broad approaches: treating the shot as instant, tracing a line from the gun barrel, or creating a projectile that flies through the world for a certain time. The choice has technical consequences and, above all, consequences for how the game plays.

The raycast

A raycast answers the question: starting from this point, in this direction, what is the first object I meet and how far away is it? Beyond shooting, it is a tool used everywhere:

  • checking line of sight between an enemy and the player;
  • checking whether the character is on the ground by casting a short ray downwards;
  • placing the crosshair or a marker on the surface being aimed at;
  • computing sound occlusion;
  • selecting objects with the mouse in a 3D scene.

Ray versus simple shapes

For shapes such as circles, spheres and rectangles there are direct formulas. Against an AABB the slab method is often used: for each axis you compute the range of the parameter t in which the ray lies between the rectangle's two planes, then intersect the ranges.

t_enter = max(tx_min, ty_min)
t_exit  = min(tx_max, ty_max)
hit = t_enter <= t_exit and t_exit >= 0

The value t_enter gives the distance along the ray to the point of impact.

Ray on a grid: the DDA algorithm

On a tile map it is not worth testing the ray against every block. The DDA (digital differential analyzer) algorithm, in the form made famous by the work of Amanatides and Woo, walks exactly the cells the ray passes through, one after another:

  1. compute how far you must travel along the ray to cross one cell horizontally and one vertically;
  2. keep track of the distance to the next vertical edge and the next horizontal edge;
  3. at each step advance to the nearer edge, moving into the adjacent cell;
  4. stop at the first solid cell or at the maximum distance.

It is the same principle used by the pseudo-3D engines of the 1990s to draw walls, and it is extremely efficient: the cost depends on the number of cells crossed, not on the size of the map.

Instant hits (hitscan)

In the hitscan approach, pulling the trigger casts a ray and any target is hit in that same instant. It suits very fast weapons such as rifles and lasers.

Advantages:

  • simplicity and minimal cost;
  • immediate, very rewarding feedback;
  • no problem with projectiles passing through thin targets.

Disadvantages:

  • no chance of dodging after the shot;
  • less visual spectacle, usually compensated with trails and flashes;
  • over long distances the instantaneousness can feel unconvincing.

Simulated projectiles

A projectile, by contrast, is an entity with position and velocity, updated every step like any other object. It can be affected by gravity, wind or magnetic forces, it can bounce, and it can be seen coming.

projectile.velocity.y += gravity * dt
projectile.position += projectile.velocity * dt

It is the natural choice for arrows, grenades, fireballs, rockets, and for games in which dodging bullets is essential, such as scrolling shooters.

The fast-projectile problem

A projectile that travels further than its target's width in one step can pass it without ever overlapping: the phenomenon known as tunnelling. The solutions, in order of complexity:

  • cap the speed or increase the physics update rate;
  • raycast along the path: at each step cast a ray from the previous position to the new one, and if it hits something, register the impact at that point;
  • continuous collision detection, offered by physics engines, which computes the exact moment of contact between two moving shapes.

Raycasting between the old and new positions combines the best of both worlds: the projectile exists, flies and is visible, but cannot pass through targets.

A table to help decide

Characteristic Hitscan Simulated projectile
Computational cost Minimal Higher, grows with the number of projectiles
Can be dodged No Yes
Gravity and wind effects No Yes
Skill required of the player Instant aim Leading the target
Suited to Rifles, lasers, fast weapons Arrows, grenades, rockets, fireballs

Many games use both: hitscan for fast weapons and projectiles for slow ones, choosing according to the desired feel rather than realism.

Details that make a difference

  • The visual trail of a hitscan shot should be drawn from the gun barrel, while the ray often starts from the centre of the camera: otherwise the shot would not go where the player is aiming.
  • Projectiles should be destroyed after a maximum lifetime or outside the level bounds, and managed with a pool.
  • Collision layers prevent a projectile from hitting whoever fired it.

In short

  • A raycast finds the first object along a direction and is useful far beyond shooting.
  • The slab method tests rectangles, while the DDA algorithm walks grids efficiently.
  • Hitscan is simple and immediate; simulated projectiles allow gravity and dodging.
  • Tunnelling of fast projectiles is avoided with raycasts along the path or continuous collisions.
  • The choice between the two approaches depends mainly on the desired game feel.
MM
Marco Marzaduri

Marco Marzaduri works in cybersecurity, digital forensics and web development. A computer science graduate, he combines rigorous technical analysis with attention to the people who use technology.

Read also