Random placement of objects with a 'bourder'

Get help using Construct 2

Post » Wed Sep 27, 2017 5:37 pm

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...
Image

...but the sprites still overlap:
Image

Got any suggestions?
ImageImageImageImage
B
31
S
9
G
7
Posts: 262
Reputation: 5,671

Post » Wed Sep 27, 2017 7:07 pm

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 :

Image

EDIT ! I forgot the While in the main loop....
Image
B
17
S
9
G
4
Posts: 473
Reputation: 6,281

Post » Thu Sep 28, 2017 3:39 pm

Guizmus wrote: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 :

Image

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

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...
Image

I then tried with MinDistance being less than "distance(Base.X, Base.Y, X, Y)", but that didn't load...
ImageImageImageImage
B
31
S
9
G
7
Posts: 262
Reputation: 5,671

Post » Thu Sep 28, 2017 6:37 pm

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!
Bruno

I like to eat, but I'm no cook. Why liking to play games would make you a game designer?
B
22
S
6
G
3
Posts: 717
Reputation: 3,996

Post » Thu Sep 28, 2017 6:40 pm

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.
Bruno

I like to eat, but I'm no cook. Why liking to play games would make you a game designer?
B
22
S
6
G
3
Posts: 717
Reputation: 3,996

Post » Thu Sep 28, 2017 9:47 pm

@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?
ImageImageImageImage
B
31
S
9
G
7
Posts: 262
Reputation: 5,671

Post » Thu Sep 28, 2017 10:03 pm

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...
Bruno

I like to eat, but I'm no cook. Why liking to play games would make you a game designer?
B
22
S
6
G
3
Posts: 717
Reputation: 3,996

Post » Thu Sep 28, 2017 10:12 pm

brunopalermo wrote:You could spawn them invisible (or in an invisible layer)

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

brunopalermo wrote: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!
ImageImageImageImage
B
31
S
9
G
7
Posts: 262
Reputation: 5,671

Post » Thu Sep 28, 2017 10:14 pm

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.
Bruno

I like to eat, but I'm no cook. Why liking to play games would make you a game designer?
B
22
S
6
G
3
Posts: 717
Reputation: 3,996

Post » Thu Sep 28, 2017 10:57 pm

@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...
ImageImageImageImage
B
31
S
9
G
7
Posts: 262
Reputation: 5,671

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 19 guests