Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void pushOutOfBlocks(){
- float growth=size-prevSize;
- if(growth<=0)return;
- growth/=2;
- //get world intersection
- List<AxisAlignedBB> boundingBoxes=getWorld().getCubes(null,getBoundingBox());
- //exit if nothing to process
- if(boundingBoxes.isEmpty())return;
- //pos = position of the entity and center position of bounding box
- AxisAlignedBB bb=getBoundingBox();
- //how much the bounding box has grown on a side
- //total movement for bounding box to exit any colliding boxes
- Vec3M push=new Vec3M();
- if(boundingBoxes.size()==1){
- AxisAlignedBB box=boundingBoxes.get(0);
- double
- boxXMiddle=(box.minX+box.maxX)/2,
- boxYMiddle=(box.minY+box.maxY)/2,
- boxZMiddle=(box.minZ+box.maxZ)/2;
- if(boxXMiddle<pos.x)push.x=box.maxX-bb.minX+growth;
- else push.x=box.maxX-bb.minX-growth;
- if(boxYMiddle<pos.y)push.y=box.maxY-bb.minY+growth;
- else push.y=box.minY-bb.maxY-growth;
- if(boxZMiddle<pos.z)push.z=box.maxZ-bb.minZ+growth;
- else push.z=box.minZ-bb.maxZ-growth;
- }else{
- for(AxisAlignedBB box:boundingBoxes){
- double
- boxXMiddle=(box.minX+box.maxX)/2,
- boxYMiddle=(box.minY+box.maxY)/2,
- boxZMiddle=(box.minZ+box.maxZ)/2;
- if(boxXMiddle<pos.x)push.x=Math.max(push.x, box.maxX-bb.minX+growth);
- else push.x=Math.min(push.x, box.maxX-bb.minX-growth);
- if(boxYMiddle<pos.y)push.y=Math.max(push.y, box.maxY-bb.minY+growth);
- else push.y=Math.min(push.y, box.minY-bb.maxY-growth);
- if(boxZMiddle<pos.z)push.z=Math.max(push.z, box.maxZ-bb.minZ+growth);
- else push.z=Math.min(push.z, box.minZ-bb.maxZ-growth);
- }
- }
- //absolute vector of push used to determine that plane should be used to minimize distance pushed
- Vec3M absPush=push.abs();
- if(absPush.x<absPush.y&&absPush.x<absPush.z){
- pos.x+=push.x;
- return;
- }
- if(absPush.y<absPush.x&&absPush.y<absPush.z){
- pos.y+=push.y;
- return;
- }
- if(absPush.z<absPush.x&&absPush.z<absPush.x){
- pos.z+=push.z;
- }
- }
Add Comment
Please, Sign In to add comment