- Home /
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
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();
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.
@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.
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
Follow this Question
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