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 J_Troher · Apr 04, 2015 at 03:39 AM · collisioninstantiaterigidbody-collisionrandomspawning

Trying to spawn a bunch of spheres in a sphere

I am trying to use random unitinsidesphere, to spawn some gameobjects that are Non-Kinematic and have sphere colliders on them. I want to spawn them in this visual sphere I have (no collider), which, obviously is no problem.

But I don't want them to be able to spawn in eachother. Now, a way I've done a "spawncheck" on something else, was to use an empty gameobject with a non kin rigidbody and a collider, move it to where I am trying to spawn, and check if anything is in the collider. If not, sure, if not find another spot in that area, until you get a good spot.

I was thinking of doing the same thing here, but I want to know really I guess if that is even optimal. If not, does anybody have a better way to do it?

-Thank in Advance!

Comment
Add comment · Show 1
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 _Kyle_1 · Apr 04, 2015 at 06:13 AM 1
Share

The way you have it is probably the best way you can get it because you are using Random.insideUnitSphere. I would put a "give up" escape after so many spawn tries to try to lower any possible lag.

You could just implement your spawncheck in to the GameObject when you try to spawn it and kill it if there is no room, that way you can just skip a step.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Huacanacha · Apr 04, 2015 at 06:58 AM

If you are only concerned about the spheres overlapping each other I can think of three approaches:

  1. Your current way checking colliders. Instead of using an empty game object just spawn the sphere and move it if it collides, until you find an empty spot. Performance will degrade as the space fills up... to optimise a little you can try to 'repair' positions by moving back a.radius+b.radius in a-b direction... if no collision use that spot otherwise generate a new random position.

  2. If the spheres you are creating are of equal radius: as you create them store the origins in a list. New positions must be > 2xRadius from all points in this list. If it isn't you can first try repair => move the point back along the origin->origin vector by the overlap distance (or a random amount >= the overlap distance) then check vs origin list again. When a suitable position is found create the game object. If you have a lot of objects to place you can use a quadtree to spatially subdivide and only check against existing objects in neighboring quadrants.

  3. 3D hex grid for the win! Divide your spherical bounds into a grid of origins 2r distance from each other (think marbles completely filling a round glass bowl). Then you need only a random integer [0,grid size) and can place your spheres guaranteeing no collision by blocking populated indexes (again you can repair by moving in a random direction until a free slot if there is a clash). This is also only suitable if the spheres you are spawning are of equal radius. It also results in a more regular positioning that isn't as purely random, although this will not be very obvious for sparse arrangements.

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

21 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

Related Questions

Network.Destroy and collision; destroying correct object. 1 Answer

Physic2D.overlapCircle Instantiation 0 Answers

Affect only one clone from multiple clones? 1 Answer

[SOLVED] Destroy instantiated object prefab on collision 1 Answer

Rigidbody slightly intrudes another one while pushing it 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