Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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 darken a procedurally added texture (explained below) 0 Answers

2d Solar System procedural generation 1 Answer

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

How can I prevend two Circle Collider's (2D) overlapping on collision? 1 Answer

Outline Overlapping Game Objects 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges