Skip to content

Instantly share code, notes, and snippets.

@Daomephsta
Created April 3, 2018 03:38
Show Gist options
  • Save Daomephsta/5742dc50b3ed1c9e951bf40f95384e21 to your computer and use it in GitHub Desktop.
Save Daomephsta/5742dc50b3ed1c9e951bf40f95384e21 to your computer and use it in GitHub Desktop.
public static RayTraceResult raytraceClosestObject(World world, @Nullable Entity exclude, Vec3d startVec, Vec3d endVec)
{
RayTraceResult result = world.rayTraceBlocks(startVec, endVec);
double blockHitDistance = 0.0D; // The distance to the block that was
// hit
if (result != null) blockHitDistance = result.hitVec.distanceTo(startVec);
// Encloses the entire area where entities that could collide with this
// ray exist
AxisAlignedBB entitySearchArea = new AxisAlignedBB(startVec.x, startVec.y, startVec.z, endVec.x, endVec.y,
endVec.z);
Entity hitEntity = null; // The closest entity that was hit
double entityHitDistance = 0.0D; // The squared distance to the closest
// entity that was hit
for (Entity entity : world.getEntitiesInAABBexcluding(exclude, entitySearchArea,
EntitySelectors.NOT_SPECTATING))
{
// The collision AABB of the entity expanded by the collision border
// size
AxisAlignedBB collisionBB = entity.getEntityBoundingBox().grow(entity.getCollisionBorderSize());
RayTraceResult intercept = collisionBB.calculateIntercept(startVec, endVec);
if (intercept != null)
{
double distance = startVec.distanceTo(intercept.hitVec);
if ((distance < blockHitDistance || blockHitDistance == 0)
&& (distance < entityHitDistance || entityHitDistance == 0.0D))
{
entityHitDistance = distance;
hitEntity = entity;
}
}
}
if (hitEntity != null) result = new RayTraceResult(hitEntity, hitEntity.getPositionVector());
if(result == null) result = new RayTraceResult(Type.MISS, endVec, null, new BlockPos(endVec));
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment