* getDeltaPositionFromRotation()
* @return The target hit.
*/
public MovingObjectPosition rayTraceEntities(World world, Vector3 target)
{
MovingObjectPosition pickedEntity = null;
Vec3 startingPosition = toVec3();
Vec3 look = target.toVec3();
double reachDistance = distance(target);
//Vec3 reachPoint = Vec3.createVectorHelper(startingPosition.xCoord + look.xCoord * reachDistance, startingPosition.yCoord + look.yCoord * reachDistance, startingPosition.zCoord + look.zCoord * reachDistance);
double checkBorder = 1.1 * reachDistance;
AxisAlignedBB boxToScan = AxisAlignedBB.getAABBPool().getAABB(-checkBorder, -checkBorder, -checkBorder, checkBorder, checkBorder, checkBorder).offset(this.x, this.y, this.z);
@SuppressWarnings("unchecked")
List<Entity> entitiesInBounds = world.getEntitiesWithinAABBExcludingEntity(null, boxToScan);
double closestEntity = reachDistance;
if (entitiesInBounds == null || entitiesInBounds.isEmpty())
{
return null;
}
for (Entity possibleHits : entitiesInBounds)
{
if (possibleHits != null && possibleHits.canBeCollidedWith() && possibleHits.boundingBox != null)
{
float border = possibleHits.getCollisionBorderSize();
AxisAlignedBB aabb = possibleHits.boundingBox.expand(border, border, border);
MovingObjectPosition hitMOP = aabb.calculateIntercept(startingPosition, target.toVec3());
if (hitMOP != null)
{
if (aabb.isVecInside(startingPosition))
{
if (0.0D < closestEntity || closestEntity == 0.0D)
{
pickedEntity = new MovingObjectPosition(possibleHits);
if (pickedEntity != null)
{
pickedEntity.hitVec = hitMOP.hitVec;
closestEntity = 0.0D;
}
}
}
else
{
double distance = startingPosition.distanceTo(hitMOP.hitVec);
if (distance < closestEntity || closestEntity == 0.0D)
{
pickedEntity = new MovingObjectPosition(possibleHits);
pickedEntity.hitVec = hitMOP.hitVec;
closestEntity = distance;
}
}
}