Solved Outline a block with particles

Discussion in 'Spigot Plugin Development' started by Fliens, Feb 29, 2020.

  1. Hi, i need some help with a method that should outline a block.

    I know how to spawn particles the thing i'am struggling with is getting the right coordinates.

    The particles should be spawned at the edges of the block so it should basically look like this:
    [​IMG]

    I've tried it with 3 for loops so one for x, y, z and some conditions like when z==0||z==1 but that wasn't working at all
     
  2. SteelPhoenix

    Moderator

    Just iterate over all the edges?
     
  3. When you want to work with that much precision use zecimals,(0,01 should lead to the closest point to that corner and so on..)
     
  4. yeah that's basically it but i was wondering if i could do it without 8 for loops (1 for each edge) but with 3 for loops so one for x, y and z
     
  5. What was the result of that?
     
  6. Code (Text):
    for(double y = 0;y<=1;y+=0.1) {
                                                for(double x = 0;x<=1;x+=0.1) {
                                                    for(double z = 0;z<=1;z+=0.1) {
                                                        if(y==0||y==1) {
                                                            if(x==0||x==1) {
                                                                block.getLocation().getWorld().spawnParticle(Particle.REDSTONE, autocrafter.getLocation().getX()+x, autocrafter.getLocation().getY()+y, autocrafter.getLocation().getZ()+z, 0, 0.001, 1, 0, 1, new Particle.DustOptions(Color.GREEN, 1));
                                                            }
                                                        }
                                                    }
                                                }
                                            }
    it drew a particle line at the bottom
     
  7. SteelPhoenix

    Moderator

    for an edge at least two of the three dimensions have to be at either the maximum of minimum

    e.g.
    Code (Text):
    bool a = min x == x coord || x coord == max x
    bool b = min y == y coord || y coord == max y
    bool c = min z == z coord || z coord == max z
    bool isEdge = a ? (b || c) : (b && c)
     
    • Winner Winner x 1
  8. Code (Text):
    int max = 100;
                                            for(int x = 0;x<=max;x++) {
                                                for(int y = 0;y<=max;y++) {
                                                    for(int z = 0;z<=max;z++) {
                                                        boolean a = 0 == x || x == max;
                                                        boolean b = 0 == y || y == max;
                                                        boolean c = 0 == z || z == max;
                                                        boolean isEdge = a ? (b || c) : (b && c);
                                                        if(isEdge)
                                                            block.getLocation().getWorld().spawnParticle(Particle.REDSTONE, autocrafter.getLocation().getX()+(double)(x/max), autocrafter.getLocation().getY()+(double)(y/max), autocrafter.getLocation().getZ()+(double)(z/max), 0, 0.001, 1, 0, 1, new Particle.DustOptions(Color.GREEN, 1));
                                                    }
                                                }
                                            }
    I've tried it like this but it seems like particles are only spawning at the corners :/
     
  9. Here the code I use to outline blocks individually. If you want to outline a bunch of blocks in a cuboid region then just add an argument to the function that sets the max X, Y, Z to the second location. Particle distance is the distance at which it makes a particle, so 0.5 would do all corners and 1 in between each corner, or do 1 for JUST corners, etc. I'm sure u get the gist. Then just spawn a particle at each location it treturns.

    Code (Java):
    public List<Location> getHollowCube(Location loc, double particleDistance) {
            List<Location> result = Lists.newArrayList();
            World world = loc.getWorld();
            double minX = loc.getBlockX();
            double minY = loc.getBlockY();
            double minZ = loc.getBlockZ();
            double maxX = loc.getBlockX()+1;
            double maxY = loc.getBlockY()+1;
            double maxZ = loc.getBlockZ()+1;

            for (double x = minX; x <= maxX; x+=particleDistance) {
                for (double y = minY; y <= maxY; y+=particleDistance) {
                    for (double z = minZ; z <= maxZ; z+=particleDistance) {
                        int components = 0;
                        if (x == minX || x == maxX) components++;
                        if (y == minY || y == maxY) components++;
                        if (z == minZ || z == maxZ) components++;
                        if (components >= 2) {
                            result.add(new Location(world, x, y, z));
                        }
                    }
                }
            }
            return result;
        }
     
    • Like Like x 2
  10. Thank you, that's absolutely perfect and exactly what i was looking for :D
     
  11. It seems like it's not possible to make the particleDistance lower than 0.5 so for example like 0.2?
     
  12. it should work just fine, i use 0.25
     
  13. you're right but somehow 0.2 does not work. But however im now using 0.25 aswell :)
     
  14. I've improved it so it should no also work with 0.2 or 0.05 ...

    Code (Text):
    public List<Location> getHollowCube(Location loc, double particleDistance) {
            List<Location> result = Lists.newArrayList();
            World world = loc.getWorld();
            double minX = loc.getBlockX();
            double minY = loc.getBlockY();
            double minZ = loc.getBlockZ();
            double maxX = loc.getBlockX()+1;
            double maxY = loc.getBlockY()+1;
            double maxZ = loc.getBlockZ()+1;

           for (double x = minX; x <= maxX; x = Math.round((x + particleDistance) * 1e2) / 1e2) {
                for (double y = minY; y <= maxY; y = Math.round((y + particleDistance) * 1e2) / 1e2) {
                    for (double z = minZ; z <= maxZ; z = Math.round((z + particleDistance) * 1e2) / 1e2) {
                        int components = 0;
                        if (x == minX || x == maxX) components++;
                        if (y == minY || y == maxY) components++;
                        if (z == minZ || z == maxZ) components++;
                        if (components >= 2) {
                            result.add(new Location(world, x, y, z));
                        }
                    }
                }
            }
            return result;
        }