Test If Damage Was From Behind?

Discussion in 'Spigot Plugin Development' started by eli.musgrove9, May 4, 2015.

  1. I need to add a backstab. So, I need to test if an entity damaged by a player was hit from behind. How can I test this? I can't find anything on the forums.
     
  2. get the entity, which is the damager of the EntityDamagedByEntity event and check the direction vector of this damager. If it's facing the same direction, then he's behind you.
     
    • Like Like x 1
  3. just get vector of the damager and damagee and compute the dot-product..if it's positive, he's behind.
     
  4. Get the locations of the hitter and the one that got damaged. Then get the direction that they're facing. If you are both facing the same direction, it is most likely a hit from the back.
     
  5. That would only work if they were facing the EXACT same way. I just need to test if they are behind, not directly behind.
     
  6. How do I "Compute the dot product"?? XD
     
  7. I believe getDirection will return either 0,1,2,3 (representing North East South West)

    EDIT:
    Nevermind, it returns "a vector pointing the direction of this location's pitch and yaw"
    Source: Spigot Docs
     
  8. Ok well thanks for the help! Always appreciated!
     
  9. gigosaurus

    Supporter

  10. gigosaurus

    Supporter

    As a new java developer attempting to use an API, one of the first things you should have learnt is how to understand javadocs - they tell you everything the API has to offer, and how to use it.

    That link shows you the method you should use to calculate the dot product between two vectors, which is what you asked.

    double dotProduct = vector1.dot(vector2);
     
    #11 gigosaurus, May 4, 2015
    Last edited: May 4, 2015
  11. if you have two vectors v1 (x1, y1, z1) and v2 (x2, v2, z2), then dot product is x1*x2 + y1*y2 + z1*z2
     
  12. Ok thanks! Do you recommend an event? I am currently using EntityDamageEvent.
     
  13. since you might want to detect backstabber using arrow as well, i would say, use EntityDamagedByEntityEvent. and if the damager is an instance of Projectile, get the shooter as the damager.
     
  14. gigosaurus

    Supporter

    Come on, it's really not that hard.
    Code (Java):
    @EventHandler
    public void onEntityAttacked(EntityDamageByEntityEvent event) {
        //only interested in players getting attacked by players
        if ((event.getEntity() instanceof Player) && (event.getDamager() instanceof Player)) {
            //get each player's direction
            Vector attackerDirection = event.getDamager().getLocation().getDirection();
            Vector victimDirection = event.getEntity().getLocation().getDirection();
            //determine if the dot product between the vectors is greater than 0
            if (attackerDirection.dot(victimDirection) > 0) {
                //player was backstabbed.}
            }
        }
    }
     
    Or if you want to include projectiles (such as arrows) that were launched by a player:
    Code (Java):
    @EventHandler
    public void onEntityAttacked(EntityDamageByEntityEvent event) {
        //only interested in players getting attacked
        if (event.getEntity() instanceof Player) {
            //if the damager isn't a player, check if it is a projectile from a player
            if (!(event.getDamager() instanceof Player)) {
                if (event.getDamager() instanceof Projectile) {
                    if (!(((Projectile) event.getDamager()).getShooter() instanceof Player)) {
                        //projectile didn't belong to a player
                        return;
                    }
               } else {
                   //damager isn't a player or projectilereturn;
               }
        }
        //get each damager's direction
        Vector attackerDirection = event.getDamager().getLocation().getDirection();
        Vector victimDirection = event.getEntity().getLocation().getDirection();
        //determine if the dot product between the vectors is greater than 0
        if (attackerDirection.dot(victimDirection) > 0) {
            //player was backstabbed.}
        }
    }
     
  15. Might not be difficult for you but might be for others.
     
    • Agree Agree x 4
  16. gigosaurus

    Supporter

    I fully appreciate that, but when someone you try to help completely ignores your solution and opts for a more difficult one...
    There's a method to calculate the dot product. Why would you ever manually calculate it instead?
     
  17. who knows. his code might need to draw max performance possible by eliminating the overhead of method calls., who knows..
     
    • Agree Agree x 1
    • Funny Funny x 1
  18. That was really quite a satisfying start of the day, to read that. Thank you.
     
  19. Sometimes it pays off to know whats going on behind the scenes. For vk to suggest the manual operation is perfectly valid.
     
    • Agree Agree x 1