Get exact coords where player is looking at

Discussion in 'Plugin Development' started by CaptainBern, Jun 27, 2013.

Thread Status:
Not open for further replies.
  1. Hello,
    I'm making a gun plugin and I need to get the exact location where the player is looking.
    I know there is player.getTargetBlock() but when you shoot the bullet goes to the lbottom left corner. So I need to use some pitch 'n yaw calcualting to get the exact coords. Is there someone who can help me with this? (Maybe you LucasEmanuel ?)
    Also at the moment I use:
    Code:
        public Vector calculateVelocity(Location first_location, Location second_location){
            double dX = first_location.getX() - second_location.getX();
            double dY = first_location.getY() - second_location.getY();
            double dZ = first_location.getZ() - second_location.getZ();
            double yaw = Math.atan2(dZ, dX);
            double pitch = Math.atan2(Math.sqrt(dZ * dZ + dX * dX), dY) + 3.141592653589793D;
            double x = Math.sin(pitch) * Math.cos(yaw);
            double y = Math.sin(pitch) * Math.sin(yaw);
            double z = Math.cos(pitch);
            return new Vector(x, z, y);
          }
    to calculate the vector from one point to another.
     
  2. Offline

    Rprrr

    CaptainBern
    If all you're doing is launching a bullet, you could also use p.launchProjectile(..).
     
    CaptainBern likes this.
  3. That breaks my vector calculating...An no, I will not use the getVelocity().multiply() method. Thanks for trying to help tough...
     
  4. Offline

    Bancey

    I did this with a fireball at some stage, I'll see if I can find it in my code for you.
     
    CaptainBern likes this.
  5. Offline

    chasechocolate

    player.getLocation().getDirection()?
     
    CaptainBern likes this.
  6. Yeah just adjusted my code to let that fit in...It works now but isn't really smooth...

    Rprrr Bancey chasechocolate, fixed it, I changed the my vector calculator so it returns the direction the player is looking but multiplied by 4, I don't like it this way but yeah...

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

    LucasEmanuel

    Use the vector retrieved from location.getDirection(), using that vector we are going to loop over all blocks in sight until we hit a solid.

    Code:
    Location loc = player.getEyeLocation();
     
    Vector v = loc.getDirection().normalize();
     
    for(int i = 1 ; i <= maxDistance ; i++) {
      loc.add(v);
      if(loc.getBlock().getType != Material.AIR)
        return loc;
    }
    EDIT:
    And I know my tutorial is lacking some stuff and needs to have a look over, I just haven't had the time for it ;)
     
  8. Offline

    Bancey

    I didn't get the coords as such but I saved it to a location;
    Code:java
    1. Location loc = p.getEyeLocation().toVector().add(p.getLocation().getDirection().multiply(2)).toLocation(p.getWorld(), p.getLocation().getYaw(), p.getLocation().getPitch());
     
Thread Status:
Not open for further replies.

Share This Page