- Home /
how do i generate cubes to form a sphere?
im making a game like minecraft and i have a script that generates cubes for a floor but i cant fix it so that the cubes size are 0.005 and im trying to make them generate a sphere out of cubes kinda like world edit on minecraft any help will be appreciated
for (var y = -4; y < 5; ++y) for (var x = -4; x < 5; ++x) { var cube : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position = transform.position + Vector3(x, 0, y); }
Answer by aldonaletto · May 26, 2012 at 05:00 PM
A simple (but somewhat stupid) way to create a hollow sphere with axis aligned blocks is to generate coordinates for a cube with the same sphere size, but only instantiate the ones that are at the radius distance:
function CreateSphere(center: Vector3, blockSize: float, radius: float){ var qBlocks: int = Mathf.Ceil(radius/blockSize); // how many blocks fit in the radius var pos: Vector3; for (var ix: int =-qBlocks; ix
function Start () { // create sphere at this object position: CreateSphere(transform.position, 0.05, 1); } The yield instruction added after the y loop is intended to not block Unity: each YZ wall will be instantiated each frame.
If you need a solid sphere, replace the if clause to:
if (pos.magnitude <= radius){ // if block inside radius...
But solid spheres take a lot of time to be created, and another big time to be rendered - the framerate drops like a rock when the sphere enters the camera frustum.
As @Bunny83 said, the enormous amount of game objects generated for a simple sphere is a big performance killer! Depending on what you want to do, creating a procedural mesh (like @Berenger suggested) with the algorithm above (or another less stupid one) may be the right solution.
Answer by Bunny83 · May 26, 2012 at 03:39 PM
You can't use seperate gameobjects for each cube. That will kill your performance. You should take a look at the minecraft starter kit. Minecraft is one of the most complex games that have been released recently. It uses very advanced technologies. It's easier to create a new Crysis or BioShock like game from a programming point of view. You should have really good knowledge of math, algorithms and how the graphics hardware works, otherwise you won't get very far...
Answer by Berenger · May 26, 2012 at 03:09 PM
I suggest you take a look at how a sphere's mesh is procedurally created in 3D modelling programs, as what you want is quite close.
A quick google search gave me those :
http://stackoverflow.com/questions/4081898/procedurally-generate-a-sphere-mesh
http://paulbourke.net/miscellaneous/sphere_cylinder/
http://www.gamedev.net/topic/537269-procedural-sphere-creation/
Answer by francecoyle · Jul 26, 2020 at 01:25 PM
Spherical brush will create a ball of blocks specified with [id:meta], of size defined with radius and minecraft circle . As diameter is radius times two and the minimum diameter of a brush is 1, [radius] of 3 will give you a seven blocks wide ball. In addition to [radius], cylinder brush also accepts a height value (If you don't define a height, you'll get a one block thick disc).
So, "//br cyl 35:0 3 4" will create a 7 blocks wide, 4 blocks tall cylinder of white wool, with the center of the bottom layer on the block where you click with the brush.
This is very useful as it can work for creating circles. but when you reduce the radius the $$anonymous$$ecraft circle start expanding like egg from two side but from the upper side its decreasing.
Your answer
Follow this Question
Related Questions
Minecraft like building system 0 Answers
Minecraft In Unity, Coal and Iron 1 Answer
Is minecraft made of cubes? 4 Answers
Some questions about random world generation 1 Answer
Anyone got a Voxel generator I can use? 0 Answers