Solved Player to Location angle calculation

Discussion in 'Plugin Development' started by TheTinySpider, Jan 5, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    TheTinySpider

    Hey

    I would like to know how you can calculate the angle between where the player is looking and a specific point in the world. Just the Yaw (2d) is fine, I would prefer both (2d & 3d). I've tried a few things with dot products and stuff, but I'm not that good with Vectors.
    If anyone would like to help out, appreciated!

    Here is a visual representation of what I want (2d):
    [​IMG]
     
  2. Offline

    TheTinySpider

  3. @TheTinySpider Location has a getDirection() method that returns a Vector - have you looked into that?
     
  4. Offline

    TheTinySpider

    Yes I have, but that returns a normalized vector which I don't know how to compare with a directionless point
     
  5. Offline

    RingOfStorms

    I would create a vector between the player and the block, and then simple call the angle method in vector. Note that the angle is in radians and if you want it in degrees then you can use Math.toDegrees(double radians) to convert it.
    Code:java
    1.  
    2. Player player = //your player
    3. Location blockLoc = //your block's location
    4. //I cloned blockLoc incase you use it somewhere else, if you don't plan on using it again then you can remove the clone
    5. player.getLocation().getDirection().angle(blockLoc.clone().subtract(player.getEyeLocation()).toVector())
    6.  
     
  6. Offline

    TheTinySpider

    Alright I got it, thanks to @fireblast709!

    For anyone interested:
    Code:
    Vector a = position.toVector().subtract(location.toVector()).normalize();
    Vector b = location.getDirection();
    double angle = Math.acos(a.dot(b));
    angle = Math.toDegrees(angle);
     
Thread Status:
Not open for further replies.

Share This Page