Random placement of objects with a 'bourder'

0 favourites
  • 14 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
  • Hey guys, I want to add some elements of randomness to a small game I've been working on.

    I have a number of sprites that I want to spawn randomly, but I want them to be at least 1300px away from each other.

    I have tried this code...

    ...but the sprites still overlap:

    Got any suggestions?

  • Hey !

    Be aware that doing this by only checking random coordinates can be very long if the map is full or nearly full of bases. You can even make an infinite loop here.

    That being said, the problem in the events is that you take 1 set of random coordinates per cycle of the main loop. So in total, you take maximum NrOfBases coordinates, even if one random set is valid or not.

    The second problem is that, if it find any base that is further enough, then it decides that it is a valid position. Instead, it should be only if ALL bases are further enough.

    So you need to change the base loop here, by making it a while instead of a for, and by modifying you "validation" detection. The easiest should be to do something like this :

    EDIT ! I forgot the While in the main loop....

  • Hey !

    Be aware that doing this by only checking random coordinates can be very long if the map is full or nearly full of bases. You can even make an infinite loop here.

    That being said, the problem in the events is that you take 1 set of random coordinates per cycle of the main loop. So in total, you take maximum NrOfBases coordinates, even if one random set is valid or not.

    The second problem is that, if it find any base that is further enough, then it decides that it is a valid position. Instead, it should be only if ALL bases are further enough.

    So you need to change the base loop here, by making it a while instead of a for, and by modifying you "validation" detection. The easiest should be to do something like this :

    EDIT ! I forgot the While in the main loop....

    Hi, sorry for responding so late...

    I tried your code (the one with the while loop), but the bases still spawn within that minimum distance...

    I then tried with MinDistance being less than "distance(Base.X, Base.Y, X, Y)", but that didn't load...

  • Hey TRMG!

    Here's a similar code. I used pick nearest so you don't have to check distance several times. Again, the same warning applies, if the space for spawning the amount of bases you need is too tight it may enter an infinite loop, so, use with caution.

    https://www.dropbox.com/s/x35ltcbwuw313 ... .capx?dl=0

    Hope this helps. Cheers!

  • Another alternative would be to implement an invisible hex grid and choose a random position in the grid for each of your bases. But this would make the whole stuff more regularly spaced, of course.

  • brunopalermo Thank you, it worked great!

    I got one problem with it tho, it seems not to spawn them all at the same time. Is there a way to make the game wait till it finished spawning them in?

    To stop the infinite loop problem, I think I'll check to see how many seconds went by since the start of the game and if and there have been like 3 seconds, I'll just stop the spawning. (Unless I'm wrong about this. This is my first randomly generated game, if you couldn't tell =D)

    Also, if it isn't to much to ask, could you show me an example of the hex grid?

  • You could spawn them invisible (or in an invisible layer) and just make all visible (or the layer) when the number of spawned bases reaches the total you want.

    Regarding the hex grid. I'll check a bubble shooter I have implemented that is built on an hex grid and adapt it. But I don't think I'll have it today...

  • You could spawn them invisible (or in an invisible layer)

    That wouldn't really work, as I need them for physics...

    Regarding the hex grid. I'll check a bubble shooter I have implemented that is built on an hex grid and adapt it. But I don't think I'll have it today...

    That would be awesome, thank you!

  • For physics? I'm not sure what exactly you're trying to do, so... If you share the capx, we may be able to find a way.

  • brunopalermo It's very basic stuff, like if the spaceship is overlapping the blue circle, it applies a force towards the center of it and if it touches the base, it destroys the ship.

    But the base needs to be in place before the player can play, otherwise a base could spawn right in front of them...

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • brunopalermo It's very basic stuff, like if the spaceship is overlapping the blue circle, it applies a force towards the center of it and if it touches the base, it destroys the ship.

    But the base needs to be in place before the player can play, otherwise a base could spawn right in front of them...

    Like I said, you could use a loader and an invisible layer. When everything is spawned you make the layer visible and let players play.

    Anyway... I just finished the capx with the hexgrid.

    https://www.dropbox.com/s/ggk6caxje7i6m ... .capx?dl=0

    See if this helps. Cheers.

  • Like I said, you could use a loader and an invisible layer. When everything is spawned you make the layer visible and let players play.

    Anyway... I just finished the capx with the hexgrid.

    https://www.dropbox.com/s/ggk6caxje7i6m ... .capx?dl=0

    See if this helps. Cheers.

    Oh, so I would stop the players with a variable or something and let them play after the bases have been spawned. Got it now, sorry for being so slow...

    Thanks for the hexgrid capx!

    It looks a bit too much like it's on a grid for this project, but I'll keep it in mind for later ones.

    Thank you for the help!

  • It looks a bit too much like it's on a grid for this project, but I'll keep it in mind for later ones.

    Yeah... I thought that would be a problem. Even though it's faster and safer.

    But... You could use the grid, with slots bigger than the range you want, and add a random misplacement, to remove this grid feeling.

    Well... Glad to help.

  • Here you go. Hexgrid with a random misplacement. It does feel more natural.

    https://www.dropbox.com/s/lylbpnbte4mfd ... .capx?dl=0

    You just have to tweak the values and find the optimal configuration for your needs.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)