Solved Find coords 10 blocks infront of direction looking

Discussion in 'Spigot Plugin Development' started by brian310, Dec 30, 2019.

Thread Status:
Not open for further replies.
  1. Hey anyone know how I could possibly get the coordinates of a point 10 blocks infront of where a armorstand's head is looking? [​IMG]
  2. Player#getLineOfSight() will work for this. You can use null for the first argument (just cast to Set<Material>) and the second arg is the range.

    Another possibility if you want to be more DIY is to get the eye location of the player, get the direction, get the block X meters away, and make a loop to get every block in that line until you reach Y blocks away.
  3. getlineofsight wont work as that only gets the block coords, It wouldn't be able to get precise cords like 12.8 5.2 23.0 & also I'm using armorstands not players.
  4. p.getlocation get direction multiply 10
  5. Code (Java):
      public Location getLooking(LivingEntity livingEntity, double distance) {
        // Getting eye location
        Location eyeLocation = livingEntity.getEyeLocation();
        // Get dir the eyes are looking at
        Vector direction = eyeLocation.getDirection();
        // Normalize (set lenght to 1) then scale
        Vector scaledDir = direction.clone().normalize().multiply(distance);
        // Adding scaled direction to eye location
        World world = eyeLocation.getWorld();
        double x = scaledDir.getX() + eyeLocation.getX();
        double y = scaledDir.getY() + eyeLocation.getY();
        double z = scaledDir.getZ() + eyeLocation.getZ();

        return new Location(world, x, y, z);
    PS if you want the first Block or Entity in this line you can just raytrace.
    • Like Like x 1
  6. There are a few things I don't like about this approach
    both getEyeLocation, getDirection creates a new object, I would just reuse them whenever possible like this:
    Code (Text):
    Location eyeLocation = p.getEyeLocation();
    Vector direction = eyeLocation.getDirection();

    return eyeLocation.add(direction.normalize().multiply(distance));
  7. You don't need to normalize the direction since Location#getDirection returns an unit vector
  8. Oh, good to know
  9. That works for player but for armorstands it just gets the rotation of it instead of the head pose for some reason.
  10. 1) I'm only creating exactly one more Object (Pointers aren't new Objects just new references)
    2) Its bad practice to modify hard linked objects like an entities Location. (Danger of teleporting the entity etc)
    3) this is a extra detailed example to comment my steps
  11. actually no, the api does not give you access to linked objects and instead when you call getLocation it creates a new object like this:

    Code (Text):
     public Location getLocation() {
    return new Location(getWorld(), this.entity.locX(), this.entity.locY(), this.entity.locZ(), this.entity.getBukkitYaw(), this.entity.pitch);
    same for getDirection()
    if you don't believe me then look into the craftbukkit yourself or you can always call 2 getLocations and see if they are ==
  12. Do you have the yaw and pitch of the armorstand head?
  13. Yes.
  14. Huh.. you are right.
    Since when?
    Still. I only call it the same amount you do ^^
  15. then just do this for direction tho i'm pretty sure it should work as is for armorstands:
    Code (Text):
        Vector vector = new Vector();
        double rotX = //yaw
        double rotY = //pitch
        double xz = Math.cos(Math.toRadians(rotY));
        vector.setX(-xz * Math.sin(Math.toRadians(rotX)));
        vector.setZ(xz * Math.cos(Math.toRadians(rotX)));
    #15 Fr33styler, Dec 30, 2019
    Last edited: Dec 30, 2019
    • Winner Winner x 1
  16. the first time i checked was in 1.7.10 but i think it was always like this to prevent what you said earlier
  17. Thanks that worked.
Thread Status:
Not open for further replies.