Removing current X values from a possible arrayList object spawn

I am working on a game where a ship avoids walls in 3D. I wanted to make sure the walls do not overlap in x values. Currently, they spawn in on a timed interval like so:

 if (walls.size() <= max) {  
// max is an int changed based off user's score
      if (millis() - intialWallAddTime > wallAddInterval) {  
        intialWallAddTime = millis();  
        wallAddInterval= int(random(1000, 3000));  
        walls.add(new Wall(random(200, 1800), 100, 20));
 for (Wall w : walls) {  

The boxes come towards you, go off-screen, then are sent back to the top of the screen. And they usually end up overlapping and making the game easier and rough-looking. Is there any way to spawn them in with x values that are then removed from the next wall’s potential x values. Or each wall is tested to see if it will overlap.


Hi @spamelim ,

Is it possible for you to show us a screenshot / gif of your game with the walls so we can see what you are talking about ? :wink:

Because I kind of understand what you mean but with an image/video it would more clear!

I kept getting lucky and not having any walls overlap when I was trying but here are the best two screenshots I could get of it.Sometimes it would be a lot worse than this with two walls spawning completely on top of each other

Ok now I see what is the issue! :wink:

You stored your walls into an array so what you need to do is to detect the collisions between all the walls when you want to add one.

Since your walls are always on the ground, you only need to check 2D intersection of rectangles (or squares if they have the same width and height).

When you add one wall, check the intersection with every other wall and if it intersects, choose another location and repeat again…