public class MusePlayerUtils {
static final double root2 = Math.sqrt(2);
public static MovingObjectPosition raytraceEntities(World world, EntityPlayer player, boolean collisionFlag, double reachDistance) {
MovingObjectPosition pickedEntity = null;
Vec3 playerPosition = Vec3.createVectorHelper(player.posX, player.posY + player.getEyeHeight(), player.posZ);
Vec3 playerLook = player.getLookVec();
Vec3 playerViewOffset = Vec3.createVectorHelper(playerPosition.xCoord + playerLook.xCoord * reachDistance, playerPosition.yCoord
+ playerLook.yCoord * reachDistance, playerPosition.zCoord + playerLook.zCoord * reachDistance);
double playerBorder = 1.1 * reachDistance;
AxisAlignedBB boxToScan = player.boundingBox.expand(playerBorder, playerBorder, playerBorder);
// AxisAlignedBB boxToScan =
// player.boundingBox.addCoord(playerLook.xCoord * reachDistance,
// playerLook.yCoord * reachDistance, playerLook.zCoord
// * reachDistance);
List entitiesHit = world.getEntitiesWithinAABBExcludingEntity(player, boxToScan);
double closestEntity = reachDistance;
if (entitiesHit == null || entitiesHit.isEmpty()) {
return null;
}
for (Entity entityHit : (Iterable<Entity>) entitiesHit) {
if (entityHit != null && entityHit.canBeCollidedWith() && entityHit.boundingBox != null) {
float border = entityHit.getCollisionBorderSize();
AxisAlignedBB aabb = entityHit.boundingBox.expand((double) border, (double) border, (double) border);
MovingObjectPosition hitMOP = aabb.calculateIntercept(playerPosition, playerViewOffset);
if (hitMOP != null) {
if (aabb.isVecInside(playerPosition)) {
if (0.0D < closestEntity || closestEntity == 0.0D) {
pickedEntity = new MovingObjectPosition(entityHit);
if (pickedEntity != null) {
pickedEntity.hitVec = hitMOP.hitVec;
closestEntity = 0.0D;
}
}
} else {
double distance = playerPosition.distanceTo(hitMOP.hitVec);
if (distance < closestEntity || closestEntity == 0.0D) {
pickedEntity = new MovingObjectPosition(entityHit);
pickedEntity.hitVec = hitMOP.hitVec;
closestEntity = distance;
}
}
}