I’m looking for an idea/approach to how to make a randomly generated area that is filled with boxes. The game is a top-down 2D game.
The area is like a corridor. The player starts from the bottom and they go all the way up. To make things simple, let’s say I want to put just 2D boxes in the area randomly. So, the path the players need to follow will varies. Also, the boxes shouldn’t be blocking the player’s path.
Currently, I divided the area into grids. I randomly select a grid box and put a 2D box in it. If they block the player’s path, I select another random grid box. This approach works, however, as you play the game, you can see the pattern. What I’m looking for is more randomized. I would like even the 2D boxes’ sizes and their rotations to be randomized without blocking the player’s path.
You could look into Poisson Disc Sampling, but im not sure if it fits your specific criteria. PDS is an algorithm for (potentially) tightly packing randomly placed dots into a square, so it is ideal for placing items on something like procedurally generated terrain, or other chunky / rectangular areas. You can determine the radius of the dots to be the diameter of your boxes plus some padding (minimally half the player width) to guarantee that there will always be paths around the boxes.
This would result in randomly placed boxes and guarantee paths around the (each) box. However, depending on what you are actually looking for, you may only want to guarantee one path, and have other boxes potentially be close enough to each other to block the path through them. You didnt really clarify that. In this case however i would go with the approach you mentioned, where you place boxes and check whether they block the path.
To get more precise and / or fitting advice for your given scenario it would help to better describe it. Screenshots of what you have would likely also help visualizing the idea you have in mind as well as the current problem.
I searched the PSD algorithm on the Internet and it looks like it meets my needs. I wasn’t trying to make a labyrinth type of area. The purpose of the boxes is giving the player some covers. Thank you.