Solved Problems with vectors

Discussion in 'Spigot Plugin Development' started by SeamusMcFreak, Apr 1, 2016.

Thread Status:
Not open for further replies.
  1. Im trying to make an arrow follow a player. I tried this:
    Code:
    Code (Text):
    public class HomingTask extends BukkitRunnable {
        Arrow arrow;
        Player target;

        public HomingTask(Arrow arrow, Player p) {
            this.arrow = arrow;
            target = p;
            long delay = 1;
            runTaskTimer(Main.plugin, delay, delay);
        }

        public void run() {
            Location ploc = target.getLocation();
            Vector vec1 = new Vector(ploc.getBlockX(), ploc.getBlockY(), ploc.getZ());
            arrow.setVelocity(vec1);
            arrow.getWorld().spawnParticle(Particle.CLOUD, arrow.getLocation(), 5);
        }

    }
     
    Instead, an arrow shoots at 100 blocks per second location not following the player and lagging the server because of particles being constantly spawned.
     
  2. I am pretty sure new Vector(x, y, z) is the speeds. Not the location so try this: (Warning I haven't tested it may have typos)

    Code (Text):
    public class HomingTask extends BukkitRunnable {
        Arrow arrow;
        Player target;

        public HomingTask(Arrow arrow, Player p) {
            this.arrow = arrow;
            target = p;
            long delay = 1;
            runTaskTimer(Main.plugin, delay, delay);
        }

        public void run() {
            Location ploc = target.getLocation();
            Location arrowLoc = arrow.getLocation();
            double speedMultiplier = 1.0;// Adjust speed as necessary
            double xDif = ploc.getX() - arrowLoc.getX();
            double yDif = ploc.getY() - arrowLoc.getY();
            double zDif = ploc.getZ() - arrowLoc.getZ();
            double totalDif = xDif + yDif + zDif;
            Vector vec1 = new Vector(xDif / totalDif, yDif / totalDif, zDif / totalDif).multiply(speedMultiplier);
            arrow.setVelocity(vec1);
            arrow.getWorld().spawnParticle(Particle.CLOUD, arrow.getLocation(), 5);
        }

    }
     
  3. You have to substract the foot (the arrow) from the peak (the player) of the vector to get the right vector.
    Thats just simple mathematics.
     
  4. Definitely look at this
     
  5. MiniDigger

    Supporter

    this is not how you use the velocity...
    have you ever learned anything about vectors? if not, I would suggest learning about the math first before trying to apply vectory in your plugins...
    EDIT: or just can just be spoon fed by someone on the internet, I still suggest you to try to understand what @GreenBeard did there...
     
  6. Can you give me an example of subtracting a vector?
     
  7. MiniDigger

    Supporter

Thread Status:
Not open for further replies.