How to get all Blocks within a sphere?

Discussion in 'Spigot Plugin Development' started by VirusBrandon, Apr 15, 2017.

  1. Good Afternoon all,

    What is the most efficient function for getting
    all blocks within a sphere/radius in all directions? ( Making black hole block suck effect :p )

    I already have the vector calculation for making an object fly in the direction
    of Location 1 (block to pull in ) looking at Location 2 ( center of black hole ), now I just need the location of the blocks
    that will be pulled in by the black hole.

    Thanks in advance!
     
  2. Something like this
    https://www.spigotmc.org/threads/creating-a-sphere.146338/
    Instead of returning x y z, return a new Location.
     
  3. Yes I was looking for the thread and opened another app to respond to something before I clicked "Post Reply"

    Also I was looking for my SphereUtils class in one of my plugins on GitHub but could not find it

    Not really trying to race with anyone...
     
    #3 Synapz, Apr 15, 2017
    Last edited: Apr 15, 2017
  4. Thanks all.
    It's not quite what I'm looking looking for.

    This creates a sphere of particles, but I am
    trying to get all blocks in a sphere. The functions
    linked are surface only sphere functions, which would
    leave blocks untouched.
     
  5. That gives you the math. Instead of storing the x y and z, you just use new Location(x,y,z);

    Edit:
    Here:
    Code (Text):
        public static List<Location> generateSphere(Location centerBlock, int radius, boolean hollow) {
            if (centerBlock == null) {
                return new ArrayList<>();
            }

            List<Location> circleBlocks = new ArrayList<Location>();

            int bx = centerBlock.getBlockX();
            int by = centerBlock.getBlockY();
            int bz = centerBlock.getBlockZ();

            for(int x = bx - radius; x <= bx + radius; x++) {
                for(int y = by - radius; y <= by + radius; 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 < radius * radius && !(hollow && distance < ((radius - 1) * (radius - 1)))) {

                            Location l = new Location(centerBlock.getWorld(), x, y, z);

                            circleBlocks.add(l);

                        }

                    }
                }
            }

            return circleBlocks;
        }
     
    Use the other resource to understand be math behind it.
     
  6. Is that the only way to do it?

    Code efficiency of O(n^3) makes me cringe...