org.bukkit.util.Vector.length()方法的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(89)

本文整理了Java中org.bukkit.util.Vector.length()方法的一些代码示例,展示了Vector.length()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Vector.length()方法的具体详情如下:
包路径:org.bukkit.util.Vector
类名称:Vector
方法名:length

Vector.length介绍

[英]Gets the magnitude of the vector, defined as sqrt(x^2+y^2+z^2). The value of this method is not cached and uses a costly square-root function, so do not repeatedly call this method to get the vector's magnitude. NaN will be returned if the inner result of the sqrt() function overflows, which will be caused if the length is too long.
[中]

代码示例

代码示例来源:origin: Bukkit/Bukkit

/**
 * Converts this vector to a unit vector (a vector with length of 1).
 *
 * @return the same vector
 */
public Vector normalize() {
  double length = length();
  x /= length;
  y /= length;
  z /= length;
  return this;
}

代码示例来源:origin: elBukkit/MagicPlugin

@Override
public SpellResult start(CastContext context) {
  if (movementSpread > 0) {
    double entitySpeed = context.getMage().getVelocity().lengthSquared();
    if (entitySpeed > 0) {
      double movementAmount = Math.min(1.0, entitySpeed / (movementSpread * movementSpread));
      spread += (movementAmount * maxMovementSpread);
      if (context.getMage().getDebugLevel() >= 3) {
        context.getMage().sendDebugMessage(ChatColor.DARK_RED + " Applying spread of " + ChatColor.RED + spread
            + ChatColor.DARK_RED + " from speed of " + ChatColor.GOLD + context.getMage().getVelocity().length());
      }
    }
  }
  return super.start(context);
}

代码示例来源:origin: Bukkit/Bukkit

/**
 * Gets the angle between this vector and another in radians.
 *
 * @param other The other vector
 * @return angle in radians
 */
public float angle(Vector other) {
  double dot = dot(other) / (length() * other.length());
  return (float) Math.acos(dot);
}

代码示例来源:origin: GlowstoneMC/Glowstone

private double distanceTo(Entity entity) {
  return RayUtil.getRayBetween(location, entity.getLocation()).length();
}

代码示例来源:origin: GlowstoneMC/Glowstone

@Override
public void setDirection(Vector vector) {
  setVelocity(vector.normalize().multiply(velocity.length()));
}

代码示例来源:origin: SpigotMC/Spigot-API

/**
 * Converts this vector to a unit vector (a vector with length of 1).
 *
 * @return the same vector
 */
public Vector normalize() {
  double length = length();
  x /= length;
  y /= length;
  z /= length;
  return this;
}

代码示例来源:origin: SpigotMC/Spigot-API

/**
 * Gets the angle between this vector and another in radians.
 *
 * @param other The other vector
 * @return angle in radians
 */
public float angle(Vector other) {
  double dot = dot(other) / (length() * other.length());
  return (float) Math.acos(dot);
}

代码示例来源:origin: CitizensDev/CitizensAPI

private static Vector clip(double max, Vector vector) {
  if (vector.length() > max) {
    return vector.normalize().multiply(max);
  }
  return vector;
}

代码示例来源:origin: ProjectKorra/ProjectKorra

public static Vector rotateVectorAroundVector(final Vector axis, final Vector rotator, final double degrees) {
  final double angle = Math.toRadians(degrees);
  Vector rotation = axis.clone();
  final Vector rotate = rotator.clone();
  rotation = rotation.normalize();
  final Vector thirdaxis = rotation.crossProduct(rotate).normalize().multiply(rotate.length());
  return rotate.multiply(Math.cos(angle)).add(thirdaxis.multiply(Math.sin(angle)));
}

代码示例来源:origin: ProjectKorra/ProjectKorra

public static double getDistanceFromLine(final Vector line, final Location pointonline, final Location point) {
  final Vector AP = new Vector();
  double Ax, Ay, Az;
  Ax = pointonline.getX();
  Ay = pointonline.getY();
  Az = pointonline.getZ();
  double Px, Py, Pz;
  Px = point.getX();
  Py = point.getY();
  Pz = point.getZ();
  AP.setX(Px - Ax);
  AP.setY(Py - Ay);
  AP.setZ(Pz - Az);
  return (AP.crossProduct(line).length()) / (line.length());
}

代码示例来源:origin: TotalFreedom/TotalFreedomMod

@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerMove(PlayerMoveEvent event)
{
  final Player player = event.getPlayer();
  final FPlayer fPlayer = plugin.pl.getPlayer(player);
  if (!fPlayer.isOrbiting())
  {
    return;
  }
  if (player.getVelocity().length() < fPlayer.orbitStrength() * (2.0 / 3.0))
  {
    player.setVelocity(new Vector(0, fPlayer.orbitStrength(), 0));
  }
}

代码示例来源:origin: Slikey/EffectLib

@Override
public void onRun() {
  Location location = getLocation();
  Location target = getTarget();
  if (target == null) {
    cancel();
    return;
  }
  Vector link = target.toVector().subtract(location.toVector());
  float length = (float) link.length();
  float pitch = (float) (4 * height / Math.pow(length, 2));
  for (int i = 0; i < particles; i++) {
    Vector v = link.clone().normalize().multiply((float) length * i / particles);
    float x = ((float) i / particles) * length - length / 2;
    float y = (float) (-pitch * Math.pow(x, 2) + height);
    location.add(v).add(0, y, 0);
    display(particle, location);
    location.subtract(0, y, 0).subtract(v);
    step++;
  }
}

代码示例来源:origin: mcmonkeyprojects/Sentinel

/**
   * Knocks a target back from damage received (for hacked-in damage applications when required by config).
   */
  public void knockback(LivingEntity entity) {
    Vector relative = entity.getLocation().toVector().subtract(getLivingEntity().getLocation().toVector());
    relative = relative.normalize();
    relative.setY(0.75);
    relative.multiply(0.5 / Math.max(1.0, entity.getVelocity().length()));
    entity.setVelocity(entity.getVelocity().multiply(0.25).add(relative));
    if (SentinelPlugin.debugMe) {
      debug("applied knockback velocity adder of " + relative);
    }
  }
}

代码示例来源:origin: gvlfm78/BukkitOldCombatMechanics

@EventHandler
  public void onProjectileLaunch(ProjectileLaunchEvent e){
    Projectile projectile = e.getEntity(); //Getting the projectile
    ProjectileSource shooter = projectile.getShooter(); //Getting the shooter
    if(shooter instanceof Player){ //If the shooter was a player
      Player player = (Player) shooter;
      if(!isEnabled(player.getWorld())) return; //If this module is enabled in this world
      debug("Making projectile go straight", player);
      //Here we get a unit vector of the direction the player is looking in and multiply it by the projectile's vector's magnitude
      //We then assign this to the projectile as its new velocity
      projectile.setVelocity(player.getLocation().getDirection().normalize().multiply(projectile.getVelocity().length()));
    }
  }
}

代码示例来源:origin: com.github.shynixn.ball/ball-bukkit-core

private void playRotationAnimation() {
  final double length = new Vector(this.hitBox.motX, this.hitBox.motY, this.hitBox.motZ).length();
  EulerAngle angle = null;
  final EulerAngle a = this.getHeadPose();
  if (length > 1.0) {
    if (this.revertAnimation) {
      angle = new EulerAngle(a.getX() - 0.5, 0, 0);
    } else {
      angle = new EulerAngle(a.getX() + 0.5, 0, 0);
    }
  } else if (length > 0.1) {
    if (this.revertAnimation) {
      angle = new EulerAngle(a.getX() - 0.25, 0, 0);
    } else {
      angle = new EulerAngle(a.getX() + 0.25, 0, 0);
    }
  } else if (length > 0.08) {
    if (this.revertAnimation) {
      angle = new EulerAngle(a.getX() - 0.025, 0, 0);
    } else {
      angle = new EulerAngle(a.getX() + 0.025, 0, 0);
    }
  }
  if (angle != null) {
    this.setHeadPose(angle);
  }
}

代码示例来源:origin: com.github.shynixn.ball/ball-bukkit-core

private void playRotationAnimation() {
  final double length = new Vector(this.hitBox.motX, this.hitBox.motY, this.hitBox.motZ).length();
  EulerAngle angle = null;
  final EulerAngle a = this.getHeadPose();
  if (length > 1.0) {
    if (this.revertAnimation) {
      angle = new EulerAngle(a.getX() - 0.5, 0, 0);
    } else {
      angle = new EulerAngle(a.getX() + 0.5, 0, 0);
    }
  } else if (length > 0.1) {
    if (this.revertAnimation) {
      angle = new EulerAngle(a.getX() - 0.25, 0, 0);
    } else {
      angle = new EulerAngle(a.getX() + 0.25, 0, 0);
    }
  } else if (length > 0.08) {
    if (this.revertAnimation) {
      angle = new EulerAngle(a.getX() - 0.025, 0, 0);
    } else {
      angle = new EulerAngle(a.getX() + 0.025, 0, 0);
    }
  }
  if (angle != null) {
    this.setHeadPose(angle);
  }
}

代码示例来源:origin: NyaaCat/RPGItems-reloaded

@Override
  public void run() {
    if (!target.isValid() || projectile.isDead() || !projectile.isValid() || ticks-- <= 0) {
      cancel();
      return;
    }
    Vector origVel = projectile.getVelocity();
    double v = origVel.length();
    Vector rel = target.getEyeLocation().toVector().subtract(projectile.getLocation().toVector()).normalize().multiply(v);
    rel.multiply(velFactor).add(origVel.multiply(1 - velFactor));
    projectile.setVelocity(rel);
    if (projectile instanceof Fireball) {
      ((Fireball) projectile).setDirection(rel.normalize());
    }
  }
}.runTaskTimer(RPGItems.plugin, 1, 0);

代码示例来源:origin: com.github.shynixn.ball/ball-bukkit-core

private void playRotationAnimation() {
  final double length = new Vector(this.hitBox.motX, this.hitBox.motY, this.hitBox.motZ).length();
  EulerAngle angle = null;
  final EulerAngle a = this.getHeadPose();
  if (length > 1.0) {
    if (this.revertAnimation) {
      angle = new EulerAngle(a.getX() - 0.5, 0, 0);
    } else {
      angle = new EulerAngle(a.getX() + 0.5, 0, 0);
    }
  } else if (length > 0.1) {
    if (this.revertAnimation) {
      angle = new EulerAngle(a.getX() - 0.25, 0, 0);
    } else {
      angle = new EulerAngle(a.getX() + 0.25, 0, 0);
    }
  } else if (length > 0.08) {
    if (this.revertAnimation) {
      angle = new EulerAngle(a.getX() - 0.025, 0, 0);
    } else {
      angle = new EulerAngle(a.getX() + 0.025, 0, 0);
    }
  }
  if (angle != null) {
    this.setHeadPose(angle);
  }
}

代码示例来源:origin: com.github.shynixn.ball/ball-bukkit-core

private void playRotationAnimation() {
  final double length = new Vector(this.hitBox.motX, this.hitBox.motY, this.hitBox.motZ).length();
  EulerAngle angle = null;
  final EulerAngle a = this.getHeadPose();
  if (length > 1.0) {
    if (this.revertAnimation) {
      angle = new EulerAngle(a.getX() - 0.5, 0, 0);
    } else {
      angle = new EulerAngle(a.getX() + 0.5, 0, 0);
    }
  } else if (length > 0.1) {
    if (this.revertAnimation) {
      angle = new EulerAngle(a.getX() - 0.25, 0, 0);
    } else {
      angle = new EulerAngle(a.getX() + 0.25, 0, 0);
    }
  } else if (length > 0.08) {
    if (this.revertAnimation) {
      angle = new EulerAngle(a.getX() - 0.025, 0, 0);
    } else {
      angle = new EulerAngle(a.getX() + 0.025, 0, 0);
    }
  }
  if (angle != null) {
    this.setHeadPose(angle);
  }
}

代码示例来源:origin: ProjectKorra/ProjectKorra

@EventHandler
public void onHorizontalCollision(final HorizontalVelocityChangeEvent e) {
  if (e.getEntity() instanceof LivingEntity) {
    if (e.getEntity().getEntityId() != e.getInstigator().getEntityId()) {
      final double minimumDistance = this.plugin.getConfig().getDouble("Properties.HorizontalCollisionPhysics.WallDamageMinimumDistance");
      final double maxDamage = this.plugin.getConfig().getDouble("Properties.HorizontalCollisionPhysics.WallDamageCap");
      final double damage = ((e.getDistanceTraveled() - minimumDistance) < 0 ? 0 : e.getDistanceTraveled() - minimumDistance) / (e.getDifference().length());
      if (damage > 0) {
        if (damage <= maxDamage) {
          DamageHandler.damageEntity(e.getEntity(), damage, e.getAbility());
        } else {
          DamageHandler.damageEntity(e.getEntity(), maxDamage, e.getAbility());
        }
      }
    }
  }
}

相关文章