# Spawn sprites randomly within a circle

Get help using Construct 2

### » Sat Feb 23, 2013 7:01 pm

Hiya

Any ideas how you'd make enemies randomly spawn within a circular (or any irregular) shape?

I don't want to hard code spawning locations if possible.
B
20
S
6
G
2
Posts: 157
Reputation: 3,109

### » Sat Feb 23, 2013 7:38 pm

Hmm..

Maybe after a circle is spawned you set the circle to spawn random sprites at locations inside the Area of the circle.

If the area of your circle is 200 then the radius is 100.

So maybe something like this:
Event>>>Circle on screen
Action>>> Create object on positions (circle.width/2 + random(-100,100),circle.height/2 + random(-100,100)

I think this will spawn enemies within the radius of the circle and then add or subtract a couple of distances within the circle. The -100 to 100 is the interval I guess, so once the position of half the circle is decided the system will either subtract or add a value between -100 to 100 to the center point of the circle giving your a random spot within the circle.

You could probably replace the value of 100 with the radius that you want for your circle

[QUOTE=silkc2] Hiya

Any ideas how you'd make enemies randomly spawn within a circular (or any irregular) shape?

I don't want to hard code spawning locations if possible.[/QUOTE] retrodude2013-02-23 19:39:26
B
18
S
4
G
3
Posts: 436
Reputation: 4,801

### » Sat Feb 23, 2013 8:53 pm

Well spawning within a irregular shape is different than a circle.

for circle
x = (cos(random(0,1)) * radius) + position
y = (sin(random(0,1)) * radius) + position

for an irregular shape
uses boxes. randomly spawn based on the box. put the boxes in whatever shape you want. # = spawn box, O = no spawn box but open space
#O#
###
OO#
That would be an irregular shape if the players can't see the spawn boxes :D

B
87
S
18
G
9
Posts: 2,455
Reputation: 14,844

### » Sat Feb 23, 2013 9:04 pm

Thanks guys

I was trying to spawn in lots of different shaped windows, hexagonal, circular, Anthony you can think of, without design-time hard coding.

I don't think there's anything at the moment that allows sprite within sprite "bounding box" constrained random points.

Once I figure out how to make plugins I might look into making one for this.
B
20
S
6
G
2
Posts: 157
Reputation: 3,109

### » Tue Nov 12, 2013 2:19 pm

@jayderyu can u make a capx of this
B
6
Posts: 49
Reputation: 423

### » Tue Nov 12, 2013 3:10 pm

touch 0 end - create objects on [square.X + random(-square.width/2, square.width/2), square.Y + random(-square.height/2, square.height/2)];

it's an approach of a square.

If I remember my last solution for it, I'll come here and share.

Edited:
= Touch 0 end
-> Create objects on [circle.X + cos(random(360) * circle.width/2), circle.Y + sin(random(360) * circle.width/2)];

B
95
S
21
G
16
Posts: 1,337
Reputation: 20,955

### » Fri Nov 14, 2014 3:25 am

TELLES0808 wrote:touch 0 end - create objects on [square.X + random(-square.width/2, square.width/2), square.Y + random(-square.height/2, square.height/2)];

it's an approach of a square.

If I remember my last solution for it, I'll come here and share.

Edited:
= Touch 0 end
-> Create objects on [circle.X + cos(random(360) * circle.width/2), circle.Y + sin(random(360) * circle.width/2)];

Forgive me for replying to an old post. Just wanted to tell anyone else that might want to know how to solve this problem. This solution worked really great for me. Thanx guys.
B
21
S
5
Posts: 44
Reputation: 1,640

### » Sun Nov 23, 2014 9:26 pm

hi,

I tried:
Every 0,1 seconds:
Create object on (circle.X + cos(random(360) * circle.width/2), circle.Y + sin(random(360) * circle.width/2)];

My wish is that the objects are all created at a random place, within the circle, not overlapping the outside of this circle.
This did not work for me.
Do you have another suggestion?

I think I am looking for a way to spawn objects at an x distance pixels from the middle of a circle.
If I worked with a square, this would have been no problem.
Circles provide more complexity.
B
16
S
5
G
7
Posts: 602
Reputation: 5,006

### » Sun Nov 23, 2014 9:34 pm

Hi @Shadowdawnz - it's no problem replying to old posts because the discussion is often useful to someone else who finds the threads much later on.
B
9
S
3
G
1
Posts: 346
Reputation: 1,560

### » Sat Dec 27, 2014 4:44 am

applehiku wrote:hi,

I tried:
Every 0,1 seconds:
Create object on (circle.X + cos(random(360) * circle.width/2), circle.Y + sin(random(360) * circle.width/2)];

My wish is that the objects are all created at a random place, within the circle, not overlapping the outside of this circle.
This did not work for me.
Do you have another suggestion?

I think I am looking for a way to spawn objects at an x distance pixels from the middle of a circle.
If I worked with a square, this would have been no problem.
Circles provide more complexity.

I didnt not try with circles, only with squares.
Unfortunately, my math is limited at this point in time.

hundredfold wrote:Hi @Shadowdawnz - it's no problem replying to old posts because the discussion is often useful to someone else who finds the threads much later on.

That is true.
B
21
S
5
Posts: 44
Reputation: 1,640

Next