How do I prevent overlapping objects?

0 favourites
  • 9 posts
From the Asset Store
Welcome! I-Spy (Hidden objects) is an educational puzzle that is more than just seek-and-find activities.
  • I am randomising the location of playing cards, and I dont want them to overlap. I tried a WHILE loop to keep selecting random positions until there is no overlap, but it just hangs. I am trying to check of a sprite overlaps with another instance of itself (PlayingCard)

    Update, OK I realised I was setting the variable at the top of the WHILE loop, hence it was hanging. I am trying a different approach as below, by destroying sprites until there is no overlap, but it still isnt working, I am getting sprites overlapping each other. When I debug, I can clearly see PlayingCard overlapping with PlayerCard, but the is overlapping test never seems to get triggered. Is this related to instances trying to detect if they overlap with themselves? Does that not trigger?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • To check instances of the same object overlapping, put it in a family and check "is family overlapping object".

  • Without a loop, how do I keep creating objects until the right number are created, and none overlap?

  • I misread, I meant it in terms of you shouldn't need a loop to check overlaps, but you need it to generate your objects so I edited it out.

  • Hi,

    I think the problem is in the way Construct 3 engine handles object creation. I think that if you create multiple same objects the engine is unavailable to process the objects' X and Y coordinates in relation to one another before that tick has passed.

    This naturally makes it difficult to ensure that 100 objects that are created simultaneously the same tick in random position don't overlap. Maybe someone else can correct me on this.

    At least one solution that works is to create your new objects one at time each tick...Like this:

  • Thanks will take a look. In your example, what is the exit condition for the loop? If you have multiple conditions in the While loop - does only one have to evaluate to False, or all conditions?

  • It's a while loop that runs the number of times you need to create the object. Naturally you can make it run with any number of conditions. If you set up a loop they run like any other events, as long as all the conditions are true.

  • Hi, not sure if this was solved or not, but an old trick we used in C2 was to move the new card created if is overlapping another a few pixels to left or right until exits the overlap... so u can move it 5-10-20-50 px's around the overlapped card randomly, it would be much faster, and less hanging out.

    Another way of doing it would be to create a dictionary or array, and save the positions that the cards are spawning at... and just spawn anywhere else but in those locations.

    array thing might be to advanced and to complicated... but the 1st solution should work just fine, unless u want it to be pixel perfect, then 2nd mention would work better.

    for the 1st solution, i given above, to work, you have to have the cards inside a familly lets say "familly cards" and then (C3 doesn't really need families to pick objects of the same kind, pretty neat, you can just directly pick same card item name top... forgot about that feature)use the overlap condition like so:

    if object.card is overlapping object.card > subcondition >> Action >setx.object.card to self.x +20

    the above line would push the newly created card to the right of the old card that is below it.

    consequently u could also, move the "famillyCards" when they are overlapped to a random +- Y position.

    il do a C3 example in a bit, for the 1st suggestion, just i can't use famillies cause freeEdition limitations... but il use 2 different sprites.

    random items, no grid placeholders example 1

    Edit2: now if your game doesn't use the spawn like in my example, but u have like a limited grid layout to spawn in ... for example, a chess table like format... u can generate a grid pattern using a sprite give it a bolean lets say "isoverlap" and toggle it false or true when u generate cards in their position. and then u can check those grid items, if "isoverlap" then pick another one. this way u don't have cards moving around ... they spawn directly in a empty space. the array solution is similar to the grid pattern with items, as grid... but more in logic ... events etc... so u see why i think array is a bit harder to implement, however it might be the best solution, but lots of work on it, since ul have to add the position spawned at+ the offset distance to keep top left right and bottom when u create the new cards.

  • Thanks for all the suggestions, I have something which seems to work by randomising the positions. I did think about creating an array of positions, and may still do that. I am convinced you cannot check for overlap in the same tick you spawned the object in. I am doing the overlap separately in a separate tick by moving the overlap logic above the spawn logic.

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