|
|
|
@ -24,8 +24,8 @@ namespace pocketmine\math;
|
|
|
|
|
class Vector3{
|
|
|
|
|
|
|
|
|
|
/** @var Vector3[] */
|
|
|
|
|
private static $vectorList = [];
|
|
|
|
|
private static $nextVector = 0;
|
|
|
|
|
public static $vectorList = [];
|
|
|
|
|
public static $nextVector = 0;
|
|
|
|
|
|
|
|
|
|
const SIDE_DOWN = 0;
|
|
|
|
|
const SIDE_UP = 1;
|
|
|
|
@ -45,15 +45,15 @@ class Vector3{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function clearVectors(){
|
|
|
|
|
self::$nextVector = 0;
|
|
|
|
|
self::$vectorList = [];
|
|
|
|
|
Vector3::$nextVector = 0;
|
|
|
|
|
Vector3::$vectorList = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function clearVectorList(){
|
|
|
|
|
if(self::$nextVector > 65536){
|
|
|
|
|
self::clearVectors();
|
|
|
|
|
if(Vector3::$nextVector > 65536){
|
|
|
|
|
Vector3::clearVectors();
|
|
|
|
|
}else{
|
|
|
|
|
self::$nextVector = 0;
|
|
|
|
|
Vector3::$nextVector = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -65,11 +65,11 @@ class Vector3{
|
|
|
|
|
* @return Vector3
|
|
|
|
|
*/
|
|
|
|
|
public static function createVector($x, $y, $z){
|
|
|
|
|
if(self::$nextVector >= count(self::$vectorList)){
|
|
|
|
|
self::$vectorList[] = new Vector3(0, 0, 0);
|
|
|
|
|
if(Vector3::$nextVector >= count(Vector3::$vectorList)){
|
|
|
|
|
Vector3::$vectorList[] = new Vector3(0, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self::$vectorList[self::$nextVector++]->setComponents($x, $y, $z);
|
|
|
|
|
return Vector3::$vectorList[Vector3::$nextVector++]->setComponents($x, $y, $z);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -78,11 +78,11 @@ class Vector3{
|
|
|
|
|
* @return Vector3
|
|
|
|
|
*/
|
|
|
|
|
public static function cloneVector(Vector3 $vector){
|
|
|
|
|
if(self::$nextVector >= count(self::$vectorList)){
|
|
|
|
|
self::$vectorList[] = new Vector3(0, 0, 0);
|
|
|
|
|
if(Vector3::$nextVector >= count(Vector3::$vectorList)){
|
|
|
|
|
Vector3::$vectorList[] = new Vector3(0, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self::$vectorList[self::$nextVector++]->setComponents($vector->x, $vector->y, $vector->z);
|
|
|
|
|
return Vector3::$vectorList[Vector3::$nextVector++]->setComponents($vector->x, $vector->y, $vector->z);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getX(){
|
|
|
|
@ -138,9 +138,9 @@ class Vector3{
|
|
|
|
|
*/
|
|
|
|
|
public function add($x, $y = 0, $z = 0){
|
|
|
|
|
if($x instanceof Vector3){
|
|
|
|
|
return self::createVector($this->x + $x->x, $this->y + $x->y, $this->z + $x->z);
|
|
|
|
|
return Vector3::createVector($this->x + $x->x, $this->y + $x->y, $this->z + $x->z);
|
|
|
|
|
}else{
|
|
|
|
|
return self::createVector($this->x + $x, $this->y + $y, $this->z + $z);
|
|
|
|
|
return Vector3::createVector($this->x + $x, $this->y + $y, $this->z + $z);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -160,46 +160,46 @@ class Vector3{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function multiply($number){
|
|
|
|
|
return self::createVector($this->x * $number, $this->y * $number, $this->z * $number);
|
|
|
|
|
return Vector3::createVector($this->x * $number, $this->y * $number, $this->z * $number);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function divide($number){
|
|
|
|
|
return self::createVector($this->x / $number, $this->y / $number, $this->z / $number);
|
|
|
|
|
return Vector3::createVector($this->x / $number, $this->y / $number, $this->z / $number);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function ceil(){
|
|
|
|
|
return self::createVector((int) ($this->x + 1), (int) ($this->y + 1), (int) ($this->z + 1));
|
|
|
|
|
return Vector3::createVector((int) ($this->x + 1), (int) ($this->y + 1), (int) ($this->z + 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function floor(){
|
|
|
|
|
$x = (int) $this->x;
|
|
|
|
|
$y = (int) $this->y;
|
|
|
|
|
$z = (int) $this->z;
|
|
|
|
|
return new Vector3($this->x >= $x ? $x : $x - 1, $this->y >= $y ? $y : $y - 1, $this->z >= $z ? $z : $z - 1);
|
|
|
|
|
return Vector3::createVector($this->x >= $x ? $x : $x - 1, $this->y >= $y ? $y : $y - 1, $this->z >= $z ? $z : $z - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function round(){
|
|
|
|
|
return new Vector3(round($this->x), round($this->y), round($this->z));
|
|
|
|
|
return Vector3::createVector(round($this->x), round($this->y), round($this->z));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function abs(){
|
|
|
|
|
return new Vector3(abs($this->x), abs($this->y), abs($this->z));
|
|
|
|
|
return Vector3::createVector(abs($this->x), abs($this->y), abs($this->z));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getSide($side, $step = 1){
|
|
|
|
|
switch((int) $side){
|
|
|
|
|
case self::SIDE_DOWN:
|
|
|
|
|
return self::createVector($this->x, $this->y - $step, $this->z);
|
|
|
|
|
case self::SIDE_UP:
|
|
|
|
|
return self::createVector($this->x, $this->y + $step, $this->z);
|
|
|
|
|
case self::SIDE_NORTH:
|
|
|
|
|
return self::createVector($this->x, $this->y, $this->z - $step);
|
|
|
|
|
case self::SIDE_SOUTH:
|
|
|
|
|
return self::createVector($this->x, $this->y, $this->z + $step);
|
|
|
|
|
case self::SIDE_WEST:
|
|
|
|
|
return self::createVector($this->x - $step, $this->y, $this->z);
|
|
|
|
|
case self::SIDE_EAST:
|
|
|
|
|
return self::createVector($this->x + $step, $this->y, $this->z);
|
|
|
|
|
case Vector3::SIDE_DOWN:
|
|
|
|
|
return Vector3::createVector($this->x, $this->y - $step, $this->z);
|
|
|
|
|
case Vector3::SIDE_UP:
|
|
|
|
|
return Vector3::createVector($this->x, $this->y + $step, $this->z);
|
|
|
|
|
case Vector3::SIDE_NORTH:
|
|
|
|
|
return Vector3::createVector($this->x, $this->y, $this->z - $step);
|
|
|
|
|
case Vector3::SIDE_SOUTH:
|
|
|
|
|
return Vector3::createVector($this->x, $this->y, $this->z + $step);
|
|
|
|
|
case Vector3::SIDE_WEST:
|
|
|
|
|
return Vector3::createVector($this->x - $step, $this->y, $this->z);
|
|
|
|
|
case Vector3::SIDE_EAST:
|
|
|
|
|
return Vector3::createVector($this->x + $step, $this->y, $this->z);
|
|
|
|
|
default:
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
@ -207,18 +207,18 @@ class Vector3{
|
|
|
|
|
|
|
|
|
|
public static function getOppositeSide($side){
|
|
|
|
|
switch((int) $side){
|
|
|
|
|
case self::SIDE_DOWN:
|
|
|
|
|
return self::SIDE_UP;
|
|
|
|
|
case self::SIDE_UP:
|
|
|
|
|
return self::SIDE_DOWN;
|
|
|
|
|
case self::SIDE_NORTH:
|
|
|
|
|
return self::SIDE_SOUTH;
|
|
|
|
|
case self::SIDE_SOUTH:
|
|
|
|
|
return self::SIDE_NORTH;
|
|
|
|
|
case self::SIDE_WEST:
|
|
|
|
|
return self::SIDE_EAST;
|
|
|
|
|
case self::SIDE_EAST:
|
|
|
|
|
return self::SIDE_WEST;
|
|
|
|
|
case Vector3::SIDE_DOWN:
|
|
|
|
|
return Vector3::SIDE_UP;
|
|
|
|
|
case Vector3::SIDE_UP:
|
|
|
|
|
return Vector3::SIDE_DOWN;
|
|
|
|
|
case Vector3::SIDE_NORTH:
|
|
|
|
|
return Vector3::SIDE_SOUTH;
|
|
|
|
|
case Vector3::SIDE_SOUTH:
|
|
|
|
|
return Vector3::SIDE_NORTH;
|
|
|
|
|
case Vector3::SIDE_WEST:
|
|
|
|
|
return Vector3::SIDE_EAST;
|
|
|
|
|
case Vector3::SIDE_EAST:
|
|
|
|
|
return Vector3::SIDE_WEST;
|
|
|
|
|
default:
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
@ -259,7 +259,7 @@ class Vector3{
|
|
|
|
|
return $this->divide($len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self::createVector(0, 0, 0);
|
|
|
|
|
return Vector3::createVector(0, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function dot(Vector3 $v){
|
|
|
|
@ -267,7 +267,7 @@ class Vector3{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function cross(Vector3 $v){
|
|
|
|
|
return self::createVector(
|
|
|
|
|
return Vector3::createVector(
|
|
|
|
|
$this->y * $v->z - $this->z * $v->y,
|
|
|
|
|
$this->z * $v->x - $this->x * $v->z,
|
|
|
|
|
$this->x * $v->y - $this->y * $v->x
|
|
|
|
@ -297,7 +297,7 @@ class Vector3{
|
|
|
|
|
if($f < 0 or $f > 1){
|
|
|
|
|
return null;
|
|
|
|
|
}else{
|
|
|
|
|
return self::createVector($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f);
|
|
|
|
|
return Vector3::createVector($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -324,7 +324,7 @@ class Vector3{
|
|
|
|
|
if($f < 0 or $f > 1){
|
|
|
|
|
return null;
|
|
|
|
|
}else{
|
|
|
|
|
return self::createVector($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f);
|
|
|
|
|
return Vector3::createVector($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -351,7 +351,7 @@ class Vector3{
|
|
|
|
|
if($f < 0 or $f > 1){
|
|
|
|
|
return null;
|
|
|
|
|
}else{
|
|
|
|
|
return self::createVector($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f);
|
|
|
|
|
return Vector3::createVector($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|