[Vector Math] Getting the block the player is looking at with precise collision point

Discussion in 'Plugin Development' started by Ziden, Oct 5, 2014.

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

    Ziden

    Hi !

    The goal of this is to play an effect, in a line, from where the player is lookin at to a point, and this is already done:

    Code:
    Vector bulletVector = GunMechanic.getShootVector(p, getRange());
           
            // calculating the locations
            Location begin = p.getEyeLocation();
            Location end = p.getEyeLocation().toVector().add(bulletVector).toLocation(p.getWorld());
           
           
            Block target = p.getTargetBlock(null, getRange());
            if(target!=null) {
                p.getWorld().playEffect(target.getLocation(), Effect.STEP_SOUND, target.getTypeId(), 3);
            }
            GunMechanic.effectLine(Effect.PARTICLE_SMOKE, begin, end, 20);
    However, this aways draws the effect at maximum range.

    I can detect the block the player is lookin at and set the end position to it, but it will tweak the line a little bit to fit integer values of the block zyx while the vector draws precisely with double values.

    So i need to know , the exact collision point that the player eye vector has with a block so i can do a precise draw of the particle effect line.

    Does anyone knows how to perform this ?

    Thanks alot in advance.
     
  2. Offline

    Skionz

    You can do something like this
    Code:java
    1. Location loc = player.getLocation();
    2. loc.add(loc.getDirection().multiply(10));

    Gets the exact point the player is looking at 10 blocks away. You could just increment the value.
     
  3. Offline

    MnMaxon

    Ziden
    Code:java
    1. // Vector bulletVector = GunMechanic.getShootVector(p, getRange()); - I don't know what this is
    2.  
    3. // calculating the locations
    4. Location begin = p.getEyeLocation();
    5. Location end = begin;
    6. double range = getRange();
    7. while(!end.getBlock().getType().isSolid() && end.distance(begin) < range) {
    8. end = end.add(p.getLocation().getDirection().multiply(.1));
    9. }
    10.  
    11. // I didn't change anything below this
    12. Block target = p.getTargetBlock(null, getRange());
    13. if(target!=null) {
    14. p.getWorld().playEffect(target.getLocation(), Effect.STEP_SOUND, target.getTypeId(), 3);
    15. }
    16. GunMechanic.effectLine(Effect.PARTICLE_SMOKE, begin, end, 20);[FONT=Georgia][/FONT]

    I'm not really sure what bulletVector was, so I ignored it. I also assumed getRange() was a double.

    If you have any questions about it, or my assumptions were wrong, tell me.
     
Thread Status:
Not open for further replies.

Share This Page