Help Me Understand Vectors & Aiming (code)

Discussion in 'Plugin Development' started by dNiym, Jun 17, 2014.

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


    I have the following code on a plugin I am working on.

    This code will spawn a fireball above the player(sender's) head about 27 blocks higher than the player's current Ypos.

    I then use the players eye position to detect a block then launch the fireball toward the second block.

    The code works sending the meteor from the location above the player's head down to the target block, however it does have some error to it.

    The code identifies the targeted block by changing it into cobblestone. When the fireball falls it is consistently 5 blocks ahead of the target block...

    The further the target block is away from the player the more this error grows, (up to about 8-15 blocks)

    What do I need to do to make sure the fireball is aimed more at the intended target?


    Location ploc = player.getLocation();
                double theta = 0;  
                double radius = 5;
                double phi = Math.toRadians(theta);
                double dx = Math.cos(phi) * radius;
                double dy = 29;
                double dz = Math.sin(phi) * radius;
                Location blockloc = player.getLocation().clone().add(dx, dy, dz);  //location above players head
                Block gblock = targetlocation.getBlock();  //location where player is looking.
                Vector from = new Vector(blockloc.getX(), blockloc.getY(), blockloc.getZ());
                Vector to  = new Vector(gblock.getX(), gblock.getY(), gblock.getZ());
                org.bukkit.entity.Fireball ball = player.getWorld().spawn(blockloc.add(0, -2, 0), org.bukkit.entity.Fireball.class);
                Vector direction = to.subtract(from);
                player.sendMessage("Target Loc:" + gblock.getLocation().getX() + "x " + gblock.getLocation().getY() + "y " + gblock.getLocation().getZ() + "z");
  2. Offline


    I'm not sure about the Math and all. I'm gonna take a stab in the dark here.

    Shouldn't it spawn at blockLoc ? Instead of blockLoc.add(0, -2, 0).
    Since from is not blockLoc.add(0, -2, 0) but it's blockLoc exactly.
    org.bukkit.entity.Fireball ball = player.getWorld().spawn(blockloc.add(0, -2, 0), org.bukkit.entity.Fireball.class);
    Orrr from should be blockLoc.add(0, -2, 0);

    Either way..
  3. Offline


    You find the deltas between the player and the target in x/y/z, then you add 27 to the y, then you stuff this into a vector and normalize, etc, set as velocity. You shouldn't need trig for this. (assuming the thing is starting out at 27 blocks above the player)
Thread Status:
Not open for further replies.

Share This Page