• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Bettts · Jan 24, 2020 at 02:02 AM · 2dprocedural generationoverlapping

Why is it that my planets aren't overlapping with each other on spawn, but they do appear to be overlapping with my sun?

 public class MapGenerator : MonoBehaviour
 {
     //start with constant number of planets
     private const int numPlanets = 20;
 
     //start with map size at 10 units
     public static int mapSize = 10;

     //sunPlanetSizeOffset is the difference in size between the sun and the planets
     public float sunMaxSize, sunMinSize, sunPlanetSizeOffset;
     public GameObject[] planets;
     public GameObject[] suns;
 
     private void Start()
     {
         //generate a sun
         int randomSun = Mathf.RoundToInt(Random.Range(0, suns.Length - 1));
         float sunScale = Random.Range(sunMinSize, sunMaxSize);
         GameObject sun = Instantiate(suns[randomSun]);
         sun.transform.localScale *= sunScale;
 
         //generate planets
         for (int i = 0; i < numPlanets; i++)
         {
             Vector3 initPos = new Vector3(0, 0, 0);
             float planetScale = 1f;
             bool isOverlap = true;
 
             int safetyNet = 0;
             while (isOverlap || safetyNet < 15)
             {
                 initPos = generateInitPos();
                 planetScale = Random.Range(1f, sunScale - sunPlanetSizeOffset);
                 isOverlap = determineOverlap(initPos, planetScale);
                 safetyNet++;
             }
 
             if (!isOverlap)
             {
                 int randomPlanet = Mathf.RoundToInt(Random.Range(0f, planets.Length - 1));
                 GameObject planet = Instantiate(planets[randomPlanet], initPos, Quaternion.identity);
                 planet.transform.localScale *= planetScale;
             }
         }
     }
 
     /// <summary>
     /// determines if the chosen location is already occupied
     /// </summary>
     /// <param name="pos"></param> location chosen
     /// <param name="radius"></param> radius for planet
     /// <returns></returns> true if location is occupied, false otherwise
     private bool determineOverlap(Vector3 pos, float radius)
     {
         Collider2D[] colliders = Physics2D.OverlapCircleAll(pos, 2 * radius);
         return !(colliders.Length == 0);
     }
 
     private Vector3 generateInitPos()
     {
         float radius = Random.Range(1f, 8f);
         float theta = Random.Range(0f, 2f * Mathf.PI);
         float x = radius * Mathf.Cos(theta);
         float y = radius * Mathf.Sin(theta);
         return new Vector3(x, y, 0);
     }
 }

I'm trying to understand the best way to do procedural generation in Unity, and I feel like I'm almost there. In this example, I'm generating a solar system by first spawning a sun with random scaling at the middle of the map and planets with random positions and scaling across the map in a way where supposedly nothing is overlapping. At the moment, my planets seem to be respecting the other planet positions, but for some reason there's still some overlap with my sun as seen in the attached image. Does anyone know what could be causing this? Thank you!

alt text

planet-spacing.png (66.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jan 24, 2020 at 02:54 AM

That's because by default Auto Sync Transforms is disabled in the 2d Physics settings. Since no physics frames happens between you creating your objects / colliders and trying to test against them, they haven't yet been updated. So you can enable auto sync transforms in the physics2d settings or call Physics2D.SyncTransforms after you created a new circle to make sure the colliders are actually updated.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

243 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to assign individual data to a tiles? (Unity3D Tilemap) 0 Answers

Best way to go about top down procedural 2d city 0 Answers

OverLapping Objects in 2d? 0 Answers

Dungeon rooms overlapping 0 Answers

How to add tilemap to procedural generation 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges