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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by megapyro · Apr 18, 2015 at 10:16 AM · mathsphere

Sphere coords, radiating from centre.

Hi, I am looking for a script or function or similar that will list all of the coordinates inside a sphere, example:

 Vector3(0, 0, 0);
 Vector3(1, 0, 0);
 Vector3(0, 1, 0);
 Vector3(0, 0, 1);
 Vector3(-1, 0, 0);
 Vector3(0, -1, 0);
 Vector3(0, 0, -1);
 Vector3(1, 1, 0);
 Vector3(1, 0, 1);
 Vector3(0, 1, 1);
 ... (skip a few)
 Vector3(4, 5, -6);
 etc...

as you can see all of the coords are ints it lists every coord it starts from the centre

I have searched google and unity answers but have only found answers about the edges of a sphere

Bonus points if you know what such a function is called Also Bonus points if you know how to adapt it for an ellipse

thanks

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
2
Best Answer

Answer by Bunny83 · Apr 18, 2015 at 10:47 AM

So if i get the question right you want all whole number-coordinates which are inside a sphere with a given radius?

In that case, just iterate over all whole-number-coordinates of the bounding box of the sphere and do an "inside check" with the formula

 x² + y² + z² < r²

You could implement it with a two method approach to keep it versatile:

 // You need a "using System.Collections.Generic;" at the top
 IEnumerable<Vector3> GetPointsInBoundingBox(Bounds aBounds)
 {
     int maxX = Mathf.RoundToInt(aBounds.max.x);
     int maxY = Mathf.RoundToInt(aBounds.max.y);
     int maxZ = Mathf.RoundToInt(aBounds.max.z);
     for(int z = Mathf.RoundToInt(aBounds.min.z); z < maxZ; z++)
         for(int y = Mathf.RoundToInt(aBounds.min.y); y < maxY; y++)
             for(int x = Mathf.RoundToInt(aBounds.min.x); x < maxX; x++)
                 yield return new Vector3(x,y,z);
 }

 IEnumerable<Vector3> GetPointsInSphere(Vector3 aCenter, float aRadius)
 {
     Bounds bounds = new Bounds(aCenter, Vector3.one * aRadius*2);
     float rSquared = aRadius*aRadius;
     foreach(var p in GetPointsInBoundingBox(bounds))
     {
         if ((p-aCenter).sqrMagnitude < rSquared)
             yield return p;
     }
 }

Now you can do something like this and you get a "blocky" filled sphere:

 foreach(var p in GetPointsInSphere(Vector3.zero, 7))
 {
     var go = GameObject.CreatePrimitive(PrimitiveType.Cube);
     go.transform.position = p;
 }

If you just want the points in a List / array, just use ToList or ToArray from the System.Linq namespace:

 // You need of course "using System.Linq;" at the top
 Vector3[] points = GetPointsInSphere(Vector3.zero, 7).ToArray();

edit

Keep in mind that i use Vector3 here for simplicity and compatibilty. If you really want them as int coordinates you might want to create your own "Vector3i" struct.

second edit

If you want the points to be sorted by the distance from the origin you can also use linq. just like this:

 Vector3[] points = GetPointsInSphere(Vector3.zero, 7).OrderBy((v)=>v.sqrMagnitude).ToArray();
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 megapyro · Apr 23, 2015 at 08:43 AM 0
Share

Thanks for the in depth answer, but alas, i fixed the issue before the moderators had filtered my question

          for (int x = chunkDistance * -1; x < chunkDistance; x++) {
             for (int y = chunkDistance * -1; y < chunkDistance; y++) {
                 for (int z = chunkDistance * -1; z < chunkDistance; z++) {
                     tempPositions.Add(new WorldPos(x, y, z));
                 }
             }
         }
          tempPositions.Sort();
          chunkPositions = tempPositions.ToArray();


This gets a list of all the positions and sorts them according to how close they are to the player

In the WorldPos Struct i put this

     public double $$anonymous$$agnitude () {
         Vector3 point = new Vector3(x, y, z);
         float distance = Vector3.Distance(point, new Vector3(0, 0, 0));
         return distance;
     }
 
     public int CompareTo(WorldPos other) {
         return (this.$$anonymous$$agnitude()).CompareTo(other.$$anonymous$$agnitude());
     }

This is what allows the tempPositions.Sort() to work

It is ordered as a sphere, but still has all the coords for a square, cutting the corners on the square is the next part im working on.

avatar image Bunny83 · Apr 24, 2015 at 03:13 AM 0
Share

@megapyro: Glad you got it working ^^. I somehow missed that you need the positions in a certain order.

btw: Your $$anonymous$$agnitude method look a bit too complicated ^^ The Vector3 struct already has a property which returns the magnitude. However just for sorting the points you don't need the exact magnitude. It's cheaper if you use the squared magnitude as it doesn't require a square root. Since you have your own $$anonymous$$agnitude method the best way would be:

 public double Sqr$$anonymous$$agnitude()
 {
     return x*x + y*y + z*z;
 }

Using that for sorting will result in the exact same order but is way easier to calculate. If your x,y and z values are actually ints you mmight want to return an int ins$$anonymous$$d of double.

ps: Just in case you didn't know, the magnitude of a vector is simply:

 $$anonymous$$athf.Sqrt(x*x + y*y + z*z)

It's simply the Pythagorean theorem in 3d.

avatar image megapyro · Apr 24, 2015 at 11:15 PM 0
Share

Thanks for that, i will definitely change that since ive been having problems with lag, however most of it seems to be from... uh, it seems to have disappeared, lol.

Im not going to question that, oh wait, massive glitchyness... !?!?!?

NV$$anonymous$$ multiple monodevelops open.

I am having trouble with Dictionary.TryGetValue taking up 60% of my total time, but im going to open a new question for that

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

2 People are following this question.

avatar image avatar image

Related Questions

Reverse of this function? 0 Answers

Radius of a Non-Uniform Sphereoid/Ellipsoid at a Given Surface Point? 1 Answer

Find position of pixel on sphere given position of pixel on texture 1 Answer

Need help with the physics and angles of gerbils in a plastic ball.. 2 Answers

How would I make a sphere with equidistant vertices? 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