I am making a game in Unity, coding in c#. The world is built from cubes (similar to minecraft), I store them in a 3D array of ints, whose values are later used to instantiate the actual cubes. I am working on filling the 3D array with different shapes. My knowledge of math is not very in depth and so I am having trouble with plotting the points of a sphere in the 3D array. Does anyone know an algorithm I could use?
Asked
Active
Viewed 427 times
0
-
1see this [Drawing 3D sphere in C/C++](https://stackoverflow.com/a/25135125/2521214) – Spektre May 23 '17 at 05:38
1 Answers
3
If you want to build a sphere out of cubes, its pretty easy. Sphere is actually the simplest 3D object out there. You need to have a float radius and a Vector3 position. Than every cube with its distance from the origin lower than the radius should be there. Example:
foreach(Transform cube in cubes[]){
if(Vector3.Distance(cube.position, origin) < radius){
//This cube should be inside the sphere
}
}

Dávid Florek
- 552
- 6
- 17
-
This is incorrect. The question requires us to provide positions of cube that could be put together to form a sphere. Your answer simply checks for whether a cube is inside a sphere at origin with given radius. – Farhan May 23 '17 at 06:58
-
Maybe I was sort of unclear with my question, but this worked brilliantly for my game. I do not have enough rep to upvote your answer, but I will one day. Thank you very much – fudgeBreadstein May 23 '17 at 07:32
-
@Farhan OP stated they are bad in math, so I explained to them how spheres actually work. I gave them an example. What did I explained incorrectly? – Dávid Florek May 23 '17 at 07:37
-
The explanation is correct, the solution though doesn't answer the question. It asked for how you'd plot cubes such that they form a sphere, and then how you'd place the positions in an array. Your answer already assumes an array and checks whether the cube at a given position lies inside a sphere. – Farhan May 23 '17 at 07:47
-
@Farhan This answer is fine. The Asker didn't provide enough information to say anything about *where* the sphere should be or *what values in the int array* indicate a block of one material versus another. The array *has* to be assumed to start with because it needs to be modified in order to contain meaningful information. The *only* thing that might be wrong about this is the nature of that array, but there's enough here for a reasonably intelligent individual to convert it to their own project. – Draco18s no longer trusts SE May 24 '17 at 03:42