setVelocity() Help

Discussion in 'Plugin Development' started by heifinator, Nov 12, 2011.

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

    heifinator

    Basically I need to change the players velocity based on their current velocity, I cannot just use Multiply() because I don't want to change their Y velocity, just the X/Z velocity.

    I am having some trouble, any help would be appreciated. Right now Im just getting the velocity for getX() and getZ() multiplying them and trying to setVelocity() the new values, but no luck.

    Thanks!
     
  2. Offline

    DDoS

    What I've used to eject items (which doesn't really matter since both inherit the setVelocity() method from the Entity class) away for signs, is this:

    PHP:
    Vector vect getVelocity(loc);

    if (
    vect != null) {

        
    droppedItem.setVelocity(vect);

    }
    And the getVelocity(Location) method:

    PHP:
    private Vector getVelocity(Location loc) {

        
    Block block loc.getBlock();

        if (
    block.getState() instanceof Sign) {

            
    Sign sign = (Signblock.getState();
            
    byte d sign.getData().getData();

            switch (
    d) {

            case 
    0x2://North, z goes down
                
    return new Vector(00, -0.2);

            case 
    0x3://South, z goes up
                
    return new Vector(000.2);

            case 
    0x4://West, x goes down
                
    return new Vector(-0.200);

            case 
    0x5://East, x goes up
                
    return new Vector(0.200);

            }
        }

        return 
    null;

    }
    Basically, I'm getting the velocity vector, and adding to it another vector (which depends on the sign's orientation, but that's just part of my plugin, don't worry about that).
     
    L3N likes this.
  3. Offline

    emericask8ur

    My favorite vector is
    Code:
     Player player = event.getPlayer();
        	 Vector dir = player.getLocation().getDirection();
              Vector vec = new Vector(dir.getX() * 0.8D, 0.8D, dir.getZ() * 0.8D);
              player.setVelocity(vec);
              player.setFallDistance(-100.0F);
     
    If you use it onPlayerInteract it works like a Fly Mod :]
     
  4. Offline

    bergerkiller

    @heifinator The reason this fails is that the 'getVelocity' in onMove is failing for players. This is because clients only send the new location of the player, and not the velocity. This shock-motion system is smoothed out, so you don't notice it. Your best bet is using the from and to to get the velocity of the player, multiply that, and set to using this new velocity.
    Code:
    Vector diff = new Vector(to.getX() - from.getX(), to.getY() - from.getY(), to.getZ() - from.getZ());
    diff = diff.multiply(1.1, 1.1, 1.1);
    to = from.clone().add(diff.getX(), diff.getY(), diff.getZ());
    But then you risk an infinite increase of the velocity (it's getting 10% higher each tick), which can cause really weird things to happen. Best is to set a limit. (diff.length() < 0.9)
     
    justcool393 and emericask8ur like this.
Thread Status:
Not open for further replies.

Share This Page