Pushback effect

Discussion in 'Plugin Development' started by treestompz, Mar 23, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    treestompz

    Hello Bukkit,

    I am trying to create a pushback effect. The problem I am having is that when I try to modify the X and Z velocity, it will only work one way.

    Code:
    p.setVelocity(event.getPlayer().getLocation().getDirection().multiply(1).setX(-1));
    p.setVelocity(event.getPlayer().getLocation().getDirection().multiply(1).setZ(-1));
    
    Any ideas on how to create a pushback effect no matter where the player is or where the player is looking? My GoogleFu wasn't helping :(

    Thanks in advance!
     
  2. Offline

    skipperguy12

    Hmm, can't you check if the direction of the x is < #, then only do the push effect?
     
  3. Offline

    treestompz

    Sorry, not following.
     
  4. Offline

    treestompz

    Bump :3
     
  5. Offline

    nitrousspark

    Code:
    player.setVelocity(player.getLocation().getDirection().multiply(-1).setY(1))
     
    microgeek likes this.
  6. Offline

    treestompz

    nitrousspark Hmmm...that isn't what I am looking for. That gets the player who is supposed to be pushed backed direction and modifies that.

    I have code that smashes a player into the ground, on impact this code is run:

    Code:
    //some code to smash into ground
     
    for (Player p : Bukkit.getOnlinePlayers())
                    {
                        if (p != event.getPlayer() && p.getWorld() == event.getPlayer().getWorld() && p.getLocation().distance(event.getPlayer().getLocation()) < 10)
                        {
                            p.damage(2);
                            p.setVelocity(p.getLocation().getDirection().multiply(-1).setY(.2));
                        }
                    }
    
    The problem with that (your recommendation) is if the player is looking away from the player smashing into ground, instead of being pushed back he will be pushed towards the player smashing into the ground.
     
  7. Just add a potion effect (knockback)
     
  8. Offline

    Tzeentchful

    This should do what you want. Just change the value in the multiply part, but make sure it's negative if you want a knock back.
    Code:
    Vector directionVector = p.getLocation().getDirection().normalize();
    p.setVelocity(p.getVelocity().add(directionVector.multiply(-2)));
     
  9. Yup - or you can do player.addPotionEffect(PotionEffectType.KNOCKBACK, time, strength);
     
  10. Offline

    nitrousspark

    knockback is a potion effect?
     
  11. Offline

    treestompz

    This still has the same issue.

    I made a video: (resolution derped but that's irrelevant)


    Code:
    Code:
    p.setVelocity(p.getVelocity().add(p.getLocation().getDirection().normalize().multiply(-2)));
     
  12. Offline

    Technius

    treestompz
    Calculate a vector from the player who's getting knocked back and the player who is causing the knockback. Normalize it and multiply it with -<your number here>.

    Edit: Something like this?
    Code:
    //Untested
    Vector v = player.getLocation().subtract(target.getLocation()).toVector();
    Vector velocity = v.normalize().multiply(power*-1);  //Vector you use for the velocity
    
     
  13. Offline

    iWareWolf

  14. Offline

    treestompz

    When I tried that nothing happened...player didn't move.
     
  15. Offline

    nitrousspark

    what sound was that O_O
     
  16. Offline

    treestompz

    IRONGOLEM_DEATH with -10 pitch :D
     
  17. Offline

    Tzeentchful

    Ah i didn't know that was the effect you where trying to achieve. I think this is what you want. It gets the delta of X and Z (we don't need Y as it is only going to push the player outwards) from the two locations. It then normalizes it so we get the vector with a length of 1. Because you are looking at a push back effect with a radius I would assume you want it to be weaker the further you get away from the center. To achieve that sort of fall off effect it uses trigonometry get the length of h between the two location (not normalized) using the deltas we calculated earlier. It then divides the max velocity set by h so you get a smaller number the longer the distance between the two players. Then multiply the normalized vector by the number we just calculated. Anyway behold the code.
    Code:java
    1.  
    2.  
    3. Location loc1 = source.getLocation();//Get the location from the source player
    4. Location loc2 = target.getLocation();//Get the location from the target player
    5.  
    6. double deltaX = loc2.getX() - loc1.getX();//Get X Delta
    7. double deltaZ = loc2.getZ() - loc1.getZ();//Get Z delta
    8.  
    9. Vector vec = new Vector(deltaX, 0, deltaZ);//Create new vector
    10. vec.normalize();//Normalize it so we don't shoot the player into oblivion
    11. target.setVelocity(vec.multiply(5 / (Math.sqrt(Math.pow(deltaX, 2.0) + Math.pow(deltaZ, 2.0)))));//Use a bit of trig to get 'h'(square root(a^2 + b^2) = c) then divide the max power by 'h' to get a fall off effect
    12.  
    13.  
     
  18. Offline

    treestompz

    Tzeentchful Wow! Thanks a ton man! Works great!

    I tried adding a little y modification to give the player a little boost (its unnatural to just go straight back gliding on the ground)...but it was too dramatic. Got an idea on how I could implement that smoothly?
     
  19. Offline

    Tzeentchful

    Try this.
    Replace with the stuff under the vec.normalize(); line with...
    Code:java
    1.  
    2. vec.multiply(5 / (Math.sqrt(Math.pow(deltaX, 2.0) + Math.pow(deltaZ, 2.0))));//Use a bit of trig to get 'h' then divide the max power by 'h' to get a fall off effect
    3. vec.setY(2 / (Math.sqrt(Math.pow(deltaX, 2.0) + Math.pow(deltaZ, 2.0))));//use a lower number so the effect is less intense.
    4. target.setVelocity(vec);
    5.  
     
    yknomeh likes this.
Thread Status:
Not open for further replies.

Share This Page