#sphere generation for blocks

1 messages ยท Page 1 of 1 (latest)

sand ice
#

hello, if anyone joins/reads this and you're good at math, or spigot devleopment pls read

#

i've been trying to make a sphere generator, for some build utilities, i just dont want to use worldedit because... its not that lightweight of a plugin, right

#

so i got some code offline and i editted it a bit, however... after my edit i realized that the code would skip certain blocks

#
public static List<Block> getSphere(Location loc, boolean hollow, int radius, int height){
        List<Block> blocks = new ArrayList<Block>();
        
        int bx = loc.getBlockX();
        int by = loc.getBlockY();
        int bz = loc.getBlockZ();

        for(int x = bx - radius; x <= bx + radius; x++){
            for(int y = by - height; y <= by + height; y++){
                for(int z = bz - radius; z <= bz + radius; z++){
                    double distance = ((bx-x) * (bx-x) + ((bz-z) * (bz-z)) + ((by-y) * (by-y)));                    
                    if(distance < height * radius && !(hollow && distance < ((height - 1) * (radius - 1)))) blocks.add(new Location(loc.getWorld(), x, y, z).getBlock());
                }
            }
        }
        return blocks;
    }
``` code (i added 'int height')
#

and heres what happens when i use radius 15, height 5

#

ignore the giant sphere in the background XD

#

and im not 1000% sure how threads work, so just ping me if you answer

solar ingot
#

You should not be specifying a height. a sphere uses radius all around.

#

@sand ice

sand ice
#

wait WE is open source, 2 seconds

solar ingot
#

you can set a height in WE?

#

if you can provide a height I'd expect it to be a scalar

#

ie, you reduce the radius for bx by the scalar

sand ice
solar ingot
#

then thats not a radius

sand ice
#

and im really not sure how to do that, im gonna continue google research ๐Ÿง 

sand ice
solar ingot
#

nope, looks like only radius

#

cyl takes a radius and a height

sand ice
#

wtffff

solar ingot
#

expanding any one dimension is not hard, so long as you don;t go below the radius

sand ice
solar ingot
#

you can go radius + a scalar, but not under the radius

sand ice
#

radius + scalar

#

is the "height"?

solar ingot
#

its + radius + scalar

#

or height if you like

#

2 dimensions have to be the same (radius) the thrird can expand

sand ice
#

okay

solar ingot
#

well you could do 2 I guess

#

but the math is going to be a nightmare

sand ice
#

its already a nightmare :pepeWhy:

solar ingot
#

๐Ÿ˜„

#

ok for starters, change to using a Set instead of a List, and store the Blocks not locations

sand ice
#

yeah i store blocks

#

and i'll use a Set, may i ask what the difference is with Sets

solar ingot
#

nah you use Locations blocks.add(new Location(loc.getWorld(), x, y, z).getBlock());

#

oh you do getBlock ๐Ÿ™‚

#

Set will not allow duplicates

sand ice
#

ahh

solar ingot
#

also change your loops to use double instead of ints

#

then you can change your granularity

#

currently you are jumping a full block at a time. which means as you expand height its going to miss blocks

sand ice
#

yikes

solar ingot
#

but that does include all the rendering

sand ice
#

yeah

#

ahh @solar ingot here's this

#

radius radius radius

#

x,y,z

#

length,height,length basicaly

#

if only the host was working ๐Ÿคฌ