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 EVOin3D · Feb 01, 2014 at 01:26 PM · randomgeneration

How to randomly generate non-intersecting cylinders within a circle?

I posted this question a while ago but unfortunately I've been unable to solve it with the answers provided. Also, I've had a look on Stack Overflow for similar questions but they're way over my head. So apologies for creating a new thread, but I'm hoping for some more suggestions.

Basically, I want to randomly generate a bunch of non-intersecting cylinders of varying size within a circle, much like an Ishihara colour test:

alt text

Colour is unimportant, I just need to be able to define the minimum distance between each circle. Any help is much appreciated.

Here's my code so far:

 public Transform circle;
 public int numberOfCircles;
 public int circleSize;
 
 void Start()
 {
     for (int i = 0; i < numberOfCircles; i++)
     {    
         circleSize = Random.Range(1, 4);
             
         circle.localScale = new Vector3(1 * circleSize, 1, 1 * circleSize);
 
         Vector2 pos = Random.insideUnitCircle * 10;
             
         Vector3 newPos = new Vector3(pos.x, 0, pos.y);
             
         Instantiate(circle, newPos, Quaternion.identity);
 
     }
 }

And here's an example result, with 25 circles.:

alt text

As you can see, there's a lot of intersecting, and the circles don't really form the shape of a larger one.

I suppose I could just instantiate a hundred or so circles then destroy them until there is none intersecting, but I'm sure there is a more elegant solution.

screen shot 2014-02-02 at 5.20.13 pm.png (21.0 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Feb 01, 2014 at 06:27 PM

This a standard algorithm/math question. StackOverflow is probably the best place for it.

It's a difficult problem. In your image, note how the largest size is scattered evenly. Do you need that? And the next smaller size is 1/2 of the largest. Do you want that, or prefer random sizes? Do you need the tiny dots? What is the least amount of space where you need to add a tiny dot? In general, what ratio/distribution of large to small dots do you want?

I think the answers are confusing because the problem is hard and has lots of ways to do it.

Once you have a list of Vector3's and sizes, the Unity part to spawn them is easy.

For fun, a "Unity" way might be to make a half-circle collider (mesh, or build from many boxes.) Then spawn a pile of random cylinders and let them fall in Once they settle down, record positions.

Comment
Add comment · Show 3 · 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 EVOin3D · Feb 02, 2014 at 06:11 AM 0
Share

Thanks Owen.

In your image, note how the largest size is scattered evenly. Do you need that?

No, I just need each circle to be at least a certain distance from one another.

And the next smaller size is 1/2 of the largest. Do you want that, or prefer random sizes?

At the moment, I'm Instantiating cylinders of three sizes.

In general, what ratio/distribution of large to small dots do you want?

Ideally, I would like for it to be a fairly even split.

avatar image Owen-Reynolds · Feb 02, 2014 at 02:13 PM 0
Share

$$anonymous$$y point is, you still have to refine the question before it can be answered (do you need three exact sizes, or would you be happy with shrinking/growing each to fill space?) It would take 1/2-hour to try your "spawn 100+" idea (and why not test with overlapSphere before placing?) You aren't going to use up your computer's weekly cylinder quota.

Once you get more and more into it, and understood the various issues (is it extra important to avoid gaps on the edge, to make sure the whole thing looks round? ...) Then you'd probably go back to the StackOverflow answers you got and they would make a lot more sense.

avatar image EVOin3D · Feb 03, 2014 at 08:35 AM 0
Share

Thanks again, Owen. I don't know how to make it much clearer, but I'll try my best.

do you need three exact sizes, or would you be happy with shrinking/growing each to fill space?

I don't $$anonymous$$d scaling each to fill space, so long as they're within a defined range.

One method I've thought of is instantiating a circle at a random point in the circle, then instantiating each circle thereafter the maximum distance from the existing circle(s), until there is no possible spaces (the $$anonymous$$imum distance cannot be reached). That way, the circles would form the shape of a larger one.

I think this would work, but again, I don't know where to begin. I guess my question then is how to instantiate an object the maximum possible distance (within a circle) away from the existing ones.

The end result I'm after is something like this:

alt text

screen shot 2014-02-03 at 7.31.09 pm.png (28.2 kB)
avatar image
0

Answer by Kiloblargh · Feb 03, 2014 at 09:20 AM

Just instantiate them all randomly, but then for each circle, get a list of transforms of the other circles that are within a certain distance. Then loop through each and apply a repelling force away from the circle inversely proportional to the distance if the distance is too close, and an attracting force toward it directionally proportional to the distance if the distance is too far. Keep doing it every frame and they will settle into a stable pattern within a few seconds.

Comment
Add comment · Show 2 · 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 Kiloblargh · Feb 03, 2014 at 09:23 AM 0
Share

( That approach will work perfectly for the number and density of circles you posted in your screen shot; to do it for as many as the color vision test in your original post, not so much. )

avatar image EVOin3D · Feb 03, 2014 at 11:13 AM 0
Share

Thanks, I'll give it a try.

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Help with Generating Random Tiles 1 Answer

Changing Objects Material Using My Array 1 Answer

Randomly Generated Levels 3 Answers

Help with spawning an object in a random location 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