1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Get "real" the block in front of an entity

Discussion in 'Development' started by kniffo80, Apr 5, 2017.

  1. kniffo80

    kniffo80 Silverfish

    Messages:
    24
    GitHub:
    kniffo80
    Hi all,

    when I use the function getPosition of an Entity, what does this position represent? The middle of the entity? I think the y-coordinate is the coordinate of the feet of the entity right? But where is x and z measured? Top left, Top right? I need the information to get correct information on the block which is in front of the entity. Sometimes, entities overlap blocks. So when getting the block in front of the entity, sometimes i get another block (the block behind the first block in line) when e.g. using the BlockIterator.

    Anyway - here's a small sample code snippet (while this is an Entity):

    Code:
    $directionBlock = null;
    $currentBlock = $this->level->getBlock($this->temporalVector->setComponents(Math::floorFloat($this->x), Math::floorFloat($this->y), Math::floorFloat($this->z)));
    
    switch($this->getDirection()) {
        case 2: // NORTH
            $directionBlock = $this->level->getBlock($currentBlock->add(-1 - ($this->length / 2), 0, 0));
            break;
        case 0: // SOUTH
            $directionBlock = $this->level->getBlock($currentBlock->add(1 + ($this->length / 2), 0, 0));
            break;
        case 1: // WEST
            $directionBlock = $this->level->getBlock($currentBlock->add(0, 0,2 + ($this->length / 2)));
            break;
        case 3: // EAST
            $directionBlock = $this->level->getBlock($currentBlock->add(0, 0,-2 - ($this->length / 2)));
            break;
    }
    
    Direction is taken into account. But i personally think, that only using the position of the entity is the wrong way? Maybe we need to divide something through the length and width of the entity?

    Any help appreciated ;)
     
  2. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
  3. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Noooo, you are recreating a function. Don't do that. Just..
    PHP:
    /** @var Player $player */
    $maxDistance 100;
    $block $player->getTargetBlock($maxDistance);
    Position represents the feet of the entity.
     
    jasonwynn10, SOFe and corytortoise like this.
  4. kniffo80

    kniffo80 Silverfish

    Messages:
    24
    GitHub:
    kniffo80
    Thanks for your answers. I will check.
     
  5. kniffo80

    kniffo80 Silverfish

    Messages:
    24
    GitHub:
    kniffo80
    It seems it is working (not in each case, but in general). Thanks for your answers! Using now getTargetBlock - although it uses the heavy weighted BlockIterator.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.