Checking whether a player is "behind" another

Discussion in 'Spigot Plugin Development' started by Alfie, Jul 1, 2016.

  1. Alfie



    For a combat class on my server, I need players to be able to "backstab" each other. But I can't seem to be able to work out a way to check if a player is behind another. This is in relation to the victim's head direction - I've looked around and only found this:

    Code (Text):
    attacker.getLocation().getDirection().angle(victim.getLocation().getDirection()) / 180 * Math.PI;
    which spits out values that take into account vertical eye location - I just need to check horizontal.

    - AlzC
  2. FormallyMyles


    Just check the distance between the location of the players?
  3. Alfie


    Did you read my question? I need to take into direction - apologies if I didn't make that clear.
  4. If the attacker direction vector is aproximatly the same as the defender direction vector it should be a backstab.
    (dont compare the Y values)
  5. FormallyMyles


    Yeah the code you have does take into account direction? You're comparing the direction of the 2 players? Which from there you need to check the range by using their location?
  6. Alfie


    Solved. Ended up using this code (courtesy of Kenny)

    Code (Text):

        private boolean isPlayerInCone(Player p, Player clicked, double coneAngle, double radius) {
            final double coneArea = Math.tan(coneAngle) * Math.tan(coneAngle), radiusSquared = radius * radius;
            org.bukkit.util.Vector n = p.getLocation().toVector().subtract(clicked.getLocation().toVector()).normalize();
            return (clicked.getLocation().getDirection().normalize().crossProduct(n).lengthSquared() <= coneArea // within cone
                    && clicked.getLocation().distanceSquared(p.getLocation()) <= radiusSquared // within radius
                    && clicked.getLocation().getDirection().dot(n) >= 0); // same direction    

    • Like Like x 1