Best approach to randomly placed islands that don't overlap?

0 favourites
  • 5 posts
From the Asset Store
Best car suspension with spring effect and very cool terrain generation.
  • My goal is to have 20 pre-made islands randomly placed on an overworld map that don't overlap.

    I'd love to hear if anyone has any clever solutions. I can explain what I've tried below, but it's a bit lengthy and I'm not convinced it's the best approach anyway so it's maybe not necessary to read;

    At the start of the layout the islands are outside of the layout. They all have an instance variable ID.

    Actually what I'm testing is a sprite that's set to the same size and location then pinned to the island tilemap since I can't test if tilemaps are overlapping objects for some reason.

    Then that object is placed into two families because that's the only way I can pick two of the same object and differentiate between the two (if family1 is overlapping family 2).

    At the start of the layout I call a function (with the islands ID as parameter) to place the first island randomly. Then I check to see if the island is overlapping another island (it's not because it's the first island).

    If it is overlapping, I call the same function again with the same island ID.

    If it is not overlapping I call the function with the islandID + 1.

    And I repeat that until all the islands have been placed.

    Or at least that's how it's supposed to happen. In reality what happens is it doesn't see the islands as overlapping so it doesn't call the function again, it goes on to the next island. But I know it can pick the overlapping objects because after all the islands have been placed I check to see if any are overlapping and it is aware that they overlap.

    When I don't check to see if that specific instance in the being placed in the function is overlapping (just checking if any islands are overlapping)it runs into the first point of an overlapping island (usually around the 6th island but depends), and then continually calls the same function even if it's not placed somewhere not overlapping.

    So I'm sure there's some bug somewhere, but I'm concerned this is just not the right approach and may take too long for the computer to iterate to a configuration that has no overlapping pieces anyway.

    Any help anyone has would be appreciated.

  • Some people run into a problem with picking instances in the same tick because they're not available to be picked yet. If your loop is running in the same tick without any waits then it might not be able to use the islands you have created for an overlap check so waiting for next tick or wait 0 seconds before calling the function each time may help. I believe it waits until the next top level event for instances to be made available so if this is all looping in sub events it may be the case. Also it may just be an issue with the picking conditions and 'family overlapping family' but I can't see the events so it's only guessing.

  • That would explain the behavior of the bug I'm seeing. But I'm not creating them, I'm moving them so they should be available right? Here is the function.

    Line 6 does not come up as true, but line 8 does.

    The IslandCollisionWorkaround is object that I'm using to test if the tilemaps are overlapping.

    What's even more confusing to me is that without the "wait" actions, then line 8 IS NOT true and says there are no overlaps. But with the waits in the function it recognizes that there are overlaps.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • This can be done a bit easier. Check out these two demo projects:

    howtoconstructdemos.com/spawn-objects-randomly-without-overlapping-2-capx

  • Once I got it converted for my specific needs it works great. Thanks you both for the help and taking the time!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)