How to prevent sprites overlaping each other

Get help using Construct 2

Post » Thu Aug 09, 2012 9:59 pm

Hello,

I am generating enemies on my map randomly. Basically on start of layout I have

system create object x random (10,8000)
                     y random (0,500)

then I have system repeat 20x times. So this ones creates 20 enemies. the problem is that sometimes it generates same or really similar x or y so two or more sprites are touching each other which is something I want to prevent. Is there something I can do?

I was thinking something like. If there is collision between them then generate x and y again?Michal112012-08-10 09:01:19
B
8
S
2
Posts: 45
Reputation: 1,168

Post » Fri Aug 10, 2012 12:18 am

Without using arrays or anything, then it's your best bet, or do them on a grid by using small integers and multiplying.

If enemies are 32x32 pix square - I'd so something like :

X=1024+Int(Random(5))*32
Y=512+Int(Random(5))*32

Image
B
27
S
9
G
5
Posts: 487
Reputation: 11,939

Post » Fri Aug 10, 2012 9:01 am

Upss I had to reedit post right now. I forgot to add random into that. Now it shoud look same like my original code :) sorryMichal112012-08-10 15:38:11
B
8
S
2
Posts: 45
Reputation: 1,168

Post » Fri Aug 10, 2012 1:01 pm

@Michal11
You could try adding a subevent with a loop that continues to reposition the object until it's found an empty space..
This will loop a lot of times though if there is very little space for it to occupy

Also I haven't tested so it might not work as it is, but it's an idea.

repeat x times
-> system create object x random (10,8000), y random (0,500)

   [subevent]repeat (maximum number, just set to 20 or higher) times
             object is overlapping object
             ->set object(1) position x random (10,8000), y random (0,500)
             else
             -> stop loop





object(1) is a way to single out one of the object instances after the "object is overlapping object" condition.

It's not perfect, there is probably a better way.keepee2012-08-10 13:10:55
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

Post » Fri Aug 10, 2012 3:39 pm

Thank you this is exactly the way I want to do that. Hence I think there must some better way which is the reason I am asking :)
B
8
S
2
Posts: 45
Reputation: 1,168

Post » Fri Aug 10, 2012 3:41 pm

if you make the enemies solid then they should be moved outside of each other if they spawn on top of each other. Note I say "should" then if your enemies shouldnt be solid you can turn it off after theyre placed
B
13
S
5
G
2
Posts: 306
Reputation: 3,262

Post » Fri Aug 10, 2012 3:46 pm

[QUOTE=aridale] if you make the enemies solid then they should be moved outside of each other if they spawn on top of each other. Note I say "should" then if your enemies shouldnt be solid you can turn it off after theyre placed[/QUOTE]

I just tried that quickly but it does not work :) They still overlap each otherMichal112012-08-10 15:46:47
B
8
S
2
Posts: 45
Reputation: 1,168

Post » Fri Aug 10, 2012 6:20 pm

then you could try just a top level event for is overlapping to move it to some other position. That should fire if theyre created overlapping

Itll also fire after initial placing so take that into account. Its either that, an array, or the nested loop already mentioned I guess =/aridale2012-08-10 18:21:09
B
13
S
5
G
2
Posts: 306
Reputation: 3,262

Post » Fri Aug 10, 2012 10:16 pm



This seems to work, not sure its the best answer.

btw, I started with a sprite on the screen, that's why it only repeats 19 times.gazoogle2012-08-10 23:04:35
B
34
S
4
G
8
Posts: 67
Reputation: 5,857

Post » Sat Aug 11, 2012 7:41 am

[QUOTE=gazoogle]

This seems to work, not sure its the best answer.

btw, I started with a sprite on the screen, that's why it only repeats 19 times.[/QUOTE]

Excellent I think this is the best way. Thanks a lot mate
B
8
S
2
Posts: 45
Reputation: 1,168

Next

Return to How do I....?

Who is online

Users browsing this forum: mercuryus and 13 guests