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.
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.
just get vector of the damager and damagee and compute the dot-product..if it's positive, he's behind.
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.
That would only work if they were facing the EXACT same way. I just need to test if they are behind, not directly behind.
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
I have already seen that. As a new java developer, I can't just look at that and be like "Ok that makes sense."
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);
if you have two vectors v1 (x1, y1, z1) and v2 (x2, v2, z2), then dot product is x1*x2 + y1*y2 + z1*z2
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.
Come on, it's really not that hard. Spoiler 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: Spoiler 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.} } }
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?
who knows. his code might need to draw max performance possible by eliminating the overhead of method calls., who knows..
Sometimes it pays off to know whats going on behind the scenes. For vk to suggest the manual operation is perfectly valid.