Solved Get if an arrow hits a shield.

Discussion in 'Spigot Plugin Development' started by Macaron, Jun 17, 2016.

  1. Since I want to tweak the mechanic of shield blocking a projectile, I want to know if a the shield player is looking at the arrow's direction. (I couldn't find method like arrow.getBlocked())

    Here's the code I have so far:

    Code (Text):
    Vector target = victim.getLocation().toVector();
    Vector arrow = attackerEntity.getVelocity();

            target = target.normalize();
            arrow = arrow.normalize();

    //Tweaked from
            return target.getX() * arrow.getX() + target.getZ() * arrow.getZ() < 0.5;
    The thing is - it's as good as not working. Sometime it returns true even if the arrow shot in the back, sometime it returns false even if player is looking at the arrow.

    I must have to admit I'm terrible at vector calculation...
    #1 Macaron, Jun 17, 2016
    Last edited: Jun 17, 2016
  2. mathematically you would get the angle between the vectors with the cartesian product. I guess that is what you wanted to do? You would like to have an angle close to 180°.

    Maybe you could first check the direction the player is looking at.

    Edit: Also i dont know what you think you achieve by turning the Location into a vector... That would give you the vector from (0,0,0) to the players location and has nothing to do with what you are trying to do.
  3. I just tried player.getVelocity(), but the vector returned has x=0 and z=0 when calculating the cartesian product.
  4. Got it! Changing target to "Vector target = victim.getLocation().getDirection();" solves the issue.
    Blame my stupidity...
  5. Why would you use the players velocity? Player could be standing still or walking backwards xD His velocity is irrelevant.

    You need the info whether the arrow hits the shield or not, so you need the direction the shield is positioned in and you would also need its dimensions... Plus info about the arrow.
    You are listening to EntityDamagedByEntityEvent? I would try getting the position of the shield from the yaw and position of the player and then compare to the direction the arrow had. Then cancel the event when they are matching.
    Not sure this is easy to do...

    Edit: ok good, you got it. nvm then ^^