I'm quite new to programming and was just wondering the best or a quick way to spawn an object avoiding overlap in a java game.
I have an array of spike pits that I want to place randomly on the level. The code I currently have for painting the image on screen is:
// Initialise all Spike Pits
for (int k = 0; k < NUMBER_OF_SPIKEPITS; k++) {
spikepitX = rand.nextInt(3600) + (thePlayer.getX() + 20); //will ensure that the spike pit cannot spawn under the player start position
spikepitY = (GroundLevel - 33);
spikepit[k] = new SpikePit(spikepitX, spikepitY);
}
init();
This prints them at random points along the ground for the width of the level, but some of them overlap. Is there anything I can add to prevent this?