fixed 0-length vector normalization issue

This commit is contained in:
Indyuce 2019-08-31 15:16:23 +02:00
parent 5cb325f41f
commit 75a2327632
4 changed files with 16 additions and 21 deletions

View File

@ -43,6 +43,17 @@ public class MMOUtils {
}
}
/*
* used by many plugin abilities and mecanisms. sometimes vector cannot be
* normalized because its length is equal to 0 (normalizing a vector just
* divides the vector coordinates by its length) however you cannot divide
* by 0. just return vec if its null. lengthSquared better for performance
* because it has no square root
*/
public static Vector normalize(Vector vector) {
return vector.lengthSquared() == 0 ? vector : vector.normalize();
}
public static String getProgressBar(double ratio, int n, String barChar) {
String bar = "";
for (int k = 0; k < n; k++)

View File

@ -60,16 +60,8 @@ public class Black_Hole extends Ability {
for (Entity entity : MMOUtils.getNearbyChunkEntities(loc))
if (entity.getLocation().distanceSquared(loc) < Math.pow(radius, 2) && MMOUtils.canDamage(stats.getPlayer(), entity))
entity.setVelocity(normalizeIfNotNull(loc.clone().subtract(entity.getLocation()).toVector()).multiply(.5));
entity.setVelocity(MMOUtils.normalize(loc.clone().subtract(entity.getLocation()).toVector()).multiply(.5));
}
}.runTaskTimer(MMOItems.plugin, 0, 1);
}
/*
* if the vector is null, you can't normalize it because you cannot divide
* by 0.
*/
private Vector normalizeIfNotNull(Vector vector) {
return vector.length() == 0 ? vector : vector.normalize();
}
}

View File

@ -5,7 +5,6 @@ import org.bukkit.Particle;
import org.bukkit.Sound;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.util.Vector;
import net.Indyuce.mmoitems.MMOUtils;
import net.Indyuce.mmoitems.api.Ability;
@ -45,15 +44,7 @@ public class Minor_Explosion extends Ability {
for (Entity entity : MMOUtils.getNearbyChunkEntities(loc))
if (entity.getLocation().distanceSquared(loc) < radiusSquared && MMOUtils.canDamage(stats.getPlayer(), entity)) {
new AttackResult(damage, DamageType.SKILL, DamageType.MAGICAL).applyEffectsAndDamage(stats, null, (LivingEntity) entity);
entity.setVelocity(normalizeIfNotNull(entity.getLocation().subtract(loc).toVector().setY(0)).setY(.2).multiply(2 * knockback));
entity.setVelocity(MMOUtils.normalize(entity.getLocation().subtract(loc).toVector().setY(0)).setY(.2).multiply(2 * knockback));
}
}
/*
* if the vector is null, you can't normalize it because you cannot divide
* by 0.
*/
private Vector normalizeIfNotNull(Vector vector) {
return vector.length() == 0 ? vector : vector.normalize();
}
}

View File

@ -22,6 +22,7 @@ import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.util.Vector;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.MMOUtils;
import net.Indyuce.mmoitems.api.Message;
import net.Indyuce.mmoitems.api.player.PlayerData;
import net.Indyuce.mmoitems.api.player.PlayerData.CooldownType;
@ -67,7 +68,7 @@ public class MitigationListener implements Listener {
player.getWorld().spawnParticle(Particle.EXPLOSION_NORMAL, player.getLocation(), 16, 0, 0, 0, .06);
if (event instanceof EntityDamageByEntityEvent && ((EntityDamageByEntityEvent) event).getDamager() instanceof LivingEntity) {
LivingEntity attacker = (LivingEntity) ((EntityDamageByEntityEvent) event).getDamager();
attacker.setVelocity(attacker.getLocation().toVector().subtract(player.getLocation().toVector()).normalize().setY(.35).multiply(MMOItems.plugin.getConfig().getDouble("mitigation.parry.knockback-force")));
attacker.setVelocity(MMOUtils.normalize(attacker.getLocation().toVector().subtract(player.getLocation().toVector())).setY(.35).multiply(MMOItems.plugin.getConfig().getDouble("mitigation.parry.knockback-force")));
}
return;
}
@ -90,7 +91,7 @@ public class MitigationListener implements Listener {
}
private Vector getVector(Player player, EntityDamageEvent event) {
return event instanceof EntityDamageByEntityEvent ? player.getLocation().subtract(((EntityDamageByEntityEvent) event).getDamager().getLocation()).toVector().normalize() : player.getEyeLocation().getDirection();
return event instanceof EntityDamageByEntityEvent ? MMOUtils.normalize(player.getLocation().subtract(((EntityDamageByEntityEvent) event).getDamager().getLocation()).toVector()) : player.getEyeLocation().getDirection();
}
private double getYaw(Entity player, Vector vec) {