Solved Arrows aiming off depending on direction

Discussion in 'Plugin Development' started by bobbob1870, Oct 2, 2013.

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

    bobbob1870

    So I'm making a plugin that involves shooting a bow and multiple arrows coming out at once. Everything went fine until I realized that if you aim in certain directions the arrows shoot off either to the left or the right. To be specific aiming at yaw = -45 and yaw = 135 arrows shoot perfectly straight with no problems, but as I move more towards yaw = 45 and yaw = -135 the arrows begin to shoot more to the left or right. I have no idea why this is happening and would very much appreciate a solution and explanation if possible.

    Here is part of my code:
    Code:java
    1. for (int i = 0; i < arrowCount; i++) {
    2. Arrow arrow = player.launchProjectile(Arrow.class);
    3. arrow.setVelocity(new Vector(event.getProjectile().getVelocity().getX() + ((Math.random() / 1.5) - .75), event.getProjectile().getVelocity().getY() + ((Math.random() / 1.5) - .25), event.getProjectile().getVelocity().getZ() + ((Math.random() / 1.5) - .75)));
    4. }
     
  2. Offline

    xTrollxDudex

    bobbob1870
    Try spawning the arrow instead of launching it
    PHP:
    Arrow arrow player.getWorld().spawn(player.getEyeLocation(), Arrow.class);
    arrow.setShooter(player);
     
  3. Offline

    MrSnare

    So like a shotbow?

    Yes and then give it your own velocity. Your yaw must also be based on where you are facing. Always setting between 45 and -135 wont work every time?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  4. Offline

    bobbob1870

    Didn't work, simply made it appear as if an arrow was in my face every time i shot the bow.

    No there is actually a lot more involved than just shooting bows but this is just a problem I've noticed related to it.

    Okay so yes I give the arrow a velocity based on the direction the player is facing but I don't seem to understand what you mean when you say "Your yaw must also be based on where you are facing. Always setting between 45 and -135 wont work every time?" Yaw is always based on where you are facing I know that but it sounds like your saying I need to set it? Also your question brings up the same question. There is no yaw variable that I can find that an Arrow object holds. If you mean setting the velocity then yes I do that by getting the velocity of the original arrow then offsetting it a bit through random numbers and simple algebra. Hence:

    Code:java
    1. arrow.setVelocity(new Vector(event.getProjectile().getVelocity().getX() + ((Math.random() / 1.5) - .75), event.getProjectile().getVelocity().getY() + ((Math.random() / 1.5) - .25), event.getProjectile().getVelocity().getZ() + ((Math.random() / 1.5) - .75)));


    Also maybe I should have mentioned this earlier but this is in a EntityShootBowEvent listener. The only fix I can think of doing is either making a huge switch statement for every 10 yaw and offsetting the arrows a bit depending on that or with an algebraic equation that modifies the velocity's x and z values depending on the yaw. The huge switch statement obviously would take a huge bulk of time which is why I am really opposed to it and I do not know how to do that latter which is why I asked for help here. However if there is a better way of doing this that would be great as well.
     
  5. Offline

    bobbob1870

    Bump, please I could really use some help this problem is very bothersome.
     
  6. Offline

    blablubbabc

    I assume you want to create some spray?
    Try this:

    Code:java
    1.  
    2. Vector velocity = event.getProjectile().getVelocity();
    3. double speed = velocity.length();
    4. Vector direction = new Vector(velocity.getX() / speed, velocity.getY() / speed, velocity.getZ() / speed);
    5. // you can tune the following value for different spray. Higher number means less spray.
    6. double spray = 3.5D;
    7.  
    8. for (int i = 0; i < arrowCount; i++) {
    9. Arrow arrow = player.launchProjectile(Arrow.class);
    10. arrow.setVelocity(new Vector(direction.getX() + (Math.random() - 0.5) / spray, direction.getY() + (Math.random() - 0.5) / spray, direction.getZ() + (Math.random() - 0.5) / spray).normalize().multiply(speed));
    11. }
    12.  


    The main difference to your previously code is, that it is now more clear in which bounds the velocity gets randomly modifed (here: from about -0.5 to 0.5; previously on x-z-direction from about -0.75 to -0.08 and on y axis from -0.25 to 0.4 -> which are strange boundaries). Furthermore, it is now more clear how much these random numbers affect the direction (because here the modified vector is first of length 1, before it is brought back to the initial length; in your code it depends on the initial arrows speed (length) how much your random modifications are taken into account.. and additionally the launched arrows now have all the same speed in the end).

    I also removed the different spray value for the y-axis, because this would lead to different spray behavior depending how much you are looking up-/downwards..
     
    Wruczek, MrSnare and bobbob1870 like this.
  7. Offline

    bobbob1870

    It works perfectly thanks a ton, also I had not intended to set the boundaries in such a strange way I suppose I simply made a mistake in my calculations. Anyway thanks!
     
Thread Status:
Not open for further replies.

Share This Page