Sick Gaming
News - Blog: How do bullets work in video games? - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Sick Gaming Community (https://www.sickgaming.net/forum-1.html)
+--- Forum: Lounge (https://www.sickgaming.net/forum-8.html)
+--- Thread: News - Blog: How do bullets work in video games? (/thread-92740.html)



News - Blog: How do bullets work in video games? - xSicKxBot - 12-11-2019

Blog: How do bullets work in video games?

<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/12/blog-how-do-bullets-work-in-video-games.gif" width="557" height="200" title="" alt="" /></div><div><p><strong><i><small> The following blog post, unless otherwise noted, was written by a member of Gamasutra’s community.<br />The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company. </small></i></strong> </p>
<hr>
<p>FPS (first-person shooter) games have been a staple in the video game industry ever since the explosion of&nbsp;<em>Wolfenstein 3D</em>&nbsp;back in 1992. Since then, the genre has been evolving with graphical upgrades, huge budgets, and an eSports ecosystem. But what about its core, the shooting mechanics? How have we progressed on that front? Why do some guns feel like it’s the real thing, while others feel like toys?</p>
<p><big><strong>“How do bullets work in video games?”</strong></big></p>
<hr>
<p data-selectable-paragraph id="ad5f">In the earlier days, many games relied on a technique called&nbsp;<a href="https://en.wikipedia.org/wiki/Ray_casting" rel="noopener nofollow noreferrer" target="_blank">raycasting</a>&nbsp;to render 3D environments onto 2D images (your screen). Raycasting also allows the engine to determine the first object intersected by a ray. Developers then started to question, “What if that ray originated from the muzzle of a gun to mimic a bullet?” With this idea, hitscan was born.</p>
<p data-selectable-paragraph><img alt="An example of raycasting" height="200" src="https://www.sickgaming.net/blog/wp-content/uploads/2019/12/blog-how-do-bullets-work-in-video-games.gif" width="557"><br /><em>Above:&nbsp;An example of raycasting</em></p>
<p data-selectable-paragraph id="7fd1">In most implementations of a hitscan weapon, when the player shoots a bullet, the physics engine will:</p>
<ul>
<li data-selectable-paragraph id="9b12">Figure out the direction the gun is pointing at,</li>
<li data-selectable-paragraph id="32eb">Cast a ray from the muzzle of the gun until a defined range,</li>
<li data-selectable-paragraph id="3094">Use raycasting to determine if the ray hit an object.</li>
</ul>
<p data-selectable-paragraph id="7e2d">If the engine determines that an object is in the line of fire, it will notify it with a message that it was “hit” with a bullet. The target then can do all the calculations needed to register the damage.</p>
<p data-selectable-paragraph><img alt height="281" src="https://www.sickgaming.net/blog/wp-content/uploads/2019/12/blog-how-do-bullets-work-in-video-games.png" width="500"></p>
<p data-selectable-paragraph><em>Above:&nbsp;From <strong>Unity</strong>. Point A represents a gun casts a ray until its maximum point B. The ray makes contact with the cube, which the engine will tell it has been hit.</em></p>
<p data-selectable-paragraph>Hitscan is simple at its core, but a lot of different modifications can be made to support other logic:</p>
<ul>
<li data-selectable-paragraph id="9bbd">If we continue the ray past the first object that it hit, we can penetrate multiple objects in a line, like the railgun in&nbsp;<em>Quake</em></li>
<li data-selectable-paragraph id="de4c">Removing the maximum range of the ray means that we can shoot out a laser that will continue forever until we hit something</li>
<li data-selectable-paragraph id="a058">Programming certain surfaces to be reflective, to bounce bullets off of</li>
</ul>
<p data-selectable-paragraph><img alt height="270" src="https://www.sickgaming.net/blog/wp-content/uploads/2019/12/blog-how-do-bullets-work-in-video-games-1.gif" width="480"></p>
<p data-selectable-paragraph><em>Above:&nbsp;<strong>Overwatch</strong>. Genji’s deflect is an example of a reflective surface.</em></p>
<p data-selectable-paragraph id="9419">The main advantage of using raycasting is that it’s super fast. It’s quick to compute and does not need overhead memory or processing time to build a new physics object. That means the network engineering needed to keep many clients in sync is minimal since the server only needs to keep track of the direction of the ray. Recoil is simple to add, as the addition of a small perturbation in the aim of the gun will mimic the effect.</p>
<p data-selectable-paragraph id="a288">Thus, it’s no surprise that many games in the industry use hitscan for its shooting logic.&nbsp;<em>Wolfenstein 3D</em>&nbsp;and&nbsp;<em>Doom&nbsp;</em>are classic examples, but even recent games use this technology. Characters such as Soldier 76, McCree and Widowmaker from&nbsp;<em>Overwatch&nbsp;</em>have hitscan weapons, and most&nbsp;<em>Call of Duty&nbsp;</em>guns are hitscan as well.</p>
<p data-selectable-paragraph><em>Below: Examples from <strong>Overwatch</strong>, <strong>Call of Duty</strong>, <strong>Wolfenstein 3D</strong></em></p>
<p data-selectable-paragraph><img alt src="https://www.sickgaming.net/blog/wp-content/uploads/2019/12/blog-how-do-bullets-work-in-video-games-2.gif"></p>
<p data-selectable-paragraph><img alt src="https://www.sickgaming.net/blog/wp-content/uploads/2019/12/blog-how-do-bullets-work-in-video-games-3.gif"></p>
<p data-selectable-paragraph><img alt src="https://www.sickgaming.net/blog/wp-content/uploads/2019/12/blog-how-do-bullets-work-in-video-games-4.gif"></p>
<p data-selectable-paragraph id="a1ed">So why don’t all games use this method?</p>
<p data-selectable-paragraph id="972d">First, you may have noticed that rays have an infinite traveling velocity, thus reaching their destination instantly. There is no travel time after you fire a bullet and hit an object. This means it’s impossible to dodge a bullet if a ray is on target, even if the target is miles away.</p>
<p data-selectable-paragraph><img alt height="279" src="https://www.sickgaming.net/blog/wp-content/uploads/2019/12/blog-how-do-bullets-work-in-video-games-5.gif" width="500"></p>
<p data-selectable-paragraph><em>Above:&nbsp;<strong>Halo</strong>. Notice how the muzzle flare and the hit effects on the ground show up at the same time.</em></p>
<p data-selectable-paragraph id="61ff">Second, most implementations of hitscan use straight rays. This means it’s hard to account for wind, gravity, and other external factors that may affect the bullet once it leaves the gun. Programmers can add kinks and bends to the ray to help it mimic real rounds, but once the player shoots a ray, there is no real way to modify its path in the middle.</p>
<p data-selectable-paragraph id="7e46">A lot of “casual” games end up using the hitscan method as it simplifies the learning curve for most beginner players. But what about games that aim to create an “immersive and realistic” shooting experience? They cannot achieve their goals within these constraints. We need to use an alternative method.</p>
<hr>
<p data-selectable-paragraph id="120e">It sounds pretty fancy, but the high-level idea is straightforward. Every bullet or projectile shot out of a weapon creates a new physics object in the environment. It has its own mass, velocity, and hitbox that the engine will track.</p>
<p data-selectable-paragraph><img alt height="281" src="https://www.sickgaming.net/blog/wp-content/uploads/2019/12/blog-how-do-bullets-work-in-video-games-6.gif" width="500"></p>
<p data-selectable-paragraph><em>Above: <strong>Max Payne 3</strong></em></p>
<p data-selectable-paragraph id="9362">The advantages of using projectile ballistics shine in games where realism is the top priority. Since every projectile exists on its own, you can now factor in wind, friction, gravity, temperature; any force that should act on the bullet. Now that you can change the physics, players can now use weapons other than simple guns and lasers; you can now add grenade and rockets to your arsenal.</p>
<p data-selectable-paragraph id="dba5">Since bullets under this system aren’t moving at the speed of light, you can also implement temporal features:</p>
<ul>
<li data-selectable-paragraph id="e75f">“Bullet-time” as seen in&nbsp;<em>Max Payne</em>,&nbsp;<em>Sniper Elite</em>&nbsp;or&nbsp;<em>Superhot&nbsp;</em>is feasible.</li>
<li data-selectable-paragraph id="9f74">Travel time for projectiles, which means if you’re taking a long-distance shot (or shooting a slow-moving projectile), aiming ahead becomes crucial.</li>
<li data-selectable-paragraph id="9a28">Delayed explosions on projectiles, like grenades</li>
</ul>
<p data-selectable-paragraph id="a8c4">With these additional computations, the processing is more taxing relative to using hitscan. Servers will have to do a lot more work to make sure all the objects are in sync, and discrepancies or conflicts in logic across clients have to be resolved not to create inconsistent experiences for players on the same server.</p>
<p data-selectable-paragraph><em>Below: Examples from <strong>Superhot</strong>, <strong>Battlefield 1</strong>, <strong>Overwatch</strong></em></p>
<p data-selectable-paragraph><em><strong><img alt src="https://www.sickgaming.net/blog/wp-content/uploads/2019/12/blog-how-do-bullets-work-in-video-games-7.gif" width="500"></strong></em></p>
<p data-selectable-paragraph><em><strong><img alt src="https://www.sickgaming.net/blog/wp-content/uploads/2019/12/blog-how-do-bullets-work-in-video-games-8.gif" width="500"></strong></em></p>
<p data-selectable-paragraph><em><strong><img alt height="281" src="https://www.sickgaming.net/blog/wp-content/uploads/2019/12/blog-how-do-bullets-work-in-video-games-9.gif" width="500"></strong></em></p>
<p data-selectable-paragraph id="cf79">There are many ways around this to squeeze out as much performance as possible. An example of engine optimization is to have a “pool” of objects loaded before playtime, and “warp in and enable” them when needed. Once it hits a surface, you can play a ballistics animation and disable the projectile, saving it for later. This method will reduce some computation and memory costs from creating and destroying objects over and over again.</p>
<p data-selectable-paragraph id="c74a">There are also multiple ways to do the computations, but the high-level difference is where they decide to process a “tick” of a game, a unit of time measurement:</p>
<ul>
<li data-selectable-paragraph id="3f2c">The tick is calculated separately from the rendering logic, which means the game will have a more accurate representation of the objects even if there are frame skips. More logic is needed to calculate the exact time that passed since the last render.</li>
<li data-selectable-paragraph id="22de">Calculating the tick on every frame; binding the physics to the frame rate. If you disable frame rate caps or start to drop frames, you can see the accelerated or choppy effects on the world.</li>
</ul>
<p data-selectable-paragraph id="310d">The consequence of tying movement to ticks is clear when projectiles are moving fast enough to cover large amounts of distance between ticks. You may run into situations where objects seem to “phase through” each other since they were never overlapping in the engine.</p>
<p data-selectable-paragraph id="f388">All of this sounds fancy, thus leading many people to think that this is a relatively new method; but it actually predates hitscan! Before FPS games, there were already many top-down shooters, such as&nbsp;<em>Asteroids</em>,&nbsp;<em>Space Invaders</em>&nbsp;or&nbsp;<em>Galaxian</em>. These are arcade games from the 70s that were already implementing projectile ballistics, albeit a bit primitive.</p>
<p data-selectable-paragraph><img alt src="https://www.sickgaming.net/blog/wp-content/uploads/2019/12/blog-how-do-bullets-work-in-video-games-10.gif" width="500"></p>
<p data-selectable-paragraph><em>Above:&nbsp;<strong>Asteroids</strong>. The bullets are a bit hard to see, but they are there!</em></p>
<p data-selectable-paragraph>Even with all these features, we’re not able to create a realistic representation of the real world. Is there a way we can get the advantages of both methods?</p>
<hr>
<p data-selectable-paragraph id="1e49">Yes, we can!</p>
<p data-selectable-paragraph id="5d61">Most game engines can handle both types of bullet simulations: hitscan and projectile ballistics. This gives the option to have a huge variety of weapons; games such as&nbsp;<em>Halo</em>,&nbsp;<em>GTA</em>, and&nbsp;<em>Half-Life</em>&nbsp;have weapons that can support both types of physics.</p>
<p data-selectable-paragraph><em>Below: <strong>Halo.</strong>&nbsp;The Assault Rifle uses hitscan; the Needler uses projectile ballistics</em></p>
<p data-selectable-paragraph><em><img alt height="281" src="https://www.sickgaming.net/blog/wp-content/uploads/2019/12/blog-how-do-bullets-work-in-video-games-11.gif" width="500"></em></p>
<p data-selectable-paragraph><em><img alt src="https://www.sickgaming.net/blog/wp-content/uploads/2019/12/blog-how-do-bullets-work-in-video-games-12.gif" width="500"></em></p>
<p data-selectable-paragraph id="3aba">Developers can also mix two techniques to cover the weaknesses of each system to provide an even more life-like experience. For example, to fix the issue of objects phasing through each other from projectile ballistics, each bullet can draw a ray every tick of the engine. This helps the engine to see if any of the rays would intersect between ticks, colliding mid-air.</p>
<p data-selectable-paragraph id="5aa2">They can also be blended to enhance features in a game. A great example of this is in the&nbsp;<em>Sniper Elite</em>&nbsp;series; after pulling the trigger, the engine uses hitscan to determine if the shot is close enough to any detectable target to trigger slow motion. If true, it will fire a bullet with projectile ballistics in bullet-time.</p>
<p data-selectable-paragraph><img alt src="https://www.sickgaming.net/blog/wp-content/uploads/2019/12/blog-how-do-bullets-work-in-video-games-13.gif"></p>
<p data-selectable-paragraph><em>Above: <strong>Sniper Elite</strong></em></p>
<hr>
<p data-selectable-paragraph id="8a42">And that about covers the basics about how bullets work in video games! It’s interesting to see that the field is more focused on smaller refinements and improvements rather than massive overhauls. We haven’t made significant bounds and leaps after the first few revolutionary games were released.</p>
<p data-selectable-paragraph id="2f9d">So what now? What lies on the road ahead?</p>
<p data-selectable-paragraph id="58c0">I don’t see the hybrid approach going away anytime soon due to the extra features it provides, but I predict a lot of the improvements will happen on projectile ballistics. As we continue to increase the frequency of the tick computation (with increased CPU power), we will be able to approach the asymptotic limit of “real-life” bullet simulation.</p>
<hr>
<p><em>This article was based on a&nbsp;</em><a href="https://www.quora.com/How-do-bullets-work-in-video-games" rel="noopener nofollow noreferrer" target="_blank"><em>Quora answer</em></a><em>&nbsp;I posted. I would like to thank Pavel Drotár, Renaud Kyokushin, Paul Winstone, and Jason Fletcher for their comments.</em></p>
<p><em>Originally published on <a href="https://medium.com/@3stan/how-do-bullets-work-in-video-games-d153f1e496a8">my Medium account</a>. You can find me on <a href="https://twitter.com/3_stan">Twitter</a>.</em></p>
</div>


https://www.sickgaming.net/blog/2019/12/09/blog-how-do-bullets-work-in-video-games/