Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Calculating non-overlapping positions near a point

Discussion in 'Scripting' started by Nanako, Nov 2, 2015.

  1. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    Hi all. I'm working on something of a strategy game, and i could do with some math assistance.

    I'd like to select multiple units, and send them all towards a specified place. They should each take up a position somewhere near the specified place, without standing inside/ontop of each other.

    I don't have any need for formations here. Infact, i'd prefer it if the positions were sort of random and unorganised. But i'm worried if i just generate a bunch of random offsets, i'll end up with overlapping sometimes as an inevitable result of probabilities.

    I suppose i could bruteforce it by randoming and checking to see if the location is at least a certain distance from other locations, but that seems liable to be slow, and possibly lead to infinite loops.

    I can assume that all the units will occupy a circular ground area with a certain radius. The radius may change from one operation to another, but all the units i'm moving in any particular group will have the same radius.

    Can anyone provide thoughts on how to do this efficiently?
     
  2. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    This might not be the best, or most efficient way to accomplish what you're looking for, but here's what I came up with.

    You could create a grid where the specified target/position would be the origin, and create subdivisions (based on whatever radius you need) to represent available positions. Then you could just store the available position as integers in a list, and prevent multiple units moving towards the same position by removing elements from the list once allocated randomly to a unit.
    Here's a basic graphic in case my explanation sucked.
     

    Attached Files:

    • Grid.png
      Grid.png
      File size:
      3.7 KB
      Views:
      1,050
    Last edited: Nov 2, 2015
  3. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    hmm, it's a bit too un-random is what i'm thinking, though. The outer nodes are unreasonably far from the centre point, so that they don't really make sense. And if i restrict it to inner ones, it would still create a tight grid
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    How are you moving your agents? Sounds like close enough is good enough. Just set random offsets, then if the agents collide when they get close have them jostle and move one another.
     
  5. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    I'll probably use unity's navmesh/pathing system.

    I don't feel like the inordinate amount of work required to make them move efficiently in formation, so i was planning to just not make them collide with each other. I suppose it could work though. collision and random offsets might just do it.

    It's not the solution i hoped for though, and it's not a solution i'll want to use later, this problem will definitely come up again. Right now i'm just knocking out a quick project but i was hoping to design a decent group placement function i can use more robustly, for a scenario where unit collisions aren't enabled or relevant