Getting direction towards another location

Discussion in 'Plugin Development' started by SgtStud, Nov 16, 2012.

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

    SgtStud

    How do you get the direction towards another location? I'm working with vectors and I don't quite understand them.

    Basically what i'm trying to do is create an arrow, and fire it towards an entity. How do I get the right vector using the starting and target locations?
     
  2. Offline

    wristdirect

    If you are trying to shoot an arrow at an entity as if shot from another entity, you can use CraftBukkit's EntityArrow. Here I'm assuming the 'shooter' and 'target' are both a LivingEntity (in the Bukkit API).

    Code:
    World world = shooter.getWorld();
    EntityLiving entityShooter = ((CraftLivingEntity)shooter).getHandle();
    EntityLiving entityTarget = ((CraftLivingEntity)target).getHandle();
    float speedFactor = 1.0f;
    float accuracyFactor = 1.0f;
    EntityArrow entityArrow = new EntityArrow(world, entityShooter, entityTarget, speedFactor, accuracyFactor);
    This will shoot an arrow at target from shooter, with normal speed and decent accuracy :)
     
    SgtStud likes this.
  3. Offline

    SgtStud

    Thanks for the reply! Unfortunetly, I am actually shooting arrows from a "tower", which is a special block. In other words, not an entity :(
     
  4. Offline

    Jogy34

    Spawn an entity at the tower then remove it after you have shot the arrow
     
  5. Offline

    wristdirect

    There's actually another way to do spawn and shoot an arrow entity, but I'm not sure if I'll have time to get it all laid out today ><

    I can quickly say that it involves a different constructor of an EntityArrow; one which doesn't also shoot the arrow as part of the constructor's code. You then have to call the EntityArrow's shoot method yourself.

    If I have time in a little bit I'll write the code out and post it :)

    SgtStud Edit: And actually, that code wouldn't have quite worked. I had world as being a Bukkit World class, not a net.minecraft.server.World class :p

    SgtStud Okee dokee, here's what I got. Hopefully I didn't leave anything out :)

    Code:
            net.minecraft.server.World world = ((CraftWorld)target.getWorld()).getHandle();
            Location targetLoc = target.getLocation();
       
            double dx = targetLoc.getX() - shooterX;
            double dy = targetLoc.getY() - shooterY;
            double dz = targetLoc.getZ() - shooterZ;
            double distanceRecip = 1/Math.sqrt(dx*dx + dy*dy + dz*dz);
            dx *= distanceRecip;
            dy *= distanceRecip;
            dz *= distanceRecip;
            float speedFactor = 1.0f;
            float accuracyFactor = 1.0f;
       
            EntityArrow entityArrow = new EntityArrow(world, shooterX + 1.2*dx, shooterY + 1.2*dy, shooterZ + 1.2*dz);
            entityArrow.shoot(dx,dy,dz,speedFactor,accuracyFactor);
    edit: The reason for the 1.2*dx etc is that you don't want to spawn the arrow inside of the shooter block location, but somewhere right next to the shooter block, in the direction of the target so it looks natural.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
    SgtStud likes this.
  6. Offline

    SgtStud

    I'm getting errors running your code. It says that CraftWorld is not a type? And why can't I use buckets world, why must i use minecraft.net's world instead?

    Thanks for all the help so far!
     
Thread Status:
Not open for further replies.

Share This Page