Solved Launching players away from Entity

Discussion in 'Spigot Plugin Development' started by Poofyjerry, Jul 29, 2020.

  1. When a trident hits the ground I am trying to create an explosion that pushes all the players around it up and away from the explosion. Right now I can only figure out how to just send the player straight up, but I can't figure out how to get the vector from the player to the location of the trident hit and reverse that. Because then I would just negate the vector I get from that and multiply it to send them flying. I think...
    Code (Text):

      public static void doBlockHitter(Player shooter, Location location) {
        for (Entity ent :, 3, 3, 3)) {
          if (!(ent instanceof LivingEntity)) {
          Location entLocation = ent.getLocation();
          Vector currentDirection = ent.getLocation().getDirection().normalize();
          currentDirection = currentDirection.multiply(new Vector(0, 2, 0));
          if (ent instanceof Player) {
            ((Player) ent).damage(5);
  2. To make sure you launch them away from the impact location, I am going to use a bit of simple vector arithmetic. We essentially look at the impact location as a vector (Vimpact) and the entities location as a vector (Ventity). If we now take the difference Ventity - Vimpact, this represent a vector going from the impact location to the entities location. This is the direction we want to push the entity, so it goes away from the impact location. Instead of elementwise multiplication by (0, 2, 0), which makes all terms except for the y zero, we will just set the y component of this new vector manually to guarantee the entity is lauched upwards. As there is no vector remove function, I just multiply it by -1 instead and use add.

    Code (Java):
    Vector launchDirection = entLocation.toVector().add(location.toVector().multiply(-1));
    • Like Like x 1
  3. I got it thank you
  4. Just wanted to add onto this as well, having encountered this problem before. This solution is great except for if you're trying to set the velocity based on proximity to the point of impact. In other words, what you'll find is that with this solution, if you're on the outer edge of the explosion's proximity, you'd be launched further than if you were closer to the point of impact.

    If you're aiming for that bit of realism, like I was, you may find you want to take this into account.

    My 2 cents - good luck with your project!
    • Creative Creative x 1