Changing player direction with angle

Discussion in 'Plugin Development' started by Kobting, May 14, 2014.

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

    Kobting

    I've just started learning how to use bukkit and I'm try to find out a way to change the direction the player is looking and moving with and angle. I basically need to add a certain degree to that players direction.
     
  2. Offline

    kxpeep93

    More information please Kobting
     
  3. Offline

    Kobting

    kxpeep93

    I'm trying to make a somewhat realistic parkour movement where when a player hits a block he is able to "jump" off of it at an angle that makes sense. So i need to be able to get the players direction and add an angle to it so it seems more realistic. Like say you hit the wall head on you'll expect to bounce off in the direction behind you.

    Edit: I basically need to set the direction and velocity of the player to the complementing angle of the angle the player hits the wall at.

    Bump?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  4. You have the set the pitch and yaw of the players location to set the direction he's facing. Have you tried that ?
     
  5. Offline

    NathanWolf

    Note that you will actually have to teleport the player, even just to change their pitch and yaw. Coupled with latency, this may create some strange bouncing effects, especially while in the middle of a parkour course. It may not work out, unless there's a way to only set the direction I'm not aware of.

    If you want to try it, Bukkit recently added a very handy method:

    http://jd.bukkit.org/dev/apidocs/org/bukkit/Location.html#setDirection(org.bukkit.util.Vector)

    Just call player.getLocation().getDirection(), mess with the vector (mathamatical!), then put it back into the same Location object and call player.teleport() with it.

    Or, use pitch and yaw as BugsyFTW suggested. This may be easier if you really just want to adjust an angle since they are converted into angles already for you. I like to deal with vectors myself, if you 're wanting to do something like "make the player look towards a specific block when they jump off block X" then the vector approach may be better.
     
    Garris0n and BugsyFTW like this.
  6. Offline

    Kobting

    BugsyFTW
    NathanWolf

    Thanks for the help I didn't even know what that pitch and yaw values were for. I'll try messing around with them and try finding a formula that will give me the kind of movement I'm looking for. I'll post back later if I'm still having problems.
     
    NathanWolf likes this.
  7. Offline

    Garris0n

    You might have an easier time with the 1.8 update and those smooth teleports.
     
    NathanWolf likes this.
  8. Offline

    Kobting

    GarrisOn
    @BugsyFTW
    @NathanWolf
    I've pretty much got the movement I'm looking for but I'm having problems because changing the yaw and pitch are doing nothing to the direction I am facing and setDirection() isn't doing anything either. I need this so that the player can continue to hold forward after hitting the wall. If the direction they are looking stays the same they bounce off and then move towards the wall not away.
     
  9. Offline

    Garris0n

     
    NathanWolf and Kobting like this.
  10. Offline

    Kobting

    GarrisOn

    So here's my code and I'm setting the players pitch to the exact opposite to make it easier to check for the change but even after teleporting the pitch isn't changing. Is there a reason that I'm not seeing as to why?

    Code:java
    1. @EventHandler
    2. public void hitWall (PlayerInteractEvent event) {
    3.  
    4. Player player = event.getPlayer();
    5.  
    6. Block groundBlock = player.getLocation().getBlock().getRelative(0, -1, 0);
    7.  
    8.  
    9. // Havn't made it so you can't spam/hold right click yet
    10. if(groundBlock.getType() == Material.AIR && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    11.  
    12. Block block = event.getClickedBlock();
    13. double x = player.getVelocity().getX();
    14. double z = player.getVelocity().getZ();
    15. double y = player.getVelocity().getY() + .3;
    16. float pYaw = player.getLocation().getYaw();
    17. float pPitch = player.getLocation().getPitch();
    18.  
    19. player.sendMessage(pYaw + "Before yaw");
    20. player.sendMessage(pPitch + "Before pitch");
    21.  
    22. // Not all of the math in these is right I'm still messing with it
    23. if(player.getVelocity().getX() < 0 && player.getVelocity().getZ() < 0) {
    24. player.getLocation().setPitch(-pPitch);
    25. x = x + (2 * player.getVelocity().getX());
    26. z = z + (2 * player.getVelocity().getZ());
    27. }
    28. else if(player.getVelocity().getX() < 0 && player.getVelocity().getZ() > 0) {
    29. player.getLocation().setPitch(-pPitch);
    30. x = x + (2 * player.getVelocity().getX());
    31. z = z - (2 * player.getVelocity().getZ());
    32. }
    33. else if(player.getVelocity().getX() > 0 && player.getVelocity().getZ() < 0) {
    34. player.getLocation().setPitch(-pPitch);
    35. x = x - (2 * player.getVelocity().getX());
    36. z = z + (2 * player.getVelocity().getZ());
    37. }
    38. else{
    39. player.getLocation().setPitch(-pPitch);
    40. x = x - (2 * player.getVelocity().getX());
    41. z = z - (2 * player.getVelocity().getZ());
    42. }
    43.  
    44. // Got tired of getting hungry
    45. if(player.getFoodLevel() < 20) {
    46. player.setFoodLevel(20);
    47. }
    48.  
    49. if(block.getType() != Material.AIR) {
    50.  
    51. player.teleport(player.getLocation());
    52. player.sendMessage(pYaw + "After yaw");
    53. player.sendMessage(pPitch + "After pitch");
    54. player.setVelocity(new Vector(x, y, z));
    55.  
    56. }
    57. }
     
Thread Status:
Not open for further replies.

Share This Page