pocketmine\math\Vector3::getIntermediateWithZValue PHP Method

getIntermediateWithZValue() public method

Returns a new vector with z value equal to the second parameter, along the line between this vector and the passed in vector, or null if not possible.
public getIntermediateWithZValue ( Vector3 $v, float $z ) : Vector3
$v Vector3
$z float
return Vector3
    public function getIntermediateWithZValue(Vector3 $v, $z)
    {
        $xDiff = $v->x - $this->x;
        $yDiff = $v->y - $this->y;
        $zDiff = $v->z - $this->z;
        if ($zDiff * $zDiff < 1.0E-7) {
            return null;
        }
        $f = ($z - $this->z) / $zDiff;
        if ($f < 0 or $f > 1) {
            return null;
        } else {
            return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f);
        }
    }

Usage Example

Beispiel #1
0
 /**
  * @param Vector3 $pos1
  * @param Vector3 $pos2
  *
  * @return MovingObjectPosition
  */
 public function calculateIntercept(Vector3 $pos1, Vector3 $pos2)
 {
     $bb = $this->getBoundingBox();
     if ($bb === null) {
         return null;
     }
     $v1 = $pos1->getIntermediateWithXValue($pos2, $bb->minX);
     $v2 = $pos1->getIntermediateWithXValue($pos2, $bb->maxX);
     $v3 = $pos1->getIntermediateWithYValue($pos2, $bb->minY);
     $v4 = $pos1->getIntermediateWithYValue($pos2, $bb->maxY);
     $v5 = $pos1->getIntermediateWithZValue($pos2, $bb->minZ);
     $v6 = $pos1->getIntermediateWithZValue($pos2, $bb->maxZ);
     if ($v1 !== null and !$bb->isVectorInYZ($v1)) {
         $v1 = null;
     }
     if ($v2 !== null and !$bb->isVectorInYZ($v2)) {
         $v2 = null;
     }
     if ($v3 !== null and !$bb->isVectorInXZ($v3)) {
         $v3 = null;
     }
     if ($v4 !== null and !$bb->isVectorInXZ($v4)) {
         $v4 = null;
     }
     if ($v5 !== null and !$bb->isVectorInXY($v5)) {
         $v5 = null;
     }
     if ($v6 !== null and !$bb->isVectorInXY($v6)) {
         $v6 = null;
     }
     $vector = $v1;
     if ($v2 !== null and ($vector === null or $pos1->distanceSquared($v2) < $pos1->distanceSquared($vector))) {
         $vector = $v2;
     }
     if ($v3 !== null and ($vector === null or $pos1->distanceSquared($v3) < $pos1->distanceSquared($vector))) {
         $vector = $v3;
     }
     if ($v4 !== null and ($vector === null or $pos1->distanceSquared($v4) < $pos1->distanceSquared($vector))) {
         $vector = $v4;
     }
     if ($v5 !== null and ($vector === null or $pos1->distanceSquared($v5) < $pos1->distanceSquared($vector))) {
         $vector = $v5;
     }
     if ($v6 !== null and ($vector === null or $pos1->distanceSquared($v6) < $pos1->distanceSquared($vector))) {
         $vector = $v6;
     }
     if ($vector === null) {
         return null;
     }
     $f = -1;
     if ($vector === $v1) {
         $f = 4;
     } elseif ($vector === $v2) {
         $f = 5;
     } elseif ($vector === $v3) {
         $f = 0;
     } elseif ($vector === $v4) {
         $f = 1;
     } elseif ($vector === $v5) {
         $f = 2;
     } elseif ($vector === $v6) {
         $f = 3;
     }
     return MovingObjectPosition::fromBlock($this->x, $this->y, $this->z, $f, $vector->add($this->x, $this->y, $this->z));
 }
All Usage Examples Of pocketmine\math\Vector3::getIntermediateWithZValue