PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
Vector3.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\math;
23 
24 class Vector3{
25 
26  const SIDE_DOWN = 0;
27  const SIDE_UP = 1;
28  const SIDE_NORTH = 2;
29  const SIDE_SOUTH = 3;
30  const SIDE_WEST = 4;
31  const SIDE_EAST = 5;
32 
33  public $x;
34  public $y;
35  public $z;
36 
37  public function __construct($x = 0, $y = 0, $z = 0){
38  $this->x = $x;
39  $this->y = $y;
40  $this->z = $z;
41  }
42 
43  public function getX(){
44  return $this->x;
45  }
46 
47  public function getY(){
48  return $this->y;
49  }
50 
51  public function getZ(){
52  return $this->z;
53  }
54 
55  public function getFloorX(){
56  return (int) $this->x;
57  }
58 
59  public function getFloorY(){
60  return (int) $this->y;
61  }
62 
63  public function getFloorZ(){
64  return (int) $this->z;
65  }
66 
67  public function getRight(){
68  return $this->x;
69  }
70 
71  public function getUp(){
72  return $this->y;
73  }
74 
75  public function getForward(){
76  return $this->z;
77  }
78 
79  public function getSouth(){
80  return $this->x;
81  }
82 
83  public function getWest(){
84  return $this->z;
85  }
86 
94  public function add($x, $y = 0, $z = 0){
95  if($x instanceof Vector3){
96  return new Vector3($this->x + $x->x, $this->y + $x->y, $this->z + $x->z);
97  }else{
98  return new Vector3($this->x + $x, $this->y + $y, $this->z + $z);
99  }
100  }
101 
109  public function subtract($x = 0, $y = 0, $z = 0){
110  if($x instanceof Vector3){
111  return $this->add(-$x->x, -$x->y, -$x->z);
112  }else{
113  return $this->add(-$x, -$y, -$z);
114  }
115  }
116 
117  public function multiply($number){
118  return new Vector3($this->x * $number, $this->y * $number, $this->z * $number);
119  }
120 
121  public function divide($number){
122  return new Vector3($this->x / $number, $this->y / $number, $this->z / $number);
123  }
124 
125  public function ceil(){
126  return new Vector3((int) ($this->x + 1), (int) ($this->y + 1), (int) ($this->z + 1));
127  }
128 
129  public function floor(){
130  $x = (int) $this->x;
131  $y = (int) $this->y;
132  $z = (int) $this->z;
133  return new Vector3($this->x >= $x ? $x : $x - 1, $this->y >= $y ? $y : $y - 1, $this->z >= $z ? $z : $z - 1);
134  }
135 
136  public function round(){
137  return new Vector3(round($this->x), round($this->y), round($this->z));
138  }
139 
140  public function abs(){
141  return new Vector3(abs($this->x), abs($this->y), abs($this->z));
142  }
143 
144  public function getSide($side, $step = 1){
145  switch((int) $side){
146  case Vector3::SIDE_DOWN:
147  return new Vector3($this->x, $this->y - $step, $this->z);
148  case Vector3::SIDE_UP:
149  return new Vector3($this->x, $this->y + $step, $this->z);
150  case Vector3::SIDE_NORTH:
151  return new Vector3($this->x, $this->y, $this->z - $step);
152  case Vector3::SIDE_SOUTH:
153  return new Vector3($this->x, $this->y, $this->z + $step);
154  case Vector3::SIDE_WEST:
155  return new Vector3($this->x - $step, $this->y, $this->z);
156  case Vector3::SIDE_EAST:
157  return new Vector3($this->x + $step, $this->y, $this->z);
158  default:
159  return $this;
160  }
161  }
162 
163  public static function getOppositeSide($side){
164  switch((int) $side){
165  case Vector3::SIDE_DOWN:
166  return Vector3::SIDE_UP;
167  case Vector3::SIDE_UP:
168  return Vector3::SIDE_DOWN;
169  case Vector3::SIDE_NORTH:
170  return Vector3::SIDE_SOUTH;
171  case Vector3::SIDE_SOUTH:
172  return Vector3::SIDE_NORTH;
173  case Vector3::SIDE_WEST:
174  return Vector3::SIDE_EAST;
175  case Vector3::SIDE_EAST:
176  return Vector3::SIDE_WEST;
177  default:
178  return -1;
179  }
180  }
181 
182  public function distance(Vector3 $pos){
183  return sqrt($this->distanceSquared($pos));
184  }
185 
186  public function distanceSquared(Vector3 $pos){
187  return pow($this->x - $pos->x, 2) + pow($this->y - $pos->y, 2) + pow($this->z - $pos->z, 2);
188  }
189 
190  public function maxPlainDistance($x = 0, $z = 0){
191  if($x instanceof Vector3){
192  return $this->maxPlainDistance($x->x, $x->z);
193  }elseif($x instanceof Vector2){
194  return $this->maxPlainDistance($x->x, $x->y);
195  }else{
196  return max(abs($this->x - $x), abs($this->z - $z));
197  }
198  }
199 
200  public function length(){
201  return sqrt($this->lengthSquared());
202  }
203 
204  public function lengthSquared(){
205  return $this->x ** 2 + $this->y ** 2 + $this->z ** 2;
206  }
207 
211  public function normalize(){
212  $len = $this->length();
213  if($len != 0){
214  return $this->divide($len);
215  }
216 
217  return new Vector3(0, 0, 0);
218  }
219 
220  public function dot(Vector3 $v){
221  return $this->x * $v->x + $this->y * $v->y + $this->z * $v->z;
222  }
223 
224  public function cross(Vector3 $v){
225  return new Vector3(
226  $this->y * $v->z - $this->z * $v->y,
227  $this->z * $v->x - $this->x * $v->z,
228  $this->x * $v->y - $this->y * $v->x
229  );
230  }
231 
241  public function getIntermediateWithXValue(Vector3 $v, $x){
242  $xDiff = $v->x - $this->x;
243  $yDiff = $v->y - $this->y;
244  $zDiff = $v->z - $this->z;
245 
246  if(($xDiff ** 2) < 1){
247  return null;
248  }
249 
250  $f = ($x - $this->x) / $xDiff;
251 
252  if($f < 0 or $f > 1){
253  return null;
254  }else{
255  return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f);
256  }
257  }
258 
268  public function getIntermediateWithYValue(Vector3 $v, $y){
269  $xDiff = $v->x - $this->x;
270  $yDiff = $v->y - $this->y;
271  $zDiff = $v->z - $this->z;
272 
273  if(($yDiff ** 2) < 1){
274  return null;
275  }
276 
277  $f = ($y - $this->y) / $yDiff;
278 
279  if($f < 0 or $f > 1){
280  return null;
281  }else{
282  return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f);
283  }
284  }
285 
295  public function getIntermediateWithZValue(Vector3 $v, $z){
296  $xDiff = $v->x - $this->x;
297  $yDiff = $v->y - $this->y;
298  $zDiff = $v->z - $this->z;
299 
300  if(($zDiff ** 2) < 1){
301  return null;
302  }
303 
304  $f = ($z - $this->z) / $zDiff;
305 
306  if($f < 0 or $f > 1){
307  return null;
308  }else{
309  return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f);
310  }
311  }
312 
320  public function setComponents($x, $y, $z){
321  $this->x = $x;
322  $this->y = $y;
323  $this->z = $z;
324  return $this;
325  }
326 
327  public function __toString(){
328  return "Vector3(x=" . $this->x . ",y=" . $this->y . ",z=" . $this->z . ")";
329  }
330 
331 }
setComponents($x, $y, $z)
Definition: Vector3.php:320
getIntermediateWithZValue(Vector3 $v, $z)
Definition: Vector3.php:295
add($x, $y=0, $z=0)
Definition: Vector3.php:94
getIntermediateWithYValue(Vector3 $v, $y)
Definition: Vector3.php:268
subtract($x=0, $y=0, $z=0)
Definition: Vector3.php:109
getIntermediateWithXValue(Vector3 $v, $x)
Definition: Vector3.php:241