[ad_1]
I am creating a game with infinite terrain. Sections of the terrain are broken up into regions of fixed size for handling (similar to chunks in Minecraft). I would like to load and unload them as needed as the player moves so that every region within a visibility range is visible and the others are not. For simplicity the pattern of loaded regions can be a square instead of a circle approximation.
The naive way to do this is to have a loop that runs every game update which iterates all loaded regions, and those who have an X or Y or Z coordinate that is out of range are deleted, and then have another loop that runs and ensures that all spaces that are within the X and Y and Z coordinates are not empty.
However having 2 loops every frame does not seem very efficient. A simple improvement is to only run those loops if the player location changes. But that is still every frame while the player is moving.
I could also only run the loops if the player crosses a region boundary, but still those loops are going to go over a lot of regions that do not need to be updated. In theory, only the regions at the edge of the loaded regions area need to be iterated over, if the player crosses a region boundary.
What is the most efficient algorithm to dynamically load and unload regions as needed around a player, in a cube/square fashion?
[ad_2]