Java Code Examples for org.bukkit.util.Vector#dot()

The following examples show how to use org.bukkit.util.Vector#dot() . 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: VectorMath.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
public static Vector rot(Vector vector, Vector axis, double angle) {
	double sin = Math.sin(angle * DEG_TO_RAD);
	double cos = Math.cos(angle * DEG_TO_RAD);
	Vector a = axis.clone().normalize();
	double ax = a.getX();
	double ay = a.getY();
	double az = a.getZ();
	Vector rotx = new Vector(cos+ax*ax*(1-cos), ax*ay*(1-cos)-az*sin, ax*az*(1-cos)+ay*sin);
	Vector roty = new Vector(ay*ax*(1-cos)+az*sin, cos+ay*ay*(1-cos), ay*az*(1-cos)-ax*sin);
	Vector rotz = new Vector(az*ax*(1-cos)-ay*sin, az*ay*(1-cos)+ax*sin, cos+az*az*(1-cos));
	double x = rotx.dot(vector);
	double y = roty.dot(vector);
	double z = rotz.dot(vector);
	vector.setX(x).setY(y).setZ(z);
	return vector;
}
 
Example 2
Source File: TargetHelper.java    From ActionHealth with MIT License 5 votes vote down vote up
/**
 * <p>Gets all entities the player is looking at within the range using
 * the given tolerance.</p>
 *
 * @param source    living entity to get the targets of
 * @param range     maximum range to check
 * @param tolerance tolerance of the line calculation
 * @return all entities in the player's vision line
 */
public List<LivingEntity> getLivingTargets(LivingEntity source, double range, double tolerance) {
    if (source == null) {
        return new ArrayList<>();
    }

    List<Entity> list = source.getNearbyEntities(range, range, range);
    List<LivingEntity> targets = new ArrayList<LivingEntity>();

    Vector facing = source.getLocation().getDirection();
    double fLengthSq = facing.lengthSquared();

    for (Entity entity : list) {
        if (!isInFront(source, entity) || !(entity instanceof LivingEntity)) continue;

        Vector relative = entity.getLocation().subtract(source.getLocation()).toVector();
        double dot = relative.dot(facing);
        double rLengthSq = relative.lengthSquared();
        double cosSquared = (dot * dot) / (rLengthSq * fLengthSq);
        double sinSquared = 1 - cosSquared;
        double dSquared = rLengthSq * sinSquared;

        // If close enough to vision line, return the entity
        if (dSquared < tolerance) targets.add((LivingEntity) entity);
    }

    return targets;
}
 
Example 3
Source File: TargetHelper.java    From ActionHealth with MIT License 5 votes vote down vote up
/**
 * Gets the targets in a cone
 *
 * @param source entity to get the targets for
 * @param arc    arc angle of the cone
 * @param range  range of the cone
 * @return list of targets
 */
public List<LivingEntity> getConeTargets(LivingEntity source, double arc, double range) {
    List<LivingEntity> targets = new ArrayList<LivingEntity>();
    List<Entity> list = source.getNearbyEntities(range, range, range);
    if (arc <= 0) return targets;

    // Initialize values
    Vector dir = source.getLocation().getDirection();
    dir.setY(0);
    double cos = Math.cos(arc * Math.PI / 180);
    double cosSq = cos * cos;

    // Get the targets in the cone
    for (Entity entity : list) {
        if (entity instanceof LivingEntity) {

            // Greater than 360 degrees is all targets
            if (arc >= 360) {
                targets.add((LivingEntity) entity);
            }

            // Otherwise, select targets based on dot product
            else {
                Vector relative = entity.getLocation().subtract(source.getLocation()).toVector();
                relative.setY(0);
                double dot = relative.dot(dir);
                double value = dot * dot / relative.lengthSquared();
                if (arc < 180 && dot > 0 && value >= cosSq) targets.add((LivingEntity) entity);
                else if (arc >= 180 && (dot > 0 || dot <= cosSq)) targets.add((LivingEntity) entity);
            }
        }
    }

    return targets;
}
 
Example 4
Source File: TargetHelper.java    From ActionHealth with MIT License 5 votes vote down vote up
/**
 * Checks if the entity is in front of the entity
 *
 * @param entity entity to check for
 * @param target target to check against
 * @return true if the target is in front of the entity
 */
public boolean isInFront(Entity entity, Entity target) {
    if (entity.getWorld() != target.getWorld())
        return false;

    // Get the necessary vectors
    Vector facing = entity.getLocation().getDirection();
    Vector relative = target.getLocation().subtract(entity.getLocation()).toVector();

    // If the dot product is positive, the target is in front
    return facing.dot(relative) >= main.configStore.lookDot;
}
 
Example 5
Source File: TargetHelper.java    From ActionHealth with MIT License 5 votes vote down vote up
/**
 * Checks if the entity is in front of the entity restricted to the given angle
 *
 * @param entity entity to check for
 * @param target target to check against
 * @param angle  angle to restrict it to (0-360)
 * @return true if the target is in front of the entity
 */
public boolean isInFront(Entity entity, Entity target, double angle) {
    if (angle <= 0) return false;
    if (angle >= 360) return true;

    // Get the necessary data
    double dotTarget = Math.cos(angle);
    Vector facing = entity.getLocation().getDirection();
    Vector relative = target.getLocation().subtract(entity.getLocation()).toVector().normalize();

    // Compare the target dot product with the actual result
    return facing.dot(relative) >= dotTarget;
}
 
Example 6
Source File: TargetHelper.java    From ActionHealth with MIT License 5 votes vote down vote up
/**
 * Checks if the entity is behind the player restricted to the given angle
 *
 * @param entity entity to check for
 * @param target target to check against
 * @param angle  angle to restrict it to (0-360)
 * @return true if the target is behind the entity
 */
public boolean isBehind(Entity entity, Entity target, double angle) {
    if (angle <= 0) return false;
    if (angle >= 360) return true;

    // Get the necessary data
    double dotTarget = Math.cos(angle);
    Vector facing = entity.getLocation().getDirection();
    Vector relative = entity.getLocation().subtract(target.getLocation()).toVector().normalize();

    // Compare the target dot product and the actual result
    return facing.dot(relative) >= dotTarget;
}