Java Code Examples for net.minecraft.util.math.Vec3d#distanceTo()

The following examples show how to use net.minecraft.util.math.Vec3d#distanceTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GTUtility.java    From GT-Classic with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Pushes entities away from target within an area, code adapted from
 * EE/ProjectE.
 */
public static void repelEntitiesInAABBFromPoint(World world, AxisAlignedBB boundingbox, double x, double y,
		double z) {
	List<Entity> list = world.getEntitiesWithinAABB(Entity.class, boundingbox);
	if (list.isEmpty()) {
		return;
	}
	for (Entity entity : list) {
		if ((entity instanceof EntityLiving) || (entity instanceof IProjectile)) {
			if (entity instanceof EntityArrow && ((EntityArrow) entity).onGround) {
				continue;
			}
			if (entity instanceof EntityArmorStand) {
				continue;
			}
			Vec3d p = new Vec3d(x, y, z);
			Vec3d t = new Vec3d(entity.posX, entity.posY, entity.posZ);
			double distance = p.distanceTo(t) + 0.1D;
			Vec3d r = new Vec3d(t.x - p.x, t.y - p.y, t.z - p.z);
			entity.motionX += r.x / 1.5D / distance;
			entity.motionY += r.y / 1.5D / distance;
			entity.motionZ += r.z / 1.5D / distance;
		}
	}
}
 
Example 2
Source File: EntityUtils.java    From ForgeHax with MIT License 6 votes vote down vote up
/**
 * Create a trace
 */
public static RayTraceResult traceEntity(
    World world, Vec3d start, Vec3d end, List<Entity> filter) {
  RayTraceResult result = null;
  double hitDistance = -1;
  
  for (Entity ent : world.loadedEntityList) {
    
    if (filter.contains(ent)) {
      continue;
    }
    
    double distance = start.distanceTo(ent.getPositionVector());
    RayTraceResult trace = ent.getEntityBoundingBox().calculateIntercept(start, end);
    
    if (trace != null && (hitDistance == -1 || distance < hitDistance)) {
      hitDistance = distance;
      result = trace;
      result.entityHit = ent;
    }
  }
  
  return result;
}
 
Example 3
Source File: SoundPhysics.java    From Sound-Physics with GNU General Public License v3.0 6 votes vote down vote up
private static float calculateAttenuation(double x, double y, double z)
{
	if (SoundPhysics.mc.thePlayer != null)
	{
		Vec3d playerPos = SoundPhysics.mc.thePlayer.getPositionVector();
		
		double soundDistance = playerPos.distanceTo(new Vec3d(x, y, z));
					
		float atten = (float)Math.max(1.0 - soundDistance / 16.0, 0.0f);		

		//logDetailed("Sound attenuation: " + atten);
		
		return atten;
	}
	else
	{
		//System.out.println("NULL PLAYER");
		return 1.0f;
	}		
}
 
Example 4
Source File: PortalFinderModule.java    From seppuku with GNU General Public License v3.0 5 votes vote down vote up
private boolean isPortalCached(int x, int y, int z, float dist) {
    for (int i = this.portals.size() - 1; i >= 0; i--) {
        Vec3d searchPortal = this.portals.get(i);

        if (searchPortal.distanceTo(new Vec3d(x, y, z)) <= dist)
            return true;

        if (searchPortal.x == x && searchPortal.y == y && searchPortal.z == z)
            return true;
    }
    return false;
}
 
Example 5
Source File: RayTraceTools.java    From YouTubeModdingTutorial with MIT License 5 votes vote down vote up
public static void rayTrace(Beam beam, Function<Entity, Boolean> consumer) {
    // Based on EntityRender.getMouseOver(float partialTicks) which we can't use because that's client only
    Vec3d start = beam.getStart();
    Vec3d lookVec = beam.getLookVec();
    Vec3d end = beam.getEnd();
    double dist = beam.getDist();
    World world = beam.getWorld();
    EntityPlayer player = beam.getPlayer();
    List<Entity> targets = world.getEntitiesInAABBexcluding(player, player.getEntityBoundingBox().expand(lookVec.x * dist, lookVec.y * dist, lookVec.z * dist).grow(1.0D, 1.0D, 1.0D),
            Predicates.and(EntitySelectors.NOT_SPECTATING, ent -> ent != null && ent.canBeCollidedWith()));
    List<Pair<Entity, Double>> hitTargets = new ArrayList<>();
    for (Entity target : targets) {
        AxisAlignedBB targetBB = target.getEntityBoundingBox().grow(target.getCollisionBorderSize());
        if (targetBB.contains(start)) {
            hitTargets.add(Pair.of(target, 0.0));
        } else {
            RayTraceResult targetResult = targetBB.calculateIntercept(start, end);
            if (targetResult != null) {
                double d3 = start.distanceTo(targetResult.hitVec);
                if (d3 < dist) {
                    hitTargets.add(Pair.of(target, d3));
                }
            }
        }
    }
    hitTargets.sort(Comparator.comparing(Pair::getRight));
    hitTargets.stream().filter(pair -> consumer.apply(pair.getLeft())).findFirst();
}
 
Example 6
Source File: AntiAfkMod.java    From ForgeHax with MIT License 5 votes vote down vote up
@Override
public void onStart() {
  ForgeHaxHooks.isSafeWalkActivated = true;
  Bindings.forward.bind();
  
  Vec3d eye = EntityUtils.getEyePos(getLocalPlayer());
  
  List<Double> yaws = Lists.newArrayList();
  for (int i = 0; i < (360 / DEGREES); ++i) {
    yaws.add((i * DEGREES) - 180.D);
  }
  Collections.shuffle(yaws);
  
  double lastDistance = -1.D;
  for (double y : yaws) {
    double[] cc = Angle.degrees(0.f, (float) y).getForwardVector();
    Vec3d target = eye.add(new Vec3d(cc[0], cc[1], cc[2]).normalize().scale(64));
    
    RayTraceResult result = getWorld().rayTraceBlocks(eye, target, false, true, false);
    double distance = result == null ? 64.D : eye.distanceTo(result.hitVec);
    if ((distance >= 1.D || lastDistance == -1.D)
        && (distance > lastDistance || Math.random() < 0.20D)) {
      angle = y;
      lastDistance = distance;
    }
  }
}
 
Example 7
Source File: EntityUtils.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns the index of the BB in the given list that the given vectors are currently pointing at.
 * @return the list index of the pointed box, or -1 of no hit was detected
 */
public static int getPointedBox(Vec3d eyesVec, Vec3d lookVec, double reach, List<AxisAlignedBB> boxes)
{
    Vec3d lookEndVec = eyesVec.add(lookVec.x * reach, lookVec.y * reach, lookVec.z * reach);
    //AxisAlignedBB box = null;
    //Vec3d hitVec = null;
    double distance = reach;
    int index = -1;

    for (int i = 0; i < boxes.size(); ++i)
    {
        AxisAlignedBB bb = boxes.get(i);
        RayTraceResult rayTrace = bb.calculateIntercept(eyesVec, lookEndVec);

        if (bb.contains(eyesVec))
        {
            if (distance >= 0.0D)
            {
                //box = bb;
                //hitVec = rayTrace == null ? eyesVec : rayTrace.hitVec;
                distance = 0.0D;
                index = i;
            }
        }
        else if (rayTrace != null)
        {
            double distanceTmp = eyesVec.distanceTo(rayTrace.hitVec);

            if (distanceTmp < distance)
            {
                //box = bb;
                //hitVec = rayTrace.hitVec;
                distance = distanceTmp;
                index = i;
            }
        }
    }

    return index;
}
 
Example 8
Source File: EntityUtils.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns the index of the BB in the given list that the given vectors are currently pointing at.
 * @return the list index of the pointed box, or null of no hit was detected
 */
@Nullable
public static <T> T getPointedBox(Vec3d eyesVec, Vec3d lookVec, double reach, Map<T, AxisAlignedBB> boxMap)
{
    Vec3d lookEndVec = eyesVec.add(lookVec.x * reach, lookVec.y * reach, lookVec.z * reach);
    double distance = reach;
    T key = null;

    for (Map.Entry<T, AxisAlignedBB> entry : boxMap.entrySet())
    {
        AxisAlignedBB bb = entry.getValue();
        RayTraceResult rayTrace = bb.calculateIntercept(eyesVec, lookEndVec);

        if (bb.contains(eyesVec))
        {
            if (distance >= 0.0D)
            {
                distance = 0.0D;
                key = entry.getKey();
            }
        }
        else if (rayTrace != null)
        {
            double distanceTmp = eyesVec.distanceTo(rayTrace.hitVec);

            if (distanceTmp < distance)
            {
                distance = distanceTmp;
                key = entry.getKey();
            }
        }
    }

    return key;
}
 
Example 9
Source File: ShapeDispatcher.java    From fabric-carpet with MIT License 4 votes vote down vote up
private static boolean isStraight(Vec3d from, Vec3d to, double density)
{
    if ( (from.x == to.x && from.y == to.y) || (from.x == to.x && from.z == to.z) || (from.y == to.y && from.z == to.z))
        return from.distanceTo(to) / density > 20;
    return false;
}
 
Example 10
Source File: PlayerParticle.java    From MiningGadgets with MIT License 4 votes vote down vote up
@Override
public void tick() {
    //System.out.println("I exist!" + posX+":"+posY+":"+posZ +"....."+targetX+":"+targetY+":"+targetZ);
    double moveX;
    double moveY;
    double moveZ;

    //Just in case something goes weird, we remove the particle if its been around too long.
    if (this.age++ >= this.maxAge) {
        this.setExpired();
    }

    //prevPos is used in the render. if you don't do this your particle rubber bands (Like lag in an MMO).
    //This is used because ticks are 20 per second, and FPS is usually 60 or higher.
    this.prevPosX = this.posX;
    this.prevPosY = this.posY;
    this.prevPosZ = this.posZ;

    Vec3d sourcePos = new Vec3d(sourceX, sourceY, sourceZ);
    Vec3d targetPos = new Vec3d(targetX, targetY, targetZ);

    //Get the current position of the particle, and figure out the vector of where it's going
    Vec3d partPos = new Vec3d(this.posX, this.posY, this.posZ);
    Vec3d targetDirection = new Vec3d(targetPos.getX() - this.posX, targetPos.getY() - this.posY, targetPos.getZ() - this.posZ);

    //The total distance between the particle and target
    double totalDistance = targetPos.distanceTo(partPos);
    if (totalDistance < 0.1)
        this.setExpired();

    double speedAdjust = 20;

    moveX = (targetX - this.posX) / speedAdjust;
    moveY = (targetY - this.posY) / speedAdjust;
    moveZ = (targetZ - this.posZ) / speedAdjust;

    BlockPos nextPos = new BlockPos(this.posX + moveX, this.posY + moveY, this.posZ + moveZ);

    if (age > 40)
        //if (world.getBlockState(nextPos).getBlock() == ModBlocks.RENDERBLOCK)
        this.canCollide = false;
    //Perform the ACTUAL move of the particle.
    this.move(moveX, moveY, moveZ);
}
 
Example 11
Source File: Aimbot.java    From ForgeHax with MIT License 4 votes vote down vote up
private boolean isInRange(Vec3d from, Vec3d to) {
  double dist = isProjectileAimbotActivated() ? projectile_range.get() : range.get();
  return dist <= 0 || from.distanceTo(to) <= dist;
}
 
Example 12
Source File: LocalPlayerUtils.java    From ForgeHax with MIT License 4 votes vote down vote up
public static RayTraceResult getViewTrace(
    Entity entity, Vec3d direction, float partialTicks, double reach, double reachAttack) {
  if (entity == null) {
    return null;
  }
  
  Vec3d eyes = entity.getPositionEyes(partialTicks);
  RayTraceResult trace = entity.rayTrace(reach, partialTicks);
  
  Vec3d dir = direction.scale(reach);
  Vec3d lookDir = eyes.add(dir);
  
  double hitDistance = trace == null ? reachAttack : trace.hitVec.distanceTo(eyes);
  Entity hitEntity = null;
  Vec3d hitEntityVec = null;
  
  for (Entity ent :
      getWorld()
          .getEntitiesInAABBexcluding(
              entity,
              entity.getEntityBoundingBox().expand(dir.x, dir.y, dir.y).grow(1.D),
              Predicates.and(
                  EntitySelectors.NOT_SPECTATING,
                  ent -> ent != null && ent.canBeCollidedWith()))) {
    AxisAlignedBB bb = ent.getEntityBoundingBox().grow(ent.getCollisionBorderSize());
    RayTraceResult tr = bb.calculateIntercept(eyes, lookDir);
    if (bb.contains(eyes)) {
      if (hitDistance > 0.D) {
        hitEntity = ent;
        hitEntityVec = tr == null ? eyes : tr.hitVec;
        hitDistance = 0.D;
      }
    } else if (tr != null) {
      double dist = eyes.distanceTo(tr.hitVec);
      if (dist < hitDistance || hitDistance == 0.D) {
        if (entity.getLowestRidingEntity() == ent.getLowestRidingEntity()
            && !ent.canRiderInteract()) {
          if (hitDistance == 0.D) {
            hitEntity = ent;
            hitEntityVec = tr.hitVec;
          }
        } else {
          hitEntity = ent;
          hitEntityVec = tr.hitVec;
          hitDistance = dist;
        }
      }
    }
  }
  
  if (hitEntity != null && reach > 3.D && eyes.distanceTo(hitEntityVec) > 3.D) {
    return new RayTraceResult(Type.MISS, hitEntityVec, EnumFacing.UP, new BlockPos(hitEntityVec));
  } else if (hitEntity != null && trace == null && hitDistance < reachAttack) {
    return new RayTraceResult(hitEntity, hitEntityVec);
  } else {
    return trace;
  }
}
 
Example 13
Source File: LibParticles.java    From Wizardry with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void SHAPE_BEAM(World world, Vec3d target, Vec3d origin, Color color) {
	ParticleBuilder beam = new ParticleBuilder(10);
	beam.setRender(new ResourceLocation(Wizardry.MODID, MISC.SPARKLE_BLURRED));
	beam.setAlphaFunction(new InterpFloatInOut(0.3f, 1f));
	//beam.disableRandom();

	beam.setColor(ColorUtils.shiftColorHueRandomly(color, 10));
	beam.setCollision(true);

	Vec3d look = target.subtract(origin).normalize();
	double dist = target.distanceTo(origin);

	ParticleSpawner.spawn(beam, world, new StaticInterp<>(origin), 1, 0, (aFloat1, particleBuilder1) -> {
		particleBuilder1.setPositionOffset(look.scale(RandUtil.nextDouble(0, dist)));
		particleBuilder1.setScale(RandUtil.nextFloat(0.1f, 0.5f));
		final int life = RandUtil.nextInt(50, 60);
		particleBuilder1.setLifetime(life);
		particleBuilder1.enableMotionCalculation();
		particleBuilder1.setCollision(true);
		particleBuilder1.setCanBounce(true);
		particleBuilder1.setAcceleration(new Vec3d(0, -0.03, 0));

		double radius = 2;
		double theta = 2.0f * (float) Math.PI * RandUtil.nextFloat();
		double r = radius * RandUtil.nextFloat();
		double x = r * MathHelper.cos((float) theta);
		double z = r * MathHelper.sin((float) theta);
		Vec3d dest = new Vec3d(x, RandUtil.nextDouble(-1, 2), z);
		particleBuilder1.setPositionFunction(new InterpBezier3D(Vec3d.ZERO, dest, dest.scale(2), new Vec3d(dest.x, RandUtil.nextDouble(-2, 2), dest.z)));

		particleBuilder1.setTick(particle -> {
			if (particle.getAge() >= particle.getLifetime() / RandUtil.nextDouble(2, 5)) {
				if (particle.getAcceleration().y == 0)
					particle.setAcceleration(new Vec3d(0, RandUtil.nextDouble(-0.05, -0.01), 0));
			} else if (particle.getAcceleration().x != 0 || particle.getAcceleration().y != 0 || particle.getAcceleration().z != 0) {
				particle.setAcceleration(Vec3d.ZERO);
			}
		});
	});

	ParticleSpawner.spawn(beam, world, new StaticInterp<>(origin), 10, 0, (aFloat, particleBuilder) -> {
		particleBuilder.setTick(particle -> particle.setAcceleration(Vec3d.ZERO));

		particleBuilder.setScaleFunction(new InterpFloatInOut(0, 1f));
		particleBuilder.setAlphaFunction(new InterpFloatInOut(0.3f, 0.2f));
		particleBuilder.setScale(RandUtil.nextFloat(0.5f, 1.5f));
		particleBuilder.setLifetime(RandUtil.nextInt(10, 20));
		particleBuilder.disableMotionCalculation();
		particleBuilder.setMotion(Vec3d.ZERO);
		particleBuilder.setCanBounce(false);
		particleBuilder.setPositionOffset(Vec3d.ZERO);
		particleBuilder.setPositionFunction(new InterpLine(Vec3d.ZERO, target.subtract(origin)));
	});
}