Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Xentarok · Jun 01, 2013 at 06:25 PM · generationorbitgeneratorplanetsstars

Random generation problem.

Hello.

Latelly I've been working on random star system generator, but I have one problem.

Sometimes, planets spawn on same orbits which obviously shouldn't happen, but I don't know how to solve this problem.

Here's an example:

alt text

As you can see, 3 planets share the same orbit.

Here's the generation code:

 Vector2 pos = new Vector2(Random.Range(-260,260) ,Random.Range(-260,260));        
 Instantiate(prefab, new Vector3(pos.x,0,pos.y), Quaternion.Euler(0,0,0));

I hope that someone can help.

-Xentarok

P.S.: English isn't my native language, so I apologize for any mistakes.

generation problem.jpg (80.8 kB)
Comment
Add comment · Show 6
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
avatar image Eric5h5 · Jun 01, 2013 at 06:34 PM 1
Share

Your English is fine, but nobody can help you unless you post code.

avatar image Memige · Jun 01, 2013 at 08:07 PM 0
Share

So to be clear, does this behavior happen often, or just occasionally?

avatar image robertbu · Jun 01, 2013 at 08:22 PM 0
Share

Are you placing them all at once or are they added at different times?

avatar image Xentarok · Jun 01, 2013 at 08:44 PM 0
Share

Well, it happens randomly but I want to eli$$anonymous$$ate it, and they are placed all at once.

avatar image Memige · Jun 17, 2013 at 08:39 PM 0
Share

Hey Xentarok, if any of the answers helped you solve your question, could you flagh it as the correct answer? This will help others with similar questions more easily find this one. Thanks!

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Jun 01, 2013 at 09:09 PM

  • Start a new scene

  • Attach the script below to an empty game object

  • Place a sphere at the origin (or anywhere) to represent the sun

  • Drag and drop the sun onto the 'Sun' variable in the empty game object

  • Move the camera back to -40 on the 'Z' axis

  • Hit run

After you run it once to see how the planets are placed. Uncomment the line:

 //v3 = Quaternion.AngleAxis(Random.Range(0.0, 360.0), Vector3.forward) * v3;

...and then run it again to see the planets spread out.

 #pragma strict
 
 var sun : Transform;
 
 var currDist = 0.5; 
 var minDist = 1.0;  // Minimum distance from the previous planet
 var maxDist = 2.5;
 
 var count = 9; 
 
 function Start() {
     PlacePlanets();
 }
 
 function PlacePlanets() {
 
     for (var i = 0; i < count; i++) {
         currDist = currDist + Random.Range(minDist, maxDist);
         var v3 = Vector3.right * currDist;
         //v3 = Quaternion.AngleAxis(Random.Range(0.0, 360.0), Vector3.forward) * v3;
         v3 += sun.position;
         
         var go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
         go.transform.position = v3;
     }
 }

The code randomly spreads the planets out along a vector and then rotates them to an arbitrary angle around the sun. 'minDist' and 'maxDist' control the minimum and maximum distance one planet may be from another. Note the initialization of 'currDist' to a value other than zero is to make sure the first planet was not too close to the sun.

Comment
Add comment · Show 1 · 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
avatar image Xentarok · Jun 01, 2013 at 09:29 PM 0
Share

Thank you for your anserws, I'm sure I know how to fix this problem now :)

avatar image
0

Answer by Memige · Jun 01, 2013 at 08:49 PM

Ok, cool, thanks for the clarification. If it is only happening occasionally, this is a standard behavior of a random number generator, you will occasionally get the same result. If you want to eliminate dupes, what you can do is register each position after you create it, then surround the random generation line with a do while loop that checks the result against your registered list. If it already exists continue, otherwise break out.

something like this:

 List<Vector2> positions = new List<Vector2>();
 while (planetcount < MaxPlanets)
 {
     Vector2 pos;
     do
     {
         pos = new Vector2(Random.Range(-260, 260), Random.Range(-260, 260));
     } while (positions.Contains(pos));
      positions.Add(pos);
      Instantiate(prefab, new Vector3(pos.x, 0, pos.y), Quaternion.Euler(0, 0, 0));
     planetcount++;
 }

Alternatively you could just compare one value (x or y) to prevent the same orbit and not just identical placement

To check just the Orbit (lets assume your Y is the distance)

 List<int> positions = new List<int>();
 while (planetcount < MaxPlanets)
 {
     Vector2 pos;
     do
     {
         pos = new Vector2(Random.Range(-260, 260), Random.Range(-260, 260));
     } while (positions.Contains(pos.y));
     positions.Add(pos.y);
     Instantiate(prefab, new Vector3(pos.x, 0, pos.y), Quaternion.Euler(0, 0, 0));
     planetcount++;
 }

To check for dupes within a given allowance, try the following:

 List<int> positions = new List<int>();
 int minDistance = 3;
 while (planetcount < MaxPlanets)
 {
     Vector2 pos;
     bool is_dupe= true;
     while(is_dupe)
     {
         pos = new Vector2(Random.Range(-260, 260), Random.Range(-260, 260));
         is_dupe = false
         foreach(int orbit in positions)
         {
             if(Mathf.Abs(orbit - pos.y) < minDistance)
             {
                 is_dupe = true;
             }
         }
     }
     positions.Add(pos.y);
     Instantiate(prefab, new Vector3(pos.x, 0, pos.y), Quaternion.Euler(0, 0, 0));
     planetcount++;
 }
Comment
Add comment · Show 7 · 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
avatar image Xentarok · Jun 01, 2013 at 08:57 PM 0
Share

Thank you for your answer, but to clarify: The generation is already in a while loop with random variable which represents the number of planets ( float NumberOfPlanets = Random.Range(1,8)). Anyway, could you please explain the "registering position and checking the result against your registered list" a little bit more? I'd be more than thankfull. And by the way, the position is always diffrent, but the distance from the star is the same.

-Xentarok

avatar image Memige · Jun 01, 2013 at 09:00 PM 0
Share

Added a second code spinet to showcase exa$$anonymous$$ing just one variable. Have a look at the edit

avatar image Memige · Jun 01, 2013 at 09:02 PM 0
Share

The idea is you have a list of all the distances that are currently being used, and every time you generate a new one, you check to see if it already exists, If it does, you discard it and generate a new one, until you get a distance not already in your list.

avatar image Xentarok · Jun 01, 2013 at 09:08 PM 0
Share

I'm sorry if I'm missing something, but what about when position is just slightly diffrent (like for example 132 and 132.5). In this case you'd still get planets "touching" each other.

And again, thank you for your answers.

-Xentarok

avatar image Memige · Jun 01, 2013 at 09:09 PM 0
Share

Not a problem :) for close things it gets a little more complicated (but only a little) gimme a sec and I'll add another snipet

Show more comments

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

17 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

Related Questions

Another Random Generation Problem 1 Answer

Spherical Light for Solar System Illumination 0 Answers

Do anyone know how im able to check if there is duplicate of the other object, than if there is change it to a different object? 1 Answer

Terrain Toolkit - License 0 Answers

Generation Problem c# 0 Answers


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