How spawn random objects only inside another object?

Hey ! My name is Hatsuya, i’m new on Gdevelop.I’m sorry for some stupidity, but I’ve been trying lots and lots of ways, and none of them worked.

Basically what I want to do here is spawn some grass randomly inside my area called “chunk”

I want to do it in a way that no matter where the chunk is placed, the grass respects it and will always be randomly placed inside the chunk.

Any idea how I can do this?

1 Like

You can have something like this

Create grass at X: RandomInRange(Chunk.X(), Chunk.X()+Chunk.Width()), Y: RandomInRange(Chunk.Y(), Chunk.Y()+Chunk.Height())

It should work, it picks a position between the Chunk’s X position (it is at the top left corner of the sprite) and Chunk’X Position + it’s width. So, if the Chunk’s X position is 100 and it’s width is 50. it will create the grass between X 100 and 150. It applies same for the height

:slight_smile:

That won’t quite work. Grass could end up with the origin on the very right of Chunk or below it. You’ll need to take the width of grass into account. After the grass object has been created, update it’s position as :

Set position of grass as X: RandomInRange(Chunk.X(), Chunk.X()+Chunk.Width() - grass.Width()) , Y: RandomInRange(Chunk.Y(), Chunk.Y()+Chunk.Height()- grass.Height())

5 Likes

It Works ! Thank you so much :smiley:

image

I just have one last question, how do I make it so when the objects spawn, they don’t collide? is there any way to do this?

I want them to spawn without being on top of each other like in the image above

Try checking if they are colliding, if yes, then delete one of them

:slight_smile:

1 Like

Hey, i might be a little late, but what if the “chunk” is for example a circle and you want the objects to spawn randomly in that circle?

Then I’d suggest using trigonometry. Select a random angle (between 0 & 360) and a random length (between 0 and the circle radius).

Then use
x = sin(ToRad(random_angle)) * random_length
and
y = cos(ToRad(random_angle)) * random_length

1 Like

Hey, i just tried it out and wrote
x: sin(ToRad(RandomInRange(0, 360))) * RandomInRange(0, 10) and
y: cos(ToRad(RandomInRange(0, 360))) * RandomInRange(0, 10) but all it does is spawn the object in the top left corner of the screen everytime. Im kinda new to gdevelop so sorry if im making dumb mistakes.

Firstly, if you do it that way it’ll place the objects in a square. You need to put the random angle and random length into variables, and use those variables in the position expression.

As an example why, say that the x random angle is 0, and the y random angle is 90. Sin(0) = 0, and cos(90) = 0, so the position would be (0,0), and not within the circle you’re after.

To why your object is spawning at (0,0), you’ll have to make the correction I mentioned at the start of this post. I suspect it still won’t work, so please post a screen snip of the events that set the random angle & length, and where the object is created using those variables.

1 Like

EDIT: So i realized my mistake, basically i didnt add the circles center to the calculations, i fixed it by just adding “+ circle_center” at the end. Thanks MrMen for the help you provided.

So these are the events:

And this is the result:


I think it creates the object around the 0,0 position rather then in the circle

1 Like