Java Code Examples for net.minecraft.util.math.MathHelper#atan2()

The following examples show how to use net.minecraft.util.math.MathHelper#atan2() . 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: EntityTofuGandlem.java    From TofuCraftReload with MIT License 6 votes vote down vote up
protected void castSpell() {
            EntityLivingBase entitylivingbase = EntityTofuGandlem.this.getAttackTarget();
//            double d0 = Math.min(entitylivingbase.posY, EntityTofuGandlem.this.posY);
//            double d1 = Math.max(entitylivingbase.posY, EntityTofuGandlem.this.posY) + 1.0D;
            float f = (float) MathHelper.atan2(entitylivingbase.posZ - EntityTofuGandlem.this.posZ, entitylivingbase.posX - EntityTofuGandlem.this.posX);


            for (int i = 0; i < 5; ++i) {
                float f1 = f + (float) i * (float) Math.PI * 0.4F;
                this.spawnFallTofu(EntityTofuGandlem.this.posX + (double) MathHelper.cos(f1) * 1.5D, EntityTofuGandlem.this.posZ + (double) MathHelper.sin(f1) * 1.5D);
                TofuMain.proxy.spawnParticle(EntityTofuGandlem.this.world, TofuParticleType.TOFUPORTAL, EntityTofuGandlem.this.posX + (double) MathHelper.cos(f1) * 1.5D, EntityTofuGandlem.this.posY + 8, EntityTofuGandlem.this.posZ + (double) MathHelper.sin(f1) * 1.5D, 0.0D, 0.0D, 0.0D);
            }

            for (int k = 0; k < 8; ++k) {
                float f2 = f + (float) k * (float) Math.PI * 2.0F / 8.0F + ((float) Math.PI * 2F / 5F);
                this.spawnFallTofu(EntityTofuGandlem.this.posX + (double) MathHelper.cos(f2) * 2.5D, EntityTofuGandlem.this.posZ + (double) MathHelper.sin(f2) * 2.5D);
                TofuMain.proxy.spawnParticle(EntityTofuGandlem.this.world, TofuParticleType.TOFUPORTAL, EntityTofuGandlem.this.posX + (double) MathHelper.cos(f2) * 2.5D, EntityTofuGandlem.this.posY + 8, EntityTofuGandlem.this.posZ + (double) MathHelper.sin(f2) * 2.5D, 0.0D, 0.0D, 0.0D);
            }

            for (int k = 0; k < 13; ++k) {
                float f3 = f + (float) k * 1.2F;
                this.spawnFallTofu(EntityTofuGandlem.this.posX + (double) MathHelper.cos(f3) * 3.5D, EntityTofuGandlem.this.posZ + (double) MathHelper.sin(f3) * 3.5D);
                TofuMain.proxy.spawnParticle(EntityTofuGandlem.this.world, TofuParticleType.TOFUPORTAL, EntityTofuGandlem.this.posX + (double) MathHelper.cos(f3) * 1.5D, EntityTofuGandlem.this.posY + 8, EntityTofuGandlem.this.posZ + (double) MathHelper.sin(f3) * 1.5D, 0.0D, 0.0D, 0.0D);
            }
        }
 
Example 2
Source File: EntityTofuFish.java    From TofuCraftReload with MIT License 6 votes vote down vote up
public void onUpdateMoveHelper() {
    if (this.fish.isInWater()) {
        this.fish.motionY += 0.005D;
    }

    if (this.action == EntityMoveHelper.Action.MOVE_TO && !this.fish.getNavigator().noPath()) {
        double d0 = this.posX - this.fish.posX;
        double d1 = this.posY - this.fish.posY;
        double d2 = this.posZ - this.fish.posZ;
        double d3 = (double) MathHelper.sqrt(d0 * d0 + d1 * d1 + d2 * d2);
        d1 = d1 / d3;
        float f = (float)(MathHelper.atan2(d2, d0) * (double)(180F / (float)Math.PI)) - 90.0F;
        this.fish.rotationYaw = this.limitAngle(this.fish.rotationYaw, f, 90.0F);
        this.fish.renderYawOffset = this.fish.rotationYaw;
        float f1 = (float)(this.speed * this.fish.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).getBaseValue());
        this.fish.setAIMoveSpeed(this.fish.getAIMoveSpeed() + (f1 - this.fish.getAIMoveSpeed()) * 0.125F);
        this.fish.motionY += (double)this.fish.getAIMoveSpeed() * d1 * 0.1D;
    } else {
        this.fish.setAIMoveSpeed(0.0F);
    }
}
 
Example 3
Source File: EntityVampireBat.java    From ToroQuest with GNU General Public License v3.0 6 votes vote down vote up
protected void batAiEdit() {

		Entity target = getAttackTarget();

		if (target == null || rand.nextInt(100) > 50) {
			this.spawnPosition = nonAttackLocation();
		} else {
			spawnPosition = target.getPosition().up();
		}

		double d0 = (double) this.spawnPosition.getX() + 0.5D - this.posX;
		double d1 = (double) this.spawnPosition.getY() + 0.1D - this.posY;
		double d2 = (double) this.spawnPosition.getZ() + 0.5D - this.posZ;
		this.motionX += (Math.signum(d0) * 0.5D - this.motionX) * 0.10000000149011612D;
		this.motionY += (Math.signum(d1) * 0.699999988079071D - this.motionY) * 0.10000000149011612D;
		this.motionZ += (Math.signum(d2) * 0.5D - this.motionZ) * 0.10000000149011612D;
		float f = (float) (MathHelper.atan2(this.motionZ, this.motionX) * (180D / Math.PI)) - 90.0F;
		float f1 = MathHelper.wrapDegrees(f - this.rotationYaw);
		this.moveForward = 0.5F;
		this.rotationYaw += f1;
	}
 
Example 4
Source File: SwitchHeldItemAndRotationPacket.java    From Cyberware with MIT License 6 votes vote down vote up
public static void faceEntity(Entity player, Entity entityIn)
{
	double d0 = entityIn.posX - player.posX;
	double d2 = entityIn.posZ - player.posZ;
	double d1;

	if (entityIn instanceof EntityLivingBase)
	{
		EntityLivingBase entitylivingbase = (EntityLivingBase)entityIn;
		d1 = entitylivingbase.posY + (double)entitylivingbase.getEyeHeight() - (player.posY + (double)player.getEyeHeight());
	}
	else
	{
		d1 = (entityIn.getEntityBoundingBox().minY + entityIn.getEntityBoundingBox().maxY) / 2.0D - (player.posY + (double)player.getEyeHeight());
	}

	double d3 = (double)MathHelper.sqrt_double(d0 * d0 + d2 * d2);
	float f = (float)(MathHelper.atan2(d2, d0) * (180D / Math.PI)) - 90.0F;
	float f1 = (float)(-(MathHelper.atan2(d1, d3) * (180D / Math.PI)));
	player.rotationPitch = f1;
	player.rotationYaw = f;
}
 
Example 5
Source File: Utils.java    From Wizardry with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static float signAngle(Vec3d a, Vec3d b, Vec3d n) {
	Vec3d cross = a.crossProduct(b);
	double s = cross.length();
	double c = a.dotProduct(b);
	double angle = MathHelper.atan2(s, c);

	if (n != null) {
		if (n.dotProduct(cross) < 0) {
			angle = -angle;
		}
	}

	return (float) Math.toDegrees(angle);
}
 
Example 6
Source File: FairyMoveHelper.java    From Wizardry with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onUpdateMoveHelper() {
	if (this.action == EntityMoveHelper.Action.MOVE_TO) {
		double x = this.posX - this.entity.posX;
		double y = this.posY - this.entity.posY;
		double z = this.posZ - this.entity.posZ;
		double distance = x * x + y * y + z * z;
		distance = (double) MathHelper.sqrt(distance);

		if (distance < this.entity.getEntityBoundingBox().getAverageEdgeLength()) {
			this.action = EntityMoveHelper.Action.WAIT;
			//	this.entity.motionX *= 0.9D;
			//	this.entity.motionY *= 0.9D;
			//	this.entity.motionZ *= 0.9D;
		} else {
			this.entity.motionX += x / distance * 0.05D * this.speed;
			this.entity.motionY += y / distance * 0.05D * this.speed;
			this.entity.motionZ += z / distance * 0.05D * this.speed;

			if (this.entity.getAttackTarget() == null) {
				this.entity.rotationYaw = -((float) MathHelper.atan2(this.entity.motionX, this.entity.motionZ)) * (180F / (float) Math.PI);
				this.entity.renderYawOffset = this.entity.rotationYaw;
			} else {
				double d4 = this.entity.getAttackTarget().posX - this.entity.posX;
				double d5 = this.entity.getAttackTarget().posZ - this.entity.posZ;
				this.entity.rotationYaw = -((float) MathHelper.atan2(d4, d5)) * (180F / (float) Math.PI);
				this.entity.renderYawOffset = this.entity.rotationYaw;
			}
		}
	}
}
 
Example 7
Source File: UnicornMoveHelper.java    From Wizardry with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onUpdateMoveHelper() {
	if (this.action == EntityMoveHelper.Action.MOVE_TO) {
		this.action = EntityMoveHelper.Action.WAIT;
		this.entity.setNoGravity(true);
		double d0 = this.posX - this.entity.posX;
		double d1 = this.posY - this.entity.posY;
		double d2 = this.posZ - this.entity.posZ;
		double d3 = d0 * d0 + d1 * d1 + d2 * d2;

		if (d3 < 2.500000277905201E-7D) {
			this.entity.setMoveVertical(0.0F);
			this.entity.setMoveForward(0.0F);
			return;
		}

		float f = (float) (MathHelper.atan2(d2, d0) * (180D / Math.PI)) - 90.0F;
		this.entity.rotationYaw = this.limitAngle(this.entity.rotationYaw, f, 10.0F);
		float f1;

		if (this.entity.onGround) {
			f1 = (float) (this.speed * this.entity.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).getAttributeValue());
		} else {
			f1 = (float) (this.speed * this.entity.getEntityAttribute(SharedMonsterAttributes.FLYING_SPEED).getAttributeValue());
		}

		this.entity.setAIMoveSpeed((float) speed);
		double d4 = (double) MathHelper.sqrt(d0 * d0 + d2 * d2);
		float f2 = (float) (-(MathHelper.atan2(d1, d4) * (180D / Math.PI)));
		this.entity.rotationPitch = this.limitAngle(this.entity.rotationPitch, f2, 10.0F);
		this.entity.setMoveVertical(d1 > 0.0D ? f1 : -f1);
	} else {
		this.entity.setNoGravity(true);
		this.entity.setMoveVertical(0.0F);
		this.entity.setMoveForward(0.0F);
	}
}
 
Example 8
Source File: DynamiteEntity.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void onUpdate() {
    this.lastTickPosX = this.posX;
    this.lastTickPosY = this.posY;
    this.lastTickPosZ = this.posZ;
    this.onEntityUpdate();

    ticksUntilExplosion--;

    if (world.rand.nextInt(3) == 2) {
        world.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, this.posX, this.posY, this.posZ, -this.motionX * 0.05f, this.inGround ? 0.05f : -this.motionY * 0.05f, -this.motionZ * 0.05f);
    }

    if (ticksUntilExplosion < 0 && !world.isRemote) {
        EntityLivingBase thrower = getThrower();
        world.createExplosion(thrower == null ? this : thrower, this.posX, this.posY, this.posZ, 1.5f, true);
        this.setDead();
        return;
    }

    if (this.inGround) {
        if (blockPosCollidedAt == null || this.world.getBlockState(blockPosCollidedAt).getBlock() != Blocks.AIR) {
            return;
        }

        this.inGround = false;
        this.motionX *= (double) (this.rand.nextFloat() * 0.2F);
        this.motionY *= (double) (this.rand.nextFloat() * 0.2F);
        this.motionZ *= (double) (this.rand.nextFloat() * 0.2F);
    }

    Vec3d vec3d = new Vec3d(this.posX, this.posY, this.posZ);
    Vec3d vec3d1 = new Vec3d(this.posX + this.motionX + (this.motionX > 0 ? 0.5f : -0.5f), this.posY + this.motionY + (this.motionY > 0 ? 0.2f : -0.2f), this.posZ + this.motionZ + (this.motionZ > 0 ? 0.2f : -0.2f));
    RayTraceResult raytraceresult = this.world.rayTraceBlocks(vec3d, vec3d1);

    if (raytraceresult != null) {
        if (raytraceresult.typeOfHit == RayTraceResult.Type.BLOCK && this.world.getBlockState(raytraceresult.getBlockPos()).getBlock() == Blocks.PORTAL) {
            this.setPortal(raytraceresult.getBlockPos());
        } else if (!net.minecraftforge.event.ForgeEventFactory.onProjectileImpact(this, raytraceresult)) {
            this.onImpact(raytraceresult);
        }
    }

    this.posX += this.motionX;
    this.posY += this.motionY;
    this.posZ += this.motionZ;
    float f = MathHelper.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
    this.rotationYaw = (float) (MathHelper.atan2(this.motionX, this.motionZ) * (180D / Math.PI));
    this.rotationPitch = (float) (MathHelper.atan2(this.motionY, (double) f) * (180D / Math.PI));

    while (this.rotationPitch - this.prevRotationPitch < -180.0F) {
        this.prevRotationPitch -= 360.0F;
    }

    while (this.rotationPitch - this.prevRotationPitch >= 180.0F) {
        this.prevRotationPitch += 360.0F;
    }

    while (this.rotationYaw - this.prevRotationYaw < -180.0F) {
        this.prevRotationYaw -= 360.0F;
    }

    while (this.rotationYaw - this.prevRotationYaw >= 180.0F) {
        this.prevRotationYaw += 360.0F;
    }

    this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F;
    this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F;
    float f1 = 0.99F;
    float f2 = this.getGravityVelocity();

    if (this.isInWater()) {
        for (int j = 0; j < 4; ++j) {
            this.world.spawnParticle(EnumParticleTypes.WATER_BUBBLE, this.posX - this.motionX * 0.25D, this.posY - this.motionY * 0.25D, this.posZ - this.motionZ * 0.25D, this.motionX, this.motionY, this.motionZ);
        }

        f1 = 0.8F;
    }

    this.motionX *= (double) f1;
    this.motionY *= (double) f1;
    this.motionZ *= (double) f1;

    if (!this.hasNoGravity()) {
        this.motionY -= (double) f2;
    }

    this.setPosition(this.posX, this.posY, this.posZ);
}
 
Example 9
Source File: PosUtils.java    From Wizardry with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static float[] vecToRotations(Vec3d vec) {
	float yaw = (float) MathHelper.atan2(vec.z, vec.x);
	float pitch = (float) Math.asin(vec.y / vec.length());
	return new float[]{(float) Math.toDegrees(pitch), (float) Math.toDegrees(yaw) + 90};
}
 
Example 10
Source File: EntitySpiritWight.java    From Wizardry with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void onUpdate() {
	super.onUpdate();
	if (isAIDisabled()) return;

	if ((ticksExisted % RandUtil.nextInt(100, 200)) == 0)
		playSound(ModSounds.HALLOWED_SPIRIT, RandUtil.nextFloat(), RandUtil.nextFloat());

	fallDistance = 0;

	EntityPlayer farPlayer = world.getNearestPlayerNotCreative(this, 300);
	setAttackTarget(farPlayer);
	if (getAttackTarget() != null) {
		noClip = true;
		Vec3d direction = getPositionVector().subtract(getAttackTarget().getPositionVector()).normalize();
		motionX = direction.x * -0.1;
		motionY = direction.y * -0.1;
		motionZ = direction.z * -0.1;
		rotationYaw = (float) (((-MathHelper.atan2(direction.x, direction.z) * 180) / Math.PI) - 180) / 2;
	} else {
		if (!collidedVertically) {
			motionY = 0;
		}
		noClip = false;
	}

	EntityPlayer player = getAttackTarget() == null ? null : world.getNearestPlayerNotCreative(this, 2);
	EntityPlayer closePlayer = getAttackTarget() == null ? null : world.getNearestPlayerNotCreative(this, 30);
	boolean angry = player != null;

	ClientRunnable.run(new ClientRunnable() {
		@Override
		@SideOnly(Side.CLIENT)
		public void runIfClient() {
			if ((closePlayer != null) && !angry)
				LibParticles.SPIRIT_WIGHT_FLAME_FAR(world, getPositionVector().add(0, getEyeHeight(), 0));
			else if (angry)
				LibParticles.SPIRIT_WIGHT_FLAME_CLOSE(world, getPositionVector().add(0, getEyeHeight(), 0));
			else LibParticles.SPIRIT_WIGHT_FLAME_NORMAL(world, getPositionVector().add(0, getEyeHeight(), 0));
		}
	});

	if (angry) {
		player.attackEntityFrom(DamageSource.MAGIC, 0.15f);
		player.hurtResistantTime = 0;
	}
}
 
Example 11
Source File: EntityEasterCow.java    From Moo-Fluids with GNU General Public License v3.0 4 votes vote down vote up
private void calculateRotationYaw(double x, double z) {
  rotationYaw = (float) (MathHelper.atan2(z - posZ, x - posX) * (180D / Math.PI)) - 90.0F;
}
 
Example 12
Source File: FlyingMoveHelper.java    From EnderZoo with Creative Commons Zero v1.0 Universal 4 votes vote down vote up
@Override
public void onUpdateMoveHelper() {

  if (!entity.getNavigator().noPath()) {
    double xDelta = posX - entity.posX;
    double yDelta = posY - entity.posY;
    double zDelta = posZ - entity.posZ;

    float moveFactor = 1;
    float moveSpeed = (float) (speed * entity.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).getAttributeValue());
    entity.setAIMoveSpeed(entity.getAIMoveSpeed() + (moveSpeed - entity.getAIMoveSpeed()) * moveFactor);

    double distSq = xDelta * xDelta + yDelta * yDelta + zDelta * zDelta;
    double dist = MathHelper.sqrt(distSq);
    yDelta = yDelta / dist;
    if (yDelta > 0) {
      // Ensure enough lift to get up to the target
      yDelta = Math.max(0.1, yDelta);
    }
    double yMove = entity.getAIMoveSpeed() * yDelta * mob.getMaxClimbRate();
    entity.motionY += yMove;
    // Limit max downward speed
    if (!entity.isDead && !entity.onGround && entity.motionY < -maxDescentSpeed) {
      entity.motionY = -maxDescentSpeed;
    }

    // Limit crazy spinning when going straight down
    float tr = mob.getMaxTurnRate();
    if (yMove < -0.12) {
      tr = 10;
    }
    float yawAngle = (float) (MathHelper.atan2(zDelta, xDelta) * 180.0D / Math.PI) - 90.0F;
    entity.rotationYaw = limitAngle(entity.rotationYaw, yawAngle, tr);
    entity.renderYawOffset = entity.rotationYaw;

    // Look
    double d7 = entity.posX + (xDelta / dist * 2.0D);
    double d8 = entity.getEyeHeight() + entity.posY + (yDelta / dist * 1.0D);
    double d9 = entity.posZ + (zDelta / dist * 2.0D);

    EntityLookHelper entitylookhelper = entity.getLookHelper();
    double lookX = entitylookhelper.getLookPosX();
    double lookY = entitylookhelper.getLookPosY();
    double lookZ = entitylookhelper.getLookPosZ();

    if (!entitylookhelper.getIsLooking()) {
      lookX = d7;
      lookY = d8;
      lookZ = d9;
    }
    entity.getLookHelper().setLookPosition(lookX + (d7 - lookX) * 0.125D, lookY + (d8 - lookY) * 0.125D, lookZ + (d9 - lookZ) * 0.125D, 10.0F, 40.0F);
  } else {
    entity.setAIMoveSpeed(0.0F);
  }
}