# Random placement of objects

I need help with random placement of objects. In the attached file, I am able to place the Star object in random places. However, I want the Stars to show up only within the four colored bars and not underneath the middle square. If you refresh the scene, sometimes they show up in the wrong place. I assume something is wrong with my random placement math. Also, why is the web page background in my game showing up as black when I have set it to gray? Thank you for your help!
random-location.zip (8.42 KB)

Ok, I checked it
RandomStars.rar (7.77 KB)
The poblem is, make a random area with some void area (i.e. PlayerBox) needs to be computed in two steps: First compute the big random area, and then check if itâ€™s in the void area, if itâ€™s randomize again or move away.

-I think Object.X(254) has no sense in GD, since Object.X only returns the objectâ€™s position.
-Youâ€™ll find an expression ``` +pow(-1,Random(1))*(Random(64)+64) ```
It seems to work, so I didnâ€™t check it a lot, but it should return:

``+(Random(64)+64) or -(Random(64)+64)``

since -1^1 = -1 and -1^0 = 1, so I force the Star to move away from the center object

1 Like

Thank you so much for your help. This worked great. I appreciate that you included two different methods to solve it. Thanks for your time.

Thank you for this!

Couple questionsâ€¦

What timer do? And how could avoid collision on within created objects? (In this example, that stars doesnt touch eachother)

regards
Markus

Absolutely nothing hahaha, I let it there because it was already in the example that doomeyes send

Mmmmmhâ€¦ the stars collides other stars actually, the code just creates stars inside the area between the side walls through random ranges, avoiding collide the central obstacle through one of the two methods:

• If a star collides the central obstacle, move it a certain distance that ensures it will not collide the obstacle anymore.
• If a star collides the central obstacle, move it to a new random location, check again, if still collides, move it to a new random location, and so on (in a while loop).

If you want to place object randomly, avoiding dynamic/random/a-lot-of obstacles, for example if you donâ€™t want the stars to collide each other in the previous example, you have to:
First take all the stars, check if a star collides with other, if so move one of the colliding stars. Take all the stars, check again, and move the a colliding star to a new location, and so on. Again, in a While loop; it can be a little tricky, since Star vs Star collision check needs the two objects being in the list of objects taken into account, but the action to move the Star just have to move one of the colliding stars so they are not moved to the same place (generating an infinite loop), here is an example:
RandomStars2.zip (13.5 KB)
The example has been cleaned, useless code removed and comments updated. Note that in this new example, the While loop to move the colliding stars is NOT run each time a Star is created, but only once, after all the stars has been creted

As you can notice, creating only 300 stars make the game to freezes some seconds at the beginning. Also, each star you add will have to find a free space randomly in a world with less free space, so it will require more loop repeats. Aside the performance, the machine limit is to add a number of stars high enough so there will be at least one star with no free space to be located, in such case the while loop will be infinite

1 Like

Thank you, youÂ´re awesome!