Ask here your questions to get help using GDevelop.
User avatar
By doomeyes
#45787 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!
Attachments
(8.42 KiB) Downloaded 114 times
User avatar
By Lizard-13
#45788 Ok, I checked it :)
(7.77 KiB) Downloaded 192 times

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
Code: Select all+pow(-1,Random(1))*(Random(64)+64)

It seems to work, so I didn't check it a lot, but it should return:
Code: Select all+(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 :evil:
User avatar
By doomeyes
#45803
Lizard-13 wrote:Ok, I checked it :)
RandomStars.rar

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
Code: Select all+pow(-1,Random(1))*(Random(64)+64)

It seems to work, so I didn't check it a lot, but it should return:
Code: Select all+(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 :evil:


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.
User avatar
By MarkusL
#59719 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
User avatar
By Lizard-13
#59721
What timer do?

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

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

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:
(13.45 KiB) Downloaded 84 times

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