LapisSea

Untitled

Jul 2nd, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1. public void pushOutOfBlocks(){
  2.     float growth=size-prevSize;
  3.     if(growth<=0)return;
  4.     growth/=2;
  5.  
  6.     //get world intersection
  7.     List<AxisAlignedBB> boundingBoxes=getWorld().getCubes(null,getBoundingBox());
  8.     //exit if nothing to process
  9.     if(boundingBoxes.isEmpty())return;
  10.    
  11.     //pos = position of the entity and center position of bounding box
  12.    
  13.     AxisAlignedBB bb=getBoundingBox();
  14.    
  15.     //how much the bounding box has grown on a side
  16.    
  17.     //total movement for bounding box to exit any colliding boxes
  18.     Vec3M push=new Vec3M();
  19.    
  20.     if(boundingBoxes.size()==1){
  21.        
  22.         AxisAlignedBB box=boundingBoxes.get(0);
  23.         double
  24.             boxXMiddle=(box.minX+box.maxX)/2,
  25.             boxYMiddle=(box.minY+box.maxY)/2,
  26.             boxZMiddle=(box.minZ+box.maxZ)/2;
  27.        
  28.         if(boxXMiddle<pos.x)push.x=box.maxX-bb.minX+growth;
  29.         else push.x=box.maxX-bb.minX-growth;
  30.        
  31.         if(boxYMiddle<pos.y)push.y=box.maxY-bb.minY+growth;
  32.         else push.y=box.minY-bb.maxY-growth;
  33.        
  34.         if(boxZMiddle<pos.z)push.z=box.maxZ-bb.minZ+growth;
  35.         else push.z=box.minZ-bb.maxZ-growth;
  36.        
  37.     }else{
  38.        
  39.         for(AxisAlignedBB box:boundingBoxes){
  40.             double
  41.                 boxXMiddle=(box.minX+box.maxX)/2,
  42.                 boxYMiddle=(box.minY+box.maxY)/2,
  43.                 boxZMiddle=(box.minZ+box.maxZ)/2;
  44.            
  45.             if(boxXMiddle<pos.x)push.x=Math.max(push.x, box.maxX-bb.minX+growth);
  46.             else push.x=Math.min(push.x, box.maxX-bb.minX-growth);
  47.            
  48.             if(boxYMiddle<pos.y)push.y=Math.max(push.y, box.maxY-bb.minY+growth);
  49.             else push.y=Math.min(push.y, box.minY-bb.maxY-growth);
  50.            
  51.             if(boxZMiddle<pos.z)push.z=Math.max(push.z, box.maxZ-bb.minZ+growth);
  52.             else push.z=Math.min(push.z, box.minZ-bb.maxZ-growth);
  53.         }
  54.        
  55.     }
  56.    
  57.     //absolute vector of push used to determine that plane should be used to minimize distance pushed
  58.     Vec3M absPush=push.abs();
  59.    
  60.     if(absPush.x<absPush.y&&absPush.x<absPush.z){
  61.         pos.x+=push.x;
  62.         return;
  63.     }
  64.     if(absPush.y<absPush.x&&absPush.y<absPush.z){
  65.         pos.y+=push.y;
  66.         return;
  67.     }
  68.     if(absPush.z<absPush.x&&absPush.z<absPush.x){
  69.         pos.z+=push.z;
  70.     }
  71. }
Add Comment
Please, Sign In to add comment