Moving a player smoothly

Discussion in 'Plugin Development' started by SycoPrime, Mar 30, 2011.

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

    SycoPrime

    I am attempting to use Player.setVelocity to move a player smoothly about an area.
    The player's vector should increase, in one direction at a time, accelerating at a rate of 1 block every 2 seconds.

    Currently, I'm using the following code every movement, where movements are at least two seconds apart:
    Code:
    Vector pVel = p.getVelocity();
    pVel = pVel.add(new Vector(dx, dy, dz));
    p.setVelocity(pVel);
    Again, this is only one direction at a time, so only one (dx, dy, dz) will be != 0 at a time.

    This works fine at very low speeds, where dx and dz are never more than 1 or 2.
    However, after traveling like this for a while, something (possibly friction?) pushes the player back from their relative position (they don't move far enough in the intended direction).
    And at higher speeds (max dx/dz), setVelocity seems to outright do nothing once dx/dz reaches ~4 (blocks / 2 seconds).

    I was hoping someone could shed some light on the issue.
     
  2. Offline

    SycoPrime

  3. Offline

    iPhysX

    I'm probably not going to be any help.
    But is there a way to update position?
    For example, you can update a players inventory?!
     
  4. Offline

    Afforess

    Notch has arbitrary limits on how fast entities can move, set for each entity. It's not that hard to find in the code (I added pull requests that got added to CB weeks ago so I can adjust the maximum speed of minecarts/boats). Using those 2 as examples, I bet you can find the maximum speed for players.
     
  5. Offline

    SycoPrime

    I took a look at the code that you've got here, but can't make sense of it. I don't read hex, unfortunately :( so that double (MAXIMUM_MOMENTUM) could be anything for all I know.
    I tried copying it and ecoing it out, thinking I was being crafty, but what came back was something like "1.0E15D...".

    I suppose all I need to do to make things work smoothly is to check if the desired momentum is > the max, and then teleport the player if so.

    @iPhysX Entity.teleport(Location). And that's what the mod is doing right now, but firing every 2 seconds, it makes the game look like you're playing under a strobe light. Not fun :( I was hoping I could use this to smooth things out.
     
  6. Offline

    Afforess

    Wait, how does the QuickStrasse plugin do it?
     
  7. Offline

    SycoPrime

    Code:
    public void onPlayerMove(PlayerMoveEvent paramPlayerMoveEvent)
       {
         Player localPlayer = paramPlayerMoveEvent.getPlayer();
         if  (paramPlayerMoveEvent.getFrom().getWorld().getBlockAt(paramPlayerMoveEvent.getFrom().getBlockX(),  paramPlayerMoveEvent.getFrom().getBlockY() - 1,  paramPlayerMoveEvent.getFrom().getBlockZ()).getTypeId() == 20)
           if  (Fw(paramPlayerMoveEvent.getFrom().getWorld().getBlockAt(paramPlayerMoveEvent.getFrom().getBlockX(),  paramPlayerMoveEvent.getFrom().getBlockY() - 1,  paramPlayerMoveEvent.getFrom().getBlockZ()), localPlayer))
           {
             Block localBlock =  Fs(paramPlayerMoveEvent.getFrom().getWorld().getBlockAt(paramPlayerMoveEvent.getFrom().getBlockX(),  paramPlayerMoveEvent.getFrom().getBlockY() - 1,  paramPlayerMoveEvent.getFrom().getBlockZ()), localPlayer);
             Location localLocation;
             (localLocation = new Location(localPlayer.getWorld(),  localBlock.getX() + 0.5D, localBlock.getY(), localBlock.getZ() +  0.5D)).setYaw(localPlayer.getLocation().getYaw());
             localLocation.setPitch(localPlayer.getLocation().getPitch());
             int i = 4;
             int j = i - 1;
             j = i * j;
             j %= 2;
             i /= 5;
             i += 4;
             i *= 0;
             if  (!GC(paramPlayerMoveEvent.getFrom().getWorld().getBlockAt(paramPlayerMoveEvent.getFrom().getBlockX(),  paramPlayerMoveEvent.getFrom().getBlockY() - 1,  paramPlayerMoveEvent.getFrom().getBlockZ())).equalsIgnoreCase(j - 0 != 0  ? GC(localBlock) : GC(localBlock)))
             {
               if (localLocation.getBlock().getTypeId() == 0)
               {
                 if (this.d != this.c[Gi(localPlayer)])
                 {
                   if ((this.d == 1) && (this.c[Gi(localPlayer)] == 2))
                   {
                     this.c[Gi(localPlayer)] = this.d;
                     (localLocation = new Location(localPlayer.getWorld(),  localBlock.getX() + 0.5D, localBlock.getY() + 1, localBlock.getZ() +  0.5D)).setYaw(localPlayer.getLocation().getYaw());
                     localLocation.setPitch(localPlayer.getLocation().getPitch());
                      plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new  QuickStrasseEvent(localPlayer, localLocation), 0L);
                     return;
                   }
                   paramPlayerMoveEvent.setTo(localLocation);
                   localPlayer.teleport(localLocation);
                   this.c[Gi(localPlayer)] = this.d;
                   return;
                 }
                 paramPlayerMoveEvent.setTo(localLocation);
                 localPlayer.teleport(localLocation);
                 return;
               }
               (localLocation = new Location(localPlayer.getWorld(),  localBlock.getX() + 0.5D, localBlock.getY() + 2, localBlock.getZ() +  0.5D)).setYaw(localPlayer.getLocation().getYaw());
               localLocation.setPitch(localPlayer.getLocation().getPitch());
               if (localLocation.getBlock().getTypeId() == 0)
               {
                  plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new  QuickStrasseEvent(localPlayer, localLocation), 0L);
                 return;
               }
             }
           }
           else
           {
             this.a[Gi(localPlayer)] = plugin.Sun(48);
           }
       }
    By not putting anything on Github and apparently obfuscating (or just working like that :confused:) the functions it does use.
    It seems like a lot of that is defined elsewhere, but...*shudders*.

    Upon closer inspection of that, it looks like he's probably setting the player's destination at the end of a move event, and then teleporting the player? :confused:
    I can't hook a move event like plugins like that can, since the player could well be standing perfectly still, not firing an event.
     
  8. Offline

    SycoPrime

    Apparently that value I stole from your code is insanely high. "10^150". There's absolutely no way anything I've ever done in minecraft, or a plugin, has ever approached that.
    Also, just saying, anything traveling at 10^150 at cloud level would find itself in unloaded chunks before the last chunks told to load even started, even in singleplayer.</CaptainObvious>
     
  9. Offline

    SycoPrime

Thread Status:
Not open for further replies.

Share This Page