Random objects without overlapping

Welcome to our brand new Clickteam Community Hub! We hope you will enjoy using the new features, which we will be further expanding in the coming months.

A few features including Passport are unavailable initially whilst we monitor stability of the new platform, we hope to bring these online very soon. Small issues will crop up following the import from our old system, including some message formatting, translation accuracy and other things.

Thank you for your patience whilst we've worked on this and we look forward to more exciting community developments soon!

Clickteam.
  • Hello everyone

    I'm looking for some information or advice again. As I continue learning Fusion, I give myself challenges that would be useful for a game I will work on in the future, and I try to solve them by myself. And every now and then I come upon a problem that I just can't seem to solve. So here is one I'm stuck at now.

    I'm trying to spawn 10 objects with random location, but without overlapping. Here's what I came up with

    - On key press (space) I start fast loop "spawn" 10 times
    - On fast loop "spawn" i create object A and move it to x rrandom(0,640) and y rrandom (0,480).

    It creates 10 objects on random locations but often they overlap, so I added

    -Object A overlaps object A : destroy object A

    So now I usually end with 8 and sometimes 6 objects, depending of how many of them overlap. Now for game I'll be working on, this will not be a problem as it would give it even more randomnes. But I would still like to figure out how to end up with all 10 objects.

    Is there a way to keep running fast loop until there are 10 objects left without overlapping? Or a way to look for overlapping inside fast loop? It kind of looks weird at the moment actually seeing objects get deleted after spawn.

    I tried adding:
    - object A overlaps object A: destroy object A
    : run fastloop "spawn" 10 times minus number of object A

    but didn't get any good results

    I have a feeling there's something really simple I'm missing but I just can't seem to figure it out at the moment :)

  • So I made some progress. This is what I have now:

    - NObjects (A) < 10 : Start loop "spawn" 10 - Nobjects (A)
    - On loop "spawn" : Create A, set x and y position Rrandom (number, number)
    - A is overlapping A : Destroy A

    It works like a charm, every time I run application, 10 objects are created on random position without overlapping. But now I lost the ability to manually execute spawn:( Because if I add "on keypress" like this:
    - NObjects (A) < 10 : Start loop "spawn" 10 - Nobjects (A)
    on press "space"
    - On loop "spawn" : Create A, set x and y position Rrandom (number, number)
    - A is overlapping A : Destroy A

    then I have to press space again if some of the objects get destroyed. It's a progress but not quite what I want at the moment

  • There's a couple ways you could approach this
    - Rather than destroying them, have overlapping objects move themselves a bit (e.g. 'Set X Position to X("Active 1") + rrandom(-5,5)' or something along those lines)

    Alternatively, something like this could work:
    1. On press space, start fast loop "loopX" 10 times

    2. On fastloop "loopX", start fastloop "loopY" 10 times

    3. On fastloop "loopY"
    - Limit Conditions: 1 out of 10 chances
    -- Create object at 0,0
    -- set object X position to 64*LoopIndex("loopX") + rrandom(-20,20)
    -- set object Y position to 48*LoopIndex("loopX") + rrandom(-20,20)

    This will create ~10 objects that (depending on how you tweak the random numbers and the size of the objects) shouldn't overlap

  • Thank you both. I feel kind of stupid for not thinking about moving the objects:) It just felt so logical to destroy them and create new ones...
    I managed to do it in the meantime, using a group in which I placed the previous code and activating it with space:D
    But moving the overlapping ones is way better and cleaner, thanks.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!