StarShadow

My AABB class

Aug 22nd, 2016 (edited)
1,993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. import org.bukkit.Location;
  2. import org.bukkit.configuration.serialization.ConfigurationSerializable;
  3. import org.bukkit.entity.Player;
  4. import org.bukkit.util.Vector;
  5.  
  6. import java.util.HashMap;
  7. import java.util.Map;
  8.  
  9. // Just an AABB class I made with some useful methods I needed.
  10. // Mainly for fast Ray-AABB collision detection.
  11.  
  12. public class AABB {
  13.    
  14.     private Vector min, max; // min/max locations
  15.    
  16.     // Create Bounding Box from min/max locations.
  17.     public AABB(Vector min, Vector max) {
  18.         this(min.getX(), min.getY(), min.getZ(), max.getX(), max.getY(), max.getZ());
  19.     }
  20.    
  21.     // Main constructor for AABB
  22.     public AABB(double x1, double y1, double z1, double x2, double y2, double z2) {
  23.         this.min = new Vector(Math.min(x1, x2), Math.min(y1, y2), Math.min(z1, z2));
  24.         this.max = new Vector(Math.max(x1, x2), Math.max(y1, y2), Math.max(z1, z2));
  25.     }
  26.    
  27.     private AABB(Player player) {
  28.         this.min = getMin(player);
  29.         this.max = getMax(player);
  30.     }
  31.    
  32.     private Vector getMin(Player player) {
  33.         return player.getLocation().toVector().add(new Vector(-0.3, 0, -0.3));
  34.     }
  35.    
  36.     private Vector getMax(Player player) {
  37.         return player.getLocation().toVector().add(new Vector(0.3, 1.8, 0.3));
  38.     }
  39.    
  40.     // Create an AABB based on a player's hitbox
  41.     public static AABB from(Player player) {
  42.         return new AABB(player);
  43.     }
  44.    
  45.     public Vector getMin() {
  46.         return min;
  47.     }
  48.    
  49.     public Vector getMax() {
  50.         return max;
  51.     }
  52.    
  53.     // Returns minimum x, y, or z point from inputs 0, 1, or 2.
  54.     public double min(int i) {
  55.         switch (i) {
  56.             case 0:
  57.                 return min.getX();
  58.             case 1:
  59.                 return min.getY();
  60.             case 2:
  61.                 return min.getZ();
  62.             default:
  63.                 return 0;
  64.         }
  65.     }
  66.    
  67.     // Returns maximum x, y, or z point from inputs 0, 1, or 2.
  68.     public double max(int i) {
  69.         switch (i) {
  70.             case 0:
  71.                 return max.getX();
  72.             case 1:
  73.                 return max.getY();
  74.             case 2:
  75.                 return max.getZ();
  76.             default:
  77.                 return 0;
  78.         }
  79.     }
  80.    
  81.     // Check if a Ray passes through this box. tmin and tmax are the bounds.
  82.     // Example: If you wanted to see if the Ray collides anywhere from its
  83.     // origin to 5 units away, the values would be 0 and 5.
  84.     public boolean collides(Ray ray, double tmin, double tmax) {
  85.         for (int i = 0; i < 3; i++) {
  86.             double d = 1 / ray.direction(i);
  87.             double t0 = (min(i) - ray.origin(i)) * d;
  88.             double t1 = (max(i) - ray.origin(i)) * d;
  89.             if (d < 0) {
  90.                 double t = t0;
  91.                 t0 = t1;
  92.                 t1 = t;
  93.             }
  94.             tmin = t0 > tmin ? t0 : tmin;
  95.             tmax = t1 < tmax ? t1 : tmax;
  96.             if (tmax <= tmin) return false;
  97.         }
  98.         return true;
  99.     }
  100.    
  101.     // Same as other collides method, but returns the distance of the nearest
  102.     // point of collision of the ray and box, or -1 if no collision.
  103.     public double collidesD(Ray ray, double tmin, double tmax) {
  104.         for (int i = 0; i < 3; i++) {
  105.             double d = 1 / ray.direction(i);
  106.             double t0 = (min(i) - ray.origin(i)) * d;
  107.             double t1 = (max(i) - ray.origin(i)) * d;
  108.             if (d < 0) {
  109.                 double t = t0;
  110.                 t0 = t1;
  111.                 t1 = t;
  112.             }
  113.             tmin = t0 > tmin ? t0 : tmin;
  114.             tmax = t1 < tmax ? t1 : tmax;
  115.             if (tmax <= tmin) return -1;
  116.         }
  117.         return tmin;
  118.     }
  119.    
  120.     // Check if the location is in this box.
  121.     public boolean contains(Location location) {
  122.         if (location.getX() > max.getX()) return false;
  123.         if (location.getY() > max.getY()) return false;
  124.         if (location.getZ() > max.getZ()) return false;
  125.         if (location.getX() < min.getX()) return false;
  126.         if (location.getY() < min.getY()) return false;
  127.         if (location.getZ() < min.getZ()) return false;
  128.         return true;
  129.     }
  130. }
Add Comment
Please, Sign In to add comment