Advertisement
Guest User

Syniobots AABB class

a guest
Jun 14th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. package bone008.syniobots.shared.bounds;
  2.  
  3. import bone008.syniobots.shared.util.Vector3d;
  4.  
  5. /**
  6.  * An immutable axis-aligned bounding box that is represented by a minimum and maximum point.
  7.  */
  8. public class AABB {
  9.    
  10.     private static final Vector3d AXIS_X = new Vector3d(1, 0, 0);
  11.     private static final Vector3d AXIS_Y = new Vector3d(0, 1, 0);
  12.     private static final Vector3d AXIS_Z = new Vector3d(0, 0, 1);
  13.    
  14.    
  15.     private final Vector3d min;
  16.     private final Vector3d max;
  17.    
  18.     /**
  19.      * Creates a new AABB.<br>
  20.      * Note that the parameters are cloned before being stored.
  21.      */
  22.     public AABB(Vector3d min, Vector3d max) {
  23.         this.min = min.clone();
  24.         this.max = max.clone();
  25.     }
  26.    
  27.    
  28.     // roughly based on http://gamedev.stackexchange.com/questions/32807/collision-resolve-sphere-aabb-and-aabb-aabb
  29.     /**
  30.      * Tests for an intersection of two axis-aligned bounding boxes, respecting a spatial offset for both of them.
  31.      *
  32.      * @param box1 the first box
  33.      * @param pos1 the bias to offset the first box
  34.      * @param box2 the second box
  35.      * @param pos2 the bias to offset the second box
  36.      * @param mtvOut modified only if an intersection was detected; it then contains the minimum translation vector required to move box1 out of box2
  37.      *
  38.      * @return true if the boxes intersect, false otherwise
  39.      */
  40.     public static boolean testIntersection(AABB box1, Vector3d pos1, AABB box2, Vector3d pos2, Vector3d mtvOut) {
  41.         // initial state
  42.         Vector3d mtvAxis = new Vector3d();
  43.         double[] mtvDistance = { Double.MAX_VALUE }; // wrap in single-element array to allow pass-by-reference
  44.        
  45.         if (!testAxis(AXIS_X, pos1.x, box1.min.x, box1.max.x, pos2.x, box2.min.x, box2.max.x, mtvAxis, mtvDistance))
  46.             return false;
  47.         if (!testAxis(AXIS_Y, pos1.y, box1.min.y, box1.max.y, pos2.y, box2.min.y, box2.max.y, mtvAxis, mtvDistance))
  48.             return false;
  49.         if (!testAxis(AXIS_Z, pos1.z, box1.min.z, box1.max.z, pos2.z, box2.min.z, box2.max.z, mtvAxis, mtvDistance))
  50.             return false;
  51.        
  52.         if (mtvOut != null)
  53.             mtvOut.copy(mtvAxis).multiply(mtvDistance[0] + 1); // small bias
  54.         return true;
  55.     }
  56.    
  57.     private static boolean testAxis(Vector3d axis, double bias1, double min1, double max1, double bias2, double min2, double max2, Vector3d mtvAxis, double[] mtvDistance) {
  58.         double pen1 = (bias2 + max2) - (bias1 + min1); // "left"
  59.         double pen2 = (bias1 + max1) - (bias2 + min2); // "right"
  60.        
  61.         if (pen1 < 0 || pen2 < 0)
  62.             return false;
  63.        
  64.         double overlap = (pen1 < pen2 ? pen1 : -pen2);
  65.        
  66.         if (overlap * overlap < mtvDistance[0] * mtvDistance[0]) {
  67.             mtvDistance[0] = overlap;
  68.             mtvAxis.copy(axis);
  69.         }
  70.        
  71.         return true;
  72.     }
  73.    
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement