r/howdidtheycodeit • u/TheCatOfWar • Jun 30 '22
Question How does hitscan shooting work in 3D games?
I could visualise and code a system for a 'physical' projectile in a game; where it is fired with an initial position and movement vector and then every (one or a few) times a frame it moves in increments, potentially also losing velocity or being affected by gravity.
But classic shooting games and their modern counterparts eg Counter Strike often use hit-scan weapons, where the very tick that the weapon is fired it instantly plots a straight line through 3D space to its eventual target.
Of course, you could just do this by doing the same thing as the projectile version, just running your 'move and check collision' loop as many times as it takes within one frame, but it seems suboptimal to do so many collision checks in one frame and potentially cause a lag spike, and is also vulnerable to the 'bullet through paper' problem if the collision checks aren't frequent enough. There are ways to mitigate this but I wondered if this is actually how its done or if another method is used?
I can sort of imagine some system using 3D projection to essentially 'look' from the pov of the gun and see what is directly in front of it, and then put that back in world space etc, but I'm not sure how I would write that or if it would truly work.
Many thanks!
Edit: Yea I get that it's raycasting and vector x triangle or solid collisions, was just hoping for some explanations of the actual maths involved i guess, but thanks for the responses!
3
u/_arc5 Jun 30 '22
You already mentioned you're using AABBs to represent your collidable geometry, and seems like you understand the general concept of a ray: just a direction with an origin point. From here it's a simple question: given our AABBs which object would a particular ray hit. This is a ray cast and involves some simple linear algebra to solve it. It's too messy to put equations on here, but there's plenty of YouTube resources out there that explain the math. Let me know (dm) if you have any specific questions after watching a couple of those videos and I'd be happy to hop on a discord call and try and answer them.
2
u/kvicker Jun 30 '22
https://www.amazon.com/Real-Time-Collision-Detection-Interactive-Technology/dp/1558607323?ref_=d6k_applink_bb_dls&dplnkId=017c9f0b-56ea-4618-84d0-8a7cb6d8a0f1
1
u/3030thirtythirty Jun 30 '22
So, how did you build your hitboxes? Are they cubes?
2
u/victfv Jun 30 '22
Let's say you're trying to hit a wall and the wall's collision is a plane. You can parametrize your ray as a line and use linear algebra to find the intersection point between a line and a plane (just an example, there are probably better ways of doing it). Let's say your level has 10 walls. You'll have to check that way for each of them. If you have too many walls in your level, you'll have to use some sort of space partitioning and only check against the parts of space your ray is inside. For other collider shapes, just use different algorithms, like ray-sphere intersection for spheres, etc.
1
u/[deleted] Jun 30 '22
I'd assume they create a 2D projection of a 3D model from the origin point, then do a line-box intersection test.
I've only ever "rolled my own" collision code in 2D so no idea how the first half of that would work!
1
u/the_Demongod Jun 30 '22
The ray-x intersection test just involves solving a system of equations to determine whether or not the ray intersects whatever shape you're interested in. The way you code that is to just find whatever math is needed to solve the system and implement it. The complexity of the systems of equations depends on the complexity of the geometry being intersected. If you're very math-fluent and want to figure it out yourself for fun you can just solve the problems on paper, or you can just google "ray-x intersection algorithm" (ray-box, ray-triangle, ray-capsule, etc.) to find an optimal algorithm someone else has already figured out.
If you have an algorithm for ray-AABB intersection (or any other axis-aligned geometry), you can easily turn it into ray-OBB intersection (or any other oriented geometry) by applying the inverse of the geometry's world-space transformation matrix to the ray's direction and origin rather than needing to find a bespoke ray-OBB intersection algorithm. Remember that the direction of the ray transforms like a vector (x, y, 0) and the origin of the ray transforms like a point (x, y, 1).
1
u/Claytonious Jun 30 '22
"introduction to Ray tracing" by Glassner is a great, compact book that details all of the algorithms you need for ray intersections with geometry.
"3d game engine design" by eberly has even more practical advice, but is a longer book covering more.
0
u/[deleted] Jun 30 '22
[deleted] — view deleted comment
0
u/[deleted] Jun 30 '22
[deleted] — view deleted comment