Paste #19466: Rotation.java (w/comments)

Date: 2015/08/16 22:10:58 UTC-07:00
Type: Plain Text

View Raw Paste Download This Paste
Copy Link


    public static Location faceLocation(Location from, Location at) {
        Vector direction = at.toVector().subtract(from.toVector()).normalize();
        Location newLocation = from.clone();
        newLocation.setYaw(180 - (float) Math.toDegrees(Math.atan2(direction.getX(), direction.getZ())));
        newLocation.setPitch(90 - (float) Math.toDegrees(Math.acos(direction.getY())));
//        double xDiff = at.getX() - from.getX();
//        double yDiff = at.getY() - from.getY();
//        double zDiff = at.getZ() - from.getZ();
//
//        double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff);
//        double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff);
//
//        double yaw = Math.toDegrees(Math.acos(xDiff / distanceXZ));
//        double pitch = Math.toDegrees(Math.acos(yDiff / distanceY)) - 70;
//
//        if (zDiff < 0.0) {
//            yaw = yaw + (Math.abs(180 - yaw) * 2);
//        }
//
//        return new Location(from.getWorld(), from.getX(), from.getY(), from.getZ(), (float)yaw - 90, (float)pitch);
        return newLocation;
    }