How Bullets Work in Video Games
Video gaming is basically a giant bag of tricks designed to simulate the real-life environments around us. As such, developers have had to create methods of simulating ideas like weapons fire and hit effects. There are two main methods for doing this — hitscanning and projectile ballistics.
Over at Gamasutra, Tristan Jung has taken a look at the two methods, comparing and contrasting their implementations and why developers use them. Hitscanning is based on raycasting, which fires a beam from the muzzle of the gun and measures if it strikes anything. If the engine determines that an object was struck, it reports a bullet impact.
This trick has been used in all sorts of ways. Want the ray to continue straight through the target you hit? Congratulations, you just invented Quake II’s railgun. Allow rays to bounce off walls, and you’ve created reflective shots or even the concept of a shrapnel hit (you can always have a ‘bullet’ do less damage if it has recorded a bounce first).
One way to tell if a game is using hitscan or not is to check the latency between pulling the trigger and hitting a target. Hitscan weapons hit instantly. They don’t tend to model factors like bullet drop or wind velocity. Developers can simulate these effects by using curved rays, but the ray won’t change direction once ‘fired.’
The other method of calculating bullet trajectories is to actually have projectile ballistics. In this system, bullets have mass, velocity, and a hitbox. This allows for a much more realistic modeling of real-world effects like gravity, wind, and friction. Games like Max Payne use this method, it’s what allows for the game’s ‘bullet time.’ While hitscanning is the technique used for games like Wolf3D, projectile ballistics is actually the older method of simulating an object. If you think about how the shotgun and chaingun work in Doom, you can tell they use hitscanning (with some pseudo auto-aim in some cases when firing at a target higher or lower than you).
With hitscan weapons, visible bullets or tracers may well be ‘ghosts,’ and where they impact on the player model may not actually correspond to where the hit was registered. Some game engines use hybrid effects, where projectile ballistics are used to calculate the visual path, but hit-checks are performed with hitscan.
This is why I described video gaming as a bag of tricks earlier in this story. We start off with two simple concepts — one for projectile ballistics and one for hitscans. Once you start unpacking the way these systems are actually implemented, you find an entirely new set of tricks for implementing effects like shrapnel, overpenetration, wind, and gravity. If a game simulates the impact of wind and gravity on projectile ballistics, it means there’s another series of methods for approximating those effects.
In some cases, diving down to this level of detail in a game engine means you’ve essentially arrived at a nuance that’s perfectly valid to explore for its own sake, but that most people don’t really care about. That’s not true when it comes to weapon ballistics. How guns handle in-game is part and parcel of the overall experience, and the method for checking hits can matter a great deal.
Jung goes into more detail on how projectile ballistics is implemented in engines, so check the article for the full details there. Some games implement both methods, which is a feature I’ve always liked. Hitscan tends to work very well for laser or various sci-fi weapons with near-instant hits. Projectile ballistics works well for objects that would take a slower amount of time to reach the target — and allows for the effective modeling of things like overpenetration or shrapnel from a frag grenade.