Making spheres possible

Discussion in 'Plugin Development' started by TwerkinCraft, Apr 24, 2014.

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

    TwerkinCraft

    Some of you have seen my thread on making circles on my server. So now i am doing a similar thing, but with spheres and i am making it public. I got the information that is in it from http://mathworld.wolfram.com/SphericalCoordinates.html. It's still incomplete, so you guys can add to it or use it for your own porposes. Its a class for making Spherical Coordinates. I designed it to be used in a series of for loops to get a Spherical array of locations and do something with those locations.
    Code:
    package com.medievalblade.plugins.RPG;
     
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.entity.Player;
     
    public class SphericalLocation {
        private World world;
        private long distance;
        private float azumith;
        private float zenith;
       
       
       
       
        public SphericalLocation(Location a, long distance, float azumith, float zenith){
            if(azumith<0){
                Exception e = new Exception("xzenith was too low while creating CartesianLocation " + this.toString());
                System.out.println(e.getMessage());
                e.printStackTrace();
                return;
            }
            if(azumith>360){
                Exception e = new Exception("xzenith was too high while creating CartesianLocation " + this.toString());
                System.out.println(e.getMessage());
                e.printStackTrace();
                return;
            }
            if(zenith<0){
                Exception e = new Exception("zenith was too low while creating CartesianLocation " + this.toString());
                System.out.println(e.getMessage());
                e.printStackTrace();
                return;
            }
            if(zenith>360){
                Exception e = new Exception("zenith was too high while creating CartesianLocation " + this.toString());
                System.out.println(e.getMessage());
                e.printStackTrace();
                return;
            }
            this.world = a.getWorld();
            this.distance = distance;
            this.azumith = azumith;
            this.zenith = zenith;
        }
       
       
       
        public Location toLocation(Location center){
            float x,y,z;
            x = (float) (this.distance * Math.cos(this.azumith) * Math.sin(this.zenith));
            z = (float) (this.distance * Math.sin(this.azumith) * Math.sin(this.zenith));
            y = (float) (this.distance * Math.cos(this.azumith));
            Location r = new Location(center.getWorld(),
                    center.getX() + x,
                    center.getY() + y,
                    center.getZ() + z);
            return r;
        }
       
        public long getDistance(){
            return this.distance;
        }
        public void setDistance(long distance){
            this.distance = distance;
        }
        public float getAzumith(){
            return this.azumith;
        }
        public void setAzumith(float azumith){
            if(azumith<0){
                Exception e = new Exception("azumith was too low while setting azumith for CartesianLocation " + this.toString());
                System.out.println(e.getMessage());
                e.printStackTrace();
                return;
            }
            if(azumith>360){
                Exception e = new Exception("azumith was too high while setting azumith for CartesianLocation " + this.toString());
                System.out.println(e.getMessage());
                e.printStackTrace();
                return;
            }
            this.azumith = azumith;
        }
        public float getZenith(){
            return this.zenith;
        }
        public void setZenith(float zenith){
            if(zenith<0){
                Exception e = new Exception("zenith was too low while setting zenith for CartesianLocation " + this.toString());
                System.out.println(e.getMessage());
                e.printStackTrace();
                return;
            }
            if(zenith>360){
                Exception e = new Exception("zenith was too high while setting zenith for CartesianLocation " + this.toString());
                System.out.println(e.getMessage());
                e.printStackTrace();
                return;
            }
            this.zenith = zenith;
        }
        public World getWorld(){
            return this.world;
        }
    }
    
     
  2. Offline

    Slikey

    Yeah.. This is an implementation of polar-coordinates for a sphere, but way do you through an exception if angles are less than 0 or greater than 360 degree? You might also change the degrees to radians, because Java works with radians.
    double radians = Math.PI * degree / 180;

    Since sinus and cosinus can handle number to infinity, you skip that exceptions and pass them the angles directly..

    Any you might rename the angles to pitch and yaw, to fit Minecrafts handling of those angles.
     
  3. Offline

    TwerkinCraft

    Slikey ok, i'll get to editing that
     
Thread Status:
Not open for further replies.

Share This Page