Angle between 2 Vectors

Discussion in 'Spigot Plugin Development' started by xXPlayer2000, Dec 8, 2018.

  1. Hey Guys i want to get a Angle between 2 Vectors.
    Reason: I want to know in which direction a player is. (south, west etc)

    I'm using :
    but the angles are just very low and i also searched for it here and i found some methods but they all dont work that good like:
    Code (Text):
    Location origin = p.getEyeLocation();
            Vector target = point.toVector();
            origin.setDirection(target.subtract(origin.toVector())); //set the origin's direction to be the direction vector between point A and B.
            float yaw = origin.getYaw(); //yay yaw
    Does anyone know an option?
  2. Code (Java):
    double angle = vector1.angle(vector2);
  3. Nope also wrong unfortunate (Also changing ° when i just move back but not to the side and that shouldnt be) and the ° are also everytime wrong
  4. It is correct, you're just supplying the wrong arguments. Right now, it won't work obviously. You're just turning the locations into vectors, that'll give incorrect values. Perhaps you want a direction in between both entities? That way you could calculate the correct angle.

    Code (Java):

    Location target = /*whatever*/;
    Location source = /*whatever*/;
    Vector inBetween = target.clone().subtract(source).toVector();
    Vector forward = source.getDirection();
    double angle = forward.angle(inBetween);
    This will give the angle in between the source's view direction and the direction to the target relative to the source.
  5. still wrong sometimes (the cardinal directions) at this point i was at my own but cant fix that because its to often wrong
  6. Define "wrong". Show what you're doing with it. That code will give the angle between those 2 vectors, there is no way it's wrong. The only possiblity is that you're expecting results from it that it cannot give you.
  7. I want to get the cardinal direction from it and it often is correct but sometimes it is east, south etc. when it isnt
  8. Do need to remember that an angle is not only horizontal. Please show how you're calculating the cardinal direction.
  9. I'm just using the angle, making it to degress and then using this method:
    Code (Text):
    private BlockFace getDirection(double rot) {
            if (0 <= rot && rot < 22.5) {
                return BlockFace.NORTH;
            } else if (22.5 <= rot && rot < 67.5) {
                return BlockFace.NORTH_EAST;
            } else if (67.5 <= rot && rot < 112.5) {
                return BlockFace.EAST;
            } else if (112.5 <= rot && rot < 157.5) {
                return BlockFace.SOUTH_EAST;
            } else if (157.5 <= rot && rot < 202.5) {
                return BlockFace.SOUTH;
            } else if (202.5 <= rot && rot < 247.5) {
                return BlockFace.SOUTH_WEST;
            } else if (247.5 <= rot && rot < 292.5) {
                return BlockFace.WEST;
            } else if (292.5 <= rot && rot < 337.5) {
                return BlockFace.NORTH_WEST;
            } else if (337.5 <= rot && rot < 360.0) {
                return BlockFace.NORTH;
            } else {
                return null;
  10. A. Why do you need two vectors to find which direction someone is facing?
    B. Can't you just use one vector's x and z components to find the cardinal direction?
  11. how should that work lol
  12. Praya


    you just need player's yaw to find out the direction of player
  13. yeah i know this but i want the direction between them
  14. Use the x and z components to check the direction, x is east (positive) / west (negative) and z is south (positive) / north (negative)
  15. You clearly haven't read what I said. The angle is relative, you're treating it like it's stationary.
  16. The obvious question which nobody has asked and you haven't answered is probably between whom?
  17. 2 players
  18. so what do i need to do i tried with the math i know from the school but that doesnt work at all
  19. Okay, so now that we know what you want, let's go back to here:
    Code (Java):

    // The start of finnbon's code
    Location target = /*whatever*/;
    Location source = /*whatever*/;
    Vector inBetween = target.clone().subtract(source).toVector();

    // If you've ever used polar coordinates, this is like that. Otherwise, just know that it's calculating the angle equivalent of a direction.
    // The +90 is because Minecraft does angles weird
    double angle = Math.atan2(inBetween.getZ(),inBetween.getX()) / 2 / Math.PI * 360 + 90;

    // Angles can be negative too, so let's fix that
    angle = (angle + 360) % 360

  20. Still problems[​IMG] (bc they dont load)
    The block u see is where my location is, sometimes its true, sometimes not

Share This Page