[SOLVED]Is My Sphere Math Bad? - I Need a Fresh Pair of Eyes...

Discussion in 'Plugin Development' started by DragonSoulSong, Feb 22, 2012.

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

    DragonSoulSong

    I'm trying to use sphere math to create a "circle" of fire, but for some reason I seem to be getting a quarter of a circle no matter how hard I try. As far as I can tell, my sphere math is perfectly fine, but apparently not. Perhaps a fresh pair of eyes can discern the potential problem:
    Code:java
    1. /*Checks if the block at blockLoc, in the world that player is in, is an air block, and if it is, sets that block to fire.*/
    2. public static void ignite(Location blockLoc) {
    3.  
    4. //Get the block at blockLoc.
    5. Block block = blockLoc.getWorld().getBlockAt(blockLoc);
    6.  
    7. //If the block at blockLoc is air, set it to fire.
    8. if (block.getType() == Material.AIR) {
    9.  
    10. block.setType(Material.FIRE);
    11. }
    12. }
    13.  
    14. /*Calls the ignite(blockLoc, player) method on all points within a sphere of a radius represented by the double parameter "radius",
    15. * and centered on the Location parameter "center" in the world that player is in.*/
    16. public static void sphereBurn(Location center, double radius) {
    17.  
    18. radius += 0.5;
    19. //Get the square of the radius.
    20. final double radSquare = square(radius);
    21.  
    22. //Get the mathematical ceiling of radius.
    23. final int radCeil = (int) Math.ceil(radius);
    24.  
    25. //Get the center's coords.
    26. final double centerX = center.getX();
    27. final double centerY = center.getY();
    28. final double centerZ = center.getZ();
    29.  
    30. //Loop through all points inside of a sphere described by the given parameters.
    31. for(double x = centerX - radCeil; x <= centerX + radCeil; x++) {
    32.  
    33. for(double y = centerY - radCeil; y <= centerY + radCeil; y++) {
    34.  
    35. for(double z = centerZ - radCeil; z <= centerZ + radCeil; z++) {
    36.  
    37. //Get the square of the distance between the sphere center and the current point.
    38. double distSquare = square(x - centerX) + square(y - centerY) + square(z - centerZ);
    39.  
    40. //If the square of the distance to the current point is greater than the square of the radius, skip forward to the next point.
    41. if (distSquare > radSquare) {
    42.  
    43. continue;
    44. }
    45.  
    46. Location currPoint = new Location(center.getWorld(), x, y, z);
    47.  
    48. //Ignite the current point.
    49. ignite(currPoint);
    50. }
    51. }
    52. }
    53. }
     
  2. Offline

    vildaberper

    Code:
    double distSquare = Math.sqrt(square(x - centerX) + square(y - centerY) + square(z - centerZ));
     
  3. Offline

    DragonSoulSong

    If you put the square root in there, it returns the distance, which is not what I want. I want the square of the distance (which the variable name implies) as I am comparing it to the square of the radius.

    The idea being that if you loop through all the points in a cube of side length radius*2 that is centered on the point 'center', then all points in that cube that satisfy the condition (x - centerX)^2 + (y - CenterY)^2 + (z - centerZ)^2 <= radSquare will be on or inside of the desired sphere with a radius of 'radCeil' and a center of 'center'.

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

    s1mpl3x

  5. Offline

    Shamebot

  6. Offline

    nisovin

    I just copied your code into a test project and it seems to work fine. It creates a circle of fire centered at the location specified.
     
  7. Offline

    DragonSoulSong

    Whoa! That's weird...then again, the weirder thing is that I recently rewrote the code because I had this same problem with the old algorithm...but my sphere code was all messed up, so that's why. So I rewrote it from scratch with a correct algorithm this time. Even weirder: I tried replacing the Material.FIRE bit in ignite() with Material.GLASS as a test, and it still placed fire when I re-exported the plugin and reloaded my server...mabey Eclipse is screwing up somehow... :confused: Man...if this is an Eclipse issue...I don't know how I'll fix this! I haven't had any problems with other plugins that I'm currently developing...odd...perhaps I could try starting a new project and putting all of the existing code into it, then exporting...I'll try that and let you know how it goes.

    EDIT: If you're wondering about what I mean about "the old algorithm", just check out the mainLib.java in the source code for Weapons_of_Legend.

    Ok, so I moved all of my code into a new project, re-exported, and reloaded my server and everything works fine now...not sure what that was about. Like I said probably Eclipse being weird. *shrug*

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

Share This Page