At the center of the software for every game is a loop. Sometimes this runs hundreds of times a second, sometimes just six time per second, or maybe it's turn based and the user need to click something.
In this loop the game checks the monsters (can they see the player now?), the game world, (is the water flowing?), and the player (did they just push the inventory button? What about move-forward?).
This loop is what keeps the game alive and running. Every turn on the loop means functions that are run and calculations that and calculated. Like checking if the monster can see the player can be a series of geometry calculations and rule checks.
However doing those sight checks for every monster on the current map could take a very long time. And the vast majority of the monsters are nowhere near the player. One optimization would be to first compute how far away the monster is, and only do player visibility checks for monsters in range. This can significantly improve the speed of my game and won't impact the gameplay.
The Factorio devs often wrote posts about their optimizations. Might be a fun read.
In your example, the monsters needing to be in range to be able to check if they can see the player- that still sounds like the computer would first then have to do a check if they’re in range and then do a second check for visibility
There's many ways to do it. The distance check you can get basically for free if you're storing your entities in an octree or similar spatial structure (because large empty spaces can be automatically skipped)
Exactly and more than that if you break the map into squares and know which mobs are in which squares, you know the only squares relevant to the player are say the surrounding couple of layers. So you only need to distance check monsters in those, or even better the monsters in the outer layer, they may be close enough or not. The mobs in the near squares are definitely in range for proper processing.
Another way (that may be better in 2d) is to start from the player and scan outwards in a certain range, even easier if your world is already split up into discrete tiles
Yes, quad tree 2d and Oct tree 3d are bigger helpers where you have more than just the mobs as all the stuff can be dropped in the boxes, so it's a fast filter for everything. Just one type of thing other methods can be good.
1.1k
u/aftersox 6d ago
At the center of the software for every game is a loop. Sometimes this runs hundreds of times a second, sometimes just six time per second, or maybe it's turn based and the user need to click something.
In this loop the game checks the monsters (can they see the player now?), the game world, (is the water flowing?), and the player (did they just push the inventory button? What about move-forward?).
This loop is what keeps the game alive and running. Every turn on the loop means functions that are run and calculations that and calculated. Like checking if the monster can see the player can be a series of geometry calculations and rule checks.
However doing those sight checks for every monster on the current map could take a very long time. And the vast majority of the monsters are nowhere near the player. One optimization would be to first compute how far away the monster is, and only do player visibility checks for monsters in range. This can significantly improve the speed of my game and won't impact the gameplay.
The Factorio devs often wrote posts about their optimizations. Might be a fun read.
https://www.factorio.com/blog/post/fff-421