Spheres, Hollow Sphers, Circles, Hollow Circles Tutorial

Discussion in 'Plugin Development' started by Jack Price-Burns, Aug 25, 2014.

Thread Status:
Not open for further replies.
  1. Hello,

    I had some serious problems when coding my plugin because I didn't have a clue how to make hollow spheres or circles but I have a method that works amazingly.

    Here is my code to make a set of locations hollow

    Code:java
    1. private Set<Location> makeHollow(Set<Location> blocks, boolean sphere){
    2. Set<Location> edge = new HashSet<Location>();
    3. if(!sphere){
    4. for(Location l : blocks){
    5. World w = l.getWorld();
    6. int X = l.getBlockX();
    7. int Y = l.getBlockY();
    8. int Z = l.getBlockZ();
    9. Location front = new Location(w, X + 1, Y, Z);
    10. Location back = new Location(w, X - 1, Y, Z);
    11. Location left = new Location(w, X, Y, Z + 1);
    12. Location right = new Location(w, X, Y, Z - 1);
    13. if(!(blocks.contains(front) && blocks.contains(back) && blocks.contains(left) && blocks.contains(right))){
    14. edge.add(l);
    15. }
    16. }
    17. return edge;
    18. } else {
    19. for(Location l : blocks){
    20. World w = l.getWorld();
    21. int X = l.getBlockX();
    22. int Y = l.getBlockY();
    23. int Z = l.getBlockZ();
    24. Location front = new Location(w, X + 1, Y, Z);
    25. Location back = new Location(w, X - 1, Y, Z);
    26. Location left = new Location(w, X, Y, Z + 1);
    27. Location right = new Location(w, X, Y, Z - 1);
    28. Location top = new Location(w, X, Y + 1, Z);
    29. Location bottom = new Location(w, X, Y - 1, Z);
    30. if(!(blocks.contains(front) && blocks.contains(back) && blocks.contains(left) && blocks.contains(right) && blocks.contains(top) && blocks.contains(bottom))){
    31. edge.add(l);
    32. }
    33. }
    34. return edge;
    35. }
    36. }


    Here is my code to make a cylinder, hollow or not

    Code:java
    1. public Set<Location> circle(Location location, int radius, boolean hollow){
    2. Set<Location> blocks = new HashSet<Location>();
    3. World world = location.getWorld();
    4. int X = location.getBlockX();
    5. int Y = location.getBlockY();
    6. int Z = location.getBlockZ();
    7. int radiusSquared = radius * radius;
    8.  
    9. if(hollow){
    10. for (int x = X - radius; x <= X + radius; x++) {
    11. for (int z = Z - radius; z <= Z + radius; z++) {
    12. if ((X - x) * (X - x) + (Z - z) * (Z - z) <= radiusSquared) {
    13. Location block = new Location(world, x, Y, z);
    14. blocks.add(block);
    15. }
    16. }
    17. }
    18. return makeHollow(blocks, false);
    19. } else {
    20. for (int x = X - radius; x <= X + radius; x++) {
    21. for (int z = Z - radius; z <= Z + radius; z++) {
    22. if ((X - x) * (X - x) + (Z - z) * (Z - z) <= radiusSquared) {
    23. Location block = new Location(world, x, Y, z);
    24. blocks.add(block);
    25. }
    26. }
    27. }
    28. return blocks;
    29. }
    30. }


    and here is my code for a sphere

    Code:java
    1. public Set<Location> sphere(Location location, int radius, boolean hollow){
    2. Set<Location> blocks = new HashSet<Location>();
    3. World world = location.getWorld();
    4. int X = location.getBlockX();
    5. int Y = location.getBlockY();
    6. int Z = location.getBlockZ();
    7. int radiusSquared = radius * radius;
    8.  
    9. if(hollow){
    10. for (int x = X - radius; x <= X + radius; x++) {
    11. for (int y = Y - radius; y <= Y + radius; y++) {
    12. for (int z = Z - radius; z <= Z + radius; z++) {
    13. if ((X - x) * (X - x) + (Y - y) * (Y - y) + (Z - z) * (Z - z) <= radiusSquared) {
    14. Location block = new Location(world, x, y, z);
    15. blocks.add(block);
    16. }
    17. }
    18. }
    19. }
    20. return makeHollow(blocks, true);
    21. } else {
    22. for (int x = X - radius; x <= X + radius; x++) {
    23. for (int y = Y - radius; y <= Y + radius; y++) {
    24. for (int z = Z - radius; z <= Z + radius; z++) {
    25. if ((X - x) * (X - x) + (Y - y) * (Y - y) + (Z - z) * (Z - z) <= radiusSquared) {
    26. Location block = new Location(world, x, y, z);
    27. blocks.add(block);
    28. }
    29. }
    30. }
    31. }
    32. return blocks;
    33. }
    34. }


    How this code works is, it creates a list of all the blocks that need something done to.

    You can use the code like this

    Code:java
    1. for(Location location : sphere(player.getLocation(), 7, true)){
    2. location.getBlock().setType(Material.GOLD_BLOCK);
    3. }


    The following code would make a sphere 7 big which is hollow around the player :D Hope my code helps you out and if anyone can find a way to improve my code that would be very handy :D
     
  2. Offline

    Marten Mooij

  3. Offline

    nuno1212sss

    Jack Price-Burns First of all, I recommend you to stop location.add(x,y,z) and use vector's instead( this is just my thing but it always worked perfectly), second to make a sphere hollow you can add this if((loc.distance(middle)<radius+1)&&(loc.distance(middle)>radius-1))

    Marten Mooij That also works xD :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  4. Offline

    AoH_Ruthless

    Jack Price-Burns
    A few optimization notes.
    • When you define front, back, end, etc.. you could do l.add(1,0,0), l.add(-1,0,0), etc. This will shorten your code just a tad.
    • In you makeHollow method, the two for loops are almost identical. You could condense your method easily by having one for loop with the if-then-else statement inside the loop.
    • You might want to look into making this not use looping through x, y and z values. This can be server intensive if many objects or large ones are being generated. Other tutorials already feature this. I have heard that vector math is less intensive, but I could be wrong on that note (I've never tested it).
    Otherwise, it's a decent tutorial, albeit there is no explanation at all for your code. The source alone is not a tutorial. You need to explain your steps and why you did what you did.
     
  5. Offline

    Marten Mooij

  6. Offline

    L33m4n123

    Calling it a tutorial and not even a single comment in your code.
     
  7. Offline

    fireblast709

    Without Vector objects you could have less overhead caused by all object creation.
    What exactly do you mean by vector math? I wonder about your solution to find all blocks on the 'edge' of the sphere.
     
Thread Status:
Not open for further replies.

Share This Page