Hitscan
In the previous era, many games for rendering 3D scenes into 2D images used a technique called raycasting . Raycasting allows the engine to determine the first object that the beam intersects with. But then the developers asked the question: “What if you let a beam out of the barrel of the weapon to simulate a bullet?” Thanks to this idea, a hitcan was born (“hit scanning”).
Raycasting example
In most implementations of weapons with hitscan, when a player fires, the physical engine performs the following operations:
- Defines the direction in which the weapon is pointing.
- Releases a beam from a barrel of a weapon at a predetermined distance.
- Uses raycasting to determine if a ray has hit an object.
If the engine determines that the object is on the line of fire, then he will inform him of this, saying that a bullet “hit” him. Then the target can perform all the calculations necessary to record the damage.
From Unity . Point A denotes a weapon emitting a beam to the maximum point B. The beam collides with a cube to which the engine reports that it was hit.
Hitscan is inherently simple, but many different modifications can be made to add other logic:
- If we continue the beam beyond the first object it hits, we can pierce several objects on the line, like the railgun in Quake
- If we remove the maximum range from the beam, then we get a laser that will fly forever until it hits something
- You can programmatically make some surfaces reflective so that bullets bounce off them
Overwatch. Genji character deflect ability is an example of a reflective surface.
The main advantage of raycasting is its tremendous processing speed. It is quickly calculated and does not require additional memory or processor time to create a new physical object. This means that the network code needed to synchronize multiple clients will be minimal, because the server only needs to track the direction of the beam. Recoil is also implemented simply, to simulate this effect, it is enough to add a slight deviation in the sight of the weapon.
Therefore, it is not surprising that hitscan is used in the shooting logic of many games. Classic examples are Wolfenstein 3D and Doom , but this technology is also used in modern games. Characters like Soldier 76, McCree, and the Fateful Widow of Overwatch use weapons with hitscan, and most weapons in Call of Duty are also based on hitscan.
Overwatch, Call of Duty, Wolfenstein 3D
So why is this approach not used in all games?
Firstly, as you probably noticed, the rays have an infinitely high speed of movement, that is, they instantly reach the end point. There is no time for a bullet to move between a shot by a bullet and hitting an object. This means that if a ray hits an object, it is impossible to avoid a bullet, even if the target is several kilometers from the player.
Halo. Note that the muzzle flash and the effects of hitting the ground occur simultaneously.
Secondly, most hitscan implementations use direct beams. This means that it is difficult to take into account wind, gravity and other external factors that can affect the bullet after flying out of the trunk. Programmers can add all sorts of tricks so that the beam imitates real bullets, but as soon as the player "shot" the beam, there is no way to change its path in the middle.
In many casual games, hitscan is still used because it simplifies the learning curve for most new players. But what about games seeking to convey the "real" sensation of shooting? With such restrictions, this is impossible to achieve, so a different method is needed.
Ballistics of a Flying Object
This term may seem complicated, but at an abstract level, the idea is quite simple. Each bullet or projectile fired from a weapon creates a new physical object in the scene. It has its own mass, speed and bounding box of the contact (hitbox), which are tracked by the game engine.
Max Payne 3
The benefits of ballistics are fully evident in games in which realism has the highest priority. Since each flying object exists on its own, we can take into account the influence of wind, friction, gravity, temperature - any force that should act on the bullet. Now that we are able to change physics, players can use more diverse weapons, not just simple pistols and lasers; we can add grenades and rockets to our arsenal.
Since bullets in such a system do not move at the speed of light, it is also possible to realize temporal properties:
- Bullet Time used by Max Payne , Sniper Elite and Superhot .
- Bullet travel time, that is, when firing at long distances (or when firing a slowly moving projectile), preemption becomes critical.
- Delayed shell explosions, such as grenades
Due to these additional calculations, processing becomes more expensive than using hitscan. To ensure synchronization, servers have to do a lot more work, it is necessary to eliminate discrepancies or conflicts in the logic on the client side, so that all players within the same server are in the same conditions.
Superhot, Battlefield 1, Overwatch
There are many workarounds to maximize productivity. For example, the engine can store a pool of objects downloaded before the start of the game and “turn on” them as needed. After hitting the surface, you can play the animation of the ballistics and turn off the bullet, saving it for the future. This method allows you to save computing resources and memory occupied by the multiple creation and destruction of objects.
There are also many ways to perform calculations, but at a high level, the difference lies in where they decide to process the "tact" of the game - a unit of time:
- The clock is calculated separately from the rendering logic, that is, the game will have more accurate reproduction of objects even when frames are skipped. More logic is needed to calculate the exact time elapsed since the previous rendering.
- Calculation of a measure in each frame; binding physics to frame rate. If you disable the restriction on the maximum frame rate or if the game starts skipping frames, then the physics of the world will accelerate or slow down.
The effects of linking movement to measures are clearly visible when the shells move fast enough to travel a fairly large distance between measures. Situations may arise when objects “pass” through each other, because they never intersected in the engine.
This all seems complicated, so many people think this is a relatively new method; however, in fact, it arose earlier than hitscan! Prior to FPS games, there were many top-down shooters like Asteroids , Space Invaders or Galaxian . These are arcade games of the 70s, in which ballistics of shells have already been implemented, although quite primitively.
Asteroids. Shells are pretty hard to see, but they are!
But even with all these features, we cannot recreate a realistic model of the real world. Is there any way to take advantage of both methods?
Hybrid systems
Most game engines are capable of handling both types of bullet simulations: hitscan and ballistics. This allows you to realize a huge selection of weapons; games like Halo , GTA, and Half-Life have weapons that can support both types of physics.
Halo. Assault Rifle uses hitscan; Needler uses ballistic shells
Developers can also mix both techniques to cover the weaknesses of each system and provide more realistic behavior. For example, to eliminate the problem of passing objects through each other, each bullet in each cycle of the engine can emit a beam. This allows the engine to see if any of the rays intersect between measures, colliding in the air.
They can also be combined to improve the features of the game. A great example of this is the Sniper Elite series; after pulling the trigger, the engine uses the hitscan to determine if the shot is made close enough to any detectable object to enable slow motion. If so, then a bullet is fired with ballistics in the “bullet-time” mode.
Sniper elite
So, we examined the basics of the behavior of bullets in video games! Interestingly, the improvements in this area are mainly in small optimizations and improvements, and not in large-scale processing. After the release of the first few revolutionary games, we did not make any significant steps or breakthroughs.
What next? How will this area develop in the future?
I do not think that in the near future the hybrid approach will disappear, because it provides additional benefits. But I predict that there will be many improvements on the part of the ballistics of the shells. The clock calculation frequency continues to increase (after all, the CPU power is growing), and we can approach the asymptotic limit of the simulation of the “real world” bullet.