Advertisement
StarShadow

My Ray class

Aug 22nd, 2016
2,911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. import org.bukkit.Location;
  2. import org.bukkit.entity.Player;
  3. import org.bukkit.util.Vector;
  4.  
  5. // Just a ray class I made with useful methods I needed.
  6.  
  7. public class Ray {
  8.    
  9.     private Vector origin, direction;
  10.    
  11.     // Create a ray at the origin pointing in a direction.
  12.     public Ray(Vector origin, Vector direction) {
  13.         this.origin = origin;
  14.         this.direction = direction;
  15.     }
  16.    
  17.     // Create a ray based on where the player is looking.
  18.     // Origin: Player Eye Location
  19.     // Direction: Player-looking direction
  20.     public static Ray from(Player player) {
  21.         return new Ray(player.getEyeLocation().toVector(), player.getLocation().getDirection());
  22.     }
  23.    
  24.     // (Used for rotating vectors) Creates a vector in the horizontal plane (y=0) perpendicular to a vector.
  25.     public static Vector right(Vector vector) {
  26.         Vector n = vector.clone();
  27.         n = n.setY(0).normalize();
  28.         double x = n.getX();
  29.         n.setX(n.getZ());
  30.         n.setZ(-x);
  31.         return n;
  32.     }
  33.  
  34.     // Returns a normalized version of this Ray with the Y component set to 0
  35.     public Ray level() {
  36.         return new Ray(origin, direction.setY(0).normalize());
  37.     }
  38.    
  39.     public Vector getOrigin() {
  40.         return origin;
  41.     }
  42.    
  43.     public Vector getDirection() {
  44.         return direction;
  45.     }
  46.    
  47.     public double origin(int i) {
  48.         switch (i) {
  49.             case 0:
  50.                 return origin.getX();
  51.             case 1:
  52.                 return origin.getY();
  53.             case 2:
  54.                 return origin.getZ();
  55.             default:
  56.                 return 0;
  57.         }
  58.     }
  59.    
  60.     public double direction(int i) {
  61.         switch (i) {
  62.             case 0:
  63.                 return direction.getX();
  64.             case 1:
  65.                 return direction.getY();
  66.             case 2:
  67.                 return direction.getZ();
  68.             default:
  69.                 return 0;
  70.         }
  71.     }
  72.    
  73.     // Get a point x distance away from this ray.
  74.     // Can be used to get a point 2 blocks in front of a player's face.
  75.     public Vector getPoint(double distance) {
  76.         return direction.clone().normalize().multiply(distance).add(origin);
  77.     }
  78.    
  79.     // Same as above, but no need to construct object.
  80.     public static Location getPoint(Player player, double distance) {
  81.         Vector point = Ray.from(player).getPoint(distance);
  82.         return new Location(player.getWorld(), point.getX(), point.getY(), point.getZ());
  83.     }
  84.    
  85.     // (Something I made for personal use)
  86.     // Twists the vector by theta degrees.
  87.     public Vector twist(double deg) {
  88.         // Quaternion is another class I made with useful methods.
  89.         Vector r = Quaternion.rotate(direction, Ray.right(direction), -90);
  90.         return Quaternion.rotate(direction, r, -deg);
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement