How to get a location relative to the direction of an entity?

Discussion in 'Spigot Plugin Development' started by nelson2tm, Apr 25, 2017.

  1. I want to get a location relative to the direction (pitch and yaw) of an entity, for example 1 block to the right of it. How would I do this? Here is an image of what I'm trying to achieve:
    [​IMG]
    I know you can get the rotation of the player using
    Code (Text):
    player.getLocation().getRotation()
    However I have no idea how to get a location relative to the position of the player and the rotation. What could I do to get this location?
     
  2. A little more specifics?

    How are you making the blue location from the given rotation. Rotating it a certain number of degrees?
     
  3. What I want to do is to get the (blue) location 1 block to the right of the player, relative to where he is looking. For example, when the player is looking at something with both pitch and yaw being 0°, get the location 1 block to the right of the player at 90°, and when he is looking with both the pitch and the yaw being 10°, get the location of 1 block to the right of the player at 100° etc.
     
  4. NathanWolf

    Supporter

    There is a method in EffectLib you can look at that may help you out:

    https://github.com/Slikey/EffectLib...de/slikey/effectlib/util/VectorUtils.java#L76

    Example of use:

    Code (Text):
    // Create a vector that is 1 block to the right
    Vector totheRight = new Vector(0, 0, 1);
    // Rotate this vector to orient it to player direction
    Location location = player.getLocation();
    toTheRight = rotateVector(toTheRight, location.getYaw(), location.getPitch();
    As you can see in the code comments I did not write the math so I'm afraid I can't explain it well, but I can verify that it works :)
     
  5. On interact sets block to the right to a diamond block. Just add JAMA to your project.
    Code (Text):
        @EventHandler
        public void click(PlayerInteractEvent event)
        {
            Player player = event.getPlayer();
            Vector orientation = player.getEyeLocation().getDirection();
            Matrix point = new Matrix(new double[][]{vecToArray(new Vector(1, 0, 0))}).transpose();
            Matrix pointToTheRight = align(point,orientation).transpose();
            double blockX = pointToTheRight.get(0,0);
            double blockY = pointToTheRight.get(0,1);
            double blockZ = pointToTheRight.get(0,2);
            Location loc = player.getLocation().add(new Vector(blockX, blockY, blockZ));
            player.getWorld().getBlockAt(loc).setType(Material.DIAMOND_BLOCK);
        }

        Matrix align(Matrix point, Vector orientation)
        {
            //normalizing all vectors is important
            orientation.normalize();
            //using the default orientation of our effect as a way to determine the current orthogonal unit vectors
            Vector tempUp = new Vector(0, 1, 0);
            Vector right = orientation.getCrossProduct(tempUp);
            right.normalize();
            Vector up = right.getCrossProduct(orientation);
            up.normalize();
            //create a rotation matrix from the orthogonal unit vectors
            double[][] rotationArray = {
                    vecToArray(right),
                    vecToArray(orientation),
                    vecToArray(up)
            };
            Matrix rotation = new Matrix(rotationArray).transpose();
            //apply the rotation matrix to the point to get the new point
            return rotation.times(point);
        }

        double[] vecToArray(Vector vector)
        {
            return new double[]{
                    vector.getX(), vector.getY(), vector.getZ()
            };
        }
    JAMA Maven
    https://mvnrepository.com/artifact/gov.nist.math/jama/1.0.3
     
    • Agree Agree x 1