Solved 1.8.8 Changing Vectors of Projectiles

Discussion in 'Plugin Development' started by Lightcaster5, Sep 4, 2020.

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

    Lightcaster5

    I understand there are threads explaining vectors, and yes I have read https://www.spigotmc.org/wiki/vector-programming-for-beginners/ before for those who may suggest it. I am very new to using vectors and I didn't know how to tackle a problem I've encountered. My goal is to create three snowballs that launch from a player when the right click their sword. One should go forward, one should go a bit to the left, one should go a bit to the right. Here is snowball creation code I've tried:
    Code:
    for (int i = 0; i < 3; i++) {
        Snowball snowball = player.getWorld().spawn(player.getEyeLocation(), Snowball.class);
        snowball.setShooter(player);
        snowball.setCustomName("ball");
        Vector v = player.getLocation().getDirection();
        switch (i) {
        case 0:
            v.add(new Vector(0, 0, 1));
            break;
        case 1:
            v.add(new Vector(0, 0, -1));
            break;
        }
        snowball.setVelocity(v);
    }
    
    The problem with this is, if a player is looking north or south, it works, though if they look east or west, it turns it sideways. Similarly, when a player is looking diagonally, the snowballs don't line up correctly. I'm sure there is some way to calculate this properly but I still can't figure it out. Thanks for any help, not looking for spoons, looking for ways to accomplish this with my own code and your knowledge.
     
  2. Offline

    Kars

    First of all i would put the snowball throwing inside of a function that takes the direction relative to the player as an argument. That way you can avoid that ugly switch statement.

    If i was trying to solve this problem, i would figure out which vector values would make the snowballs line up correctly when facing east/west and also diagonally. Perhaps you can figure out something from there. If not; you could check what direction the player is facing and set the values depending on that.
     
  3. Offline

    Lightcaster5

    I appreciate the input! The function is a good idea, I'll have to try that. Though, I don't want a switch or if else statement to check if the player is north/east or diagonally. I'm trying to figure out what I could use to determine the exact direction and use that in the vector displacement. This way, everything would line up.
    Thanks!
     
  4. Offline

    Kars

    @Lightcaster5 i don't know how vectors work exactly. It would make sense to have a one size fits all for all directions be possible, but you would have to experiment with different values for that.
     
    Xp10d3 likes this.
Thread Status:
Not open for further replies.

Share This Page