Solved Getting sphere of locations with radius and center point

Discussion in 'Plugin Development' started by woutwoot, May 6, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    woutwoot

    Hello Everyone,

    I'm currently working on a plugin where I want to play particle effects in a sphere around the player. I know this can be done, but I don't really know the math needed to calculate this.

    First you would have to find a way to describe a sphere using math on 3D locations, and then find a way to divide it into locations a half a block (or less) from each other. How can I achieve this?

    Wout

    Nevermind! I figured it out by myself! (Well, found it on the forums somewhere) Here is the code:
    Code:java
    1. public static List<Location> circle(Location loc, Integer r, Integer h, Boolean hollow, Boolean sphere, int plus_y) {
    2. List<Location> circleblocks = new ArrayList<Location>();
    3. int cx = loc.getBlockX();
    4. int cy = loc.getBlockY();
    5. int cz = loc.getBlockZ();
    6. for (int x = cx - r; x <= cx + r; x++)
    7. for (int z = cz - r; z <= cz + r; z++)
    8. for (int y = (sphere ? cy - r : cy); y < (sphere ? cy + r : cy + h); y++) {
    9. double dist = (cx - x) * (cx - x) + (cz - z) * (cz - z) + (sphere ? (cy - y) * (cy - y) : 0);
    10. if (dist < r * r && !(hollow && dist < (r - 1) * (r - 1))) {
    11. Location l = new Location(loc.getWorld(), x, y + plus_y, z);
    12. circleblocks.add(l);
    13. }
    14. }
    15.  
    16. return circleblocks;
    17. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
Thread Status:
Not open for further replies.

Share This Page