Best way to work with distances

Discussion in 'Plugin Development' started by kameronn, Jan 29, 2017.

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

    kameronn

    What I am doing is I'm making a fake player that follows the player who spawned it in around. However the problem is that I want him to stop moving after he's a certain amount of blocks near him and keep moving when hes not in that range.

    This would obviously be simple but the main problem I am experiencing is I want the fake player to follow the other player in a consistent straight path and not going backwards when the player is moving toward it. I'm not quite sure how to do this

    TL;DR
    What's the best way to check if a fake player is moving backwards or the player is moving towards the fake player.

    Second Problem: The fake player is only moving when the player is moving.

    Current Code:
    Code:
        public void look(float yaw, float pitch) {
            WrapperPlayServerEntityMoveLook move = new WrapperPlayServerEntityMoveLook();
            move.setEntityID(getId());
            Location loc = new Location(player.getWorld(), locX, locY, locZ);
    
            double x = locX - player.getLocation().getX();
            double y = locY - player.getLocation().getY();
            double z = locZ - player.getLocation().getZ();
    
            //if (getDistance(player.getLocation(), loc) > 4) {
              
                x = -x;
                y = -y;
                z = -z;
              
                locX = locX + x;
                locY = locY + y;
                locZ = locZ + z;
    
                move.setDx(x);
                //move.setDy(y);
                move.setDz(z);
              
                System.out.println("X: " + x + " Y: " + y + " Z: " + z);
    
            //}
    
            move.setYaw(getLocalAngle(new Vector(locX, 0, locZ), player.getLocation().toVector()));
            move.setPitch(-player.getLocation().getPitch());
            move.sendPacket(player);
    
        }
    This is being ran every 3 ticks
     
  2. Offline

    Drkmaster83

    As for the first part of your problem:
    I believe that the player's location contains a getDirection() method which returns a unit vector which you can get the angle between by invoking Vector#angle(), which returns the angle in radians (which you can then convert to degrees using the Math class). From there, you can see if the angle between them is greater than 180 and less than or equal to 360. (Might not be the right range, but it might be).
     
Thread Status:
Not open for further replies.

Share This Page