/src/pocketmine/math/Vector3.php

https://gitlab.com/wesleyvanneck/ImagicalMine · PHP · 545 lines · 248 code · 87 blank · 210 comment · 21 complexity · 2ca48688510f542499a24e11f9c45133 MD5 · raw file

  1. <?php
  2. /**
  3. * src/pocketmine/math/Vector3.php
  4. *
  5. * @package default
  6. */
  7. /*
  8. *
  9. * _ _ _ __ __ _
  10. * (_) (_) | | \/ (_)
  11. * _ _ __ ___ __ _ __ _ _ ___ __ _| | \ / |_ _ __ ___
  12. * | | '_ ` _ \ / _` |/ _` | |/ __/ _` | | |\/| | | '_ \ / _ \
  13. * | | | | | | | (_| | (_| | | (_| (_| | | | | | | | | | __/
  14. * |_|_| |_| |_|\__,_|\__, |_|\___\__,_|_|_| |_|_|_| |_|\___|
  15. * __/ |
  16. * |___/
  17. *
  18. * This program is a third party build by ImagicalMine.
  19. *
  20. * PocketMine is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Lesser General Public License as published by
  22. * the Free Software Foundation, either version 3 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * @author ImagicalMine Team
  26. * @link http://forums.imagicalcorp.ml/
  27. *
  28. *
  29. */
  30. namespace pocketmine\math;
  31. class Vector3
  32. {
  33. const SIDE_DOWN = 0;
  34. const SIDE_UP = 1;
  35. const SIDE_NORTH = 2;
  36. const SIDE_SOUTH = 3;
  37. const SIDE_WEST = 4;
  38. const SIDE_EAST = 5;
  39. public $x;
  40. public $y;
  41. public $z;
  42. /**
  43. *
  44. * @param unknown $x (optional)
  45. * @param unknown $y (optional)
  46. * @param unknown $z (optional)
  47. */
  48. public function __construct($x = 0, $y = 0, $z = 0)
  49. {
  50. $this->x = $x;
  51. $this->y = $y;
  52. $this->z = $z;
  53. }
  54. /**
  55. *
  56. * @return unknown
  57. */
  58. public function getX()
  59. {
  60. return $this->x;
  61. }
  62. /**
  63. *
  64. * @return unknown
  65. */
  66. public function getY()
  67. {
  68. return $this->y;
  69. }
  70. /**
  71. *
  72. * @return unknown
  73. */
  74. public function getZ()
  75. {
  76. return $this->z;
  77. }
  78. /**
  79. *
  80. * @return unknown
  81. */
  82. public function getFloorX()
  83. {
  84. return (int) floor($this->x);
  85. }
  86. /**
  87. *
  88. * @return unknown
  89. */
  90. public function getFloorY()
  91. {
  92. return (int) floor($this->y);
  93. }
  94. /**
  95. *
  96. * @return unknown
  97. */
  98. public function getFloorZ()
  99. {
  100. return (int) floor($this->z);
  101. }
  102. /**
  103. *
  104. * @return unknown
  105. */
  106. public function getRight()
  107. {
  108. return $this->x;
  109. }
  110. /**
  111. *
  112. * @return unknown
  113. */
  114. public function getUp()
  115. {
  116. return $this->y;
  117. }
  118. /**
  119. *
  120. * @return unknown
  121. */
  122. public function getForward()
  123. {
  124. return $this->z;
  125. }
  126. /**
  127. *
  128. * @return unknown
  129. */
  130. public function getSouth()
  131. {
  132. return $this->x;
  133. }
  134. /**
  135. *
  136. * @return unknown
  137. */
  138. public function getWest()
  139. {
  140. return $this->z;
  141. }
  142. /**
  143. *
  144. * @param Vector3|int $x
  145. * @param int $y (optional)
  146. * @param int $z (optional)
  147. * @return Vector3
  148. */
  149. public function add($x, $y = 0, $z = 0)
  150. {
  151. if ($x instanceof Vector3) {
  152. return new Vector3($this->x + $x->x, $this->y + $x->y, $this->z + $x->z);
  153. } else {
  154. return new Vector3($this->x + $x, $this->y + $y, $this->z + $z);
  155. }
  156. }
  157. /**
  158. *
  159. * @param Vector3|int $x
  160. * @param int $y (optional)
  161. * @param int $z (optional)
  162. * @return Vector3
  163. */
  164. public function subtract($x = 0, $y = 0, $z = 0)
  165. {
  166. if ($x instanceof Vector3) {
  167. return $this->add(-$x->x, -$x->y, -$x->z);
  168. } else {
  169. return $this->add(-$x, -$y, -$z);
  170. }
  171. }
  172. /**
  173. *
  174. * @param unknown $number
  175. * @return unknown
  176. */
  177. public function multiply($number)
  178. {
  179. return new Vector3($this->x * $number, $this->y * $number, $this->z * $number);
  180. }
  181. /**
  182. *
  183. * @param unknown $number
  184. * @return unknown
  185. */
  186. public function divide($number)
  187. {
  188. return new Vector3($this->x / $number, $this->y / $number, $this->z / $number);
  189. }
  190. /**
  191. *
  192. * @return unknown
  193. */
  194. public function ceil()
  195. {
  196. return new Vector3((int) ceil($this->x), (int) ceil($this->y), (int) ceil($this->z));
  197. }
  198. /**
  199. *
  200. * @return unknown
  201. */
  202. public function floor()
  203. {
  204. return new Vector3((int) floor($this->x), (int) floor($this->y), (int) floor($this->z));
  205. }
  206. /**
  207. *
  208. * @return unknown
  209. */
  210. public function round()
  211. {
  212. return new Vector3((int) round($this->x), (int) round($this->y), (int) round($this->z));
  213. }
  214. /**
  215. *
  216. * @return unknown
  217. */
  218. public function abs()
  219. {
  220. return new Vector3(abs($this->x), abs($this->y), abs($this->z));
  221. }
  222. /**
  223. *
  224. * @param unknown $side
  225. * @param unknown $step (optional)
  226. * @return unknown
  227. */
  228. public function getSide($side, $step = 1)
  229. {
  230. switch ((int) $side) {
  231. case Vector3::SIDE_DOWN:
  232. return new Vector3($this->x, $this->y - $step, $this->z);
  233. case Vector3::SIDE_UP:
  234. return new Vector3($this->x, $this->y + $step, $this->z);
  235. case Vector3::SIDE_NORTH:
  236. return new Vector3($this->x, $this->y, $this->z - $step);
  237. case Vector3::SIDE_SOUTH:
  238. return new Vector3($this->x, $this->y, $this->z + $step);
  239. case Vector3::SIDE_WEST:
  240. return new Vector3($this->x - $step, $this->y, $this->z);
  241. case Vector3::SIDE_EAST:
  242. return new Vector3($this->x + $step, $this->y, $this->z);
  243. default:
  244. return $this;
  245. }
  246. }
  247. /**
  248. *
  249. * @param unknown $side
  250. * @return unknown
  251. */
  252. public static function getOppositeSide($side)
  253. {
  254. switch ((int) $side) {
  255. case Vector3::SIDE_DOWN:
  256. return Vector3::SIDE_UP;
  257. case Vector3::SIDE_UP:
  258. return Vector3::SIDE_DOWN;
  259. case Vector3::SIDE_NORTH:
  260. return Vector3::SIDE_SOUTH;
  261. case Vector3::SIDE_SOUTH:
  262. return Vector3::SIDE_NORTH;
  263. case Vector3::SIDE_WEST:
  264. return Vector3::SIDE_EAST;
  265. case Vector3::SIDE_EAST:
  266. return Vector3::SIDE_WEST;
  267. default:
  268. return -1;
  269. }
  270. }
  271. /**
  272. *
  273. * @param Vector3 $pos
  274. * @return unknown
  275. */
  276. public function distance(Vector3 $pos)
  277. {
  278. return sqrt($this->distanceSquared($pos));
  279. }
  280. /**
  281. *
  282. * @param Vector3 $pos
  283. * @return unknown
  284. */
  285. public function distanceSquared(Vector3 $pos)
  286. {
  287. return pow($this->x - $pos->x, 2) + pow($this->y - $pos->y, 2) + pow($this->z - $pos->z, 2);
  288. }
  289. /**
  290. *
  291. * @param unknown $x (optional)
  292. * @param unknown $z (optional)
  293. * @return unknown
  294. */
  295. public function maxPlainDistance($x = 0, $z = 0)
  296. {
  297. if ($x instanceof Vector3) {
  298. return $this->maxPlainDistance($x->x, $x->z);
  299. } elseif ($x instanceof Vector2) {
  300. return $this->maxPlainDistance($x->x, $x->y);
  301. } else {
  302. return max(abs($this->x - $x), abs($this->z - $z));
  303. }
  304. }
  305. /**
  306. *
  307. * @return unknown
  308. */
  309. public function length()
  310. {
  311. return sqrt($this->lengthSquared());
  312. }
  313. /**
  314. *
  315. * @return unknown
  316. */
  317. public function lengthSquared()
  318. {
  319. return $this->x * $this->x + $this->y * $this->y + $this->z * $this->z;
  320. }
  321. /**
  322. *
  323. * @return Vector3
  324. */
  325. public function normalize()
  326. {
  327. $len = $this->lengthSquared();
  328. if ($len > 0) {
  329. return $this->divide(sqrt($len));
  330. }
  331. return new Vector3(0, 0, 0);
  332. }
  333. /**
  334. *
  335. * @param Vector3 $v
  336. * @return unknown
  337. */
  338. public function dot(Vector3 $v)
  339. {
  340. return $this->x * $v->x + $this->y * $v->y + $this->z * $v->z;
  341. }
  342. /**
  343. *
  344. * @param Vector3 $v
  345. * @return unknown
  346. */
  347. public function cross(Vector3 $v)
  348. {
  349. return new Vector3(
  350. $this->y * $v->z - $this->z * $v->y,
  351. $this->z * $v->x - $this->x * $v->z,
  352. $this->x * $v->y - $this->y * $v->x
  353. );
  354. }
  355. /**
  356. *
  357. * @param Vector3 $v
  358. * @return unknown
  359. */
  360. public function equals(Vector3 $v)
  361. {
  362. return $this->x == $v->x and $this->y == $v->y and $this->z == $v->z;
  363. }
  364. /**
  365. * Returns a new vector with x value equal to the second parameter, along the line between this vector and the
  366. * passed in vector, or null if not possible.
  367. *
  368. *
  369. * @param Vector3 $v
  370. * @param float $x
  371. * @return Vector3
  372. */
  373. public function getIntermediateWithXValue(Vector3 $v, $x)
  374. {
  375. $xDiff = $v->x - $this->x;
  376. $yDiff = $v->y - $this->y;
  377. $zDiff = $v->z - $this->z;
  378. if (($xDiff * $xDiff) < 0.0000001) {
  379. return null;
  380. }
  381. $f = ($x - $this->x) / $xDiff;
  382. if ($f < 0 or $f > 1) {
  383. return null;
  384. } else {
  385. return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f);
  386. }
  387. }
  388. /**
  389. * Returns a new vector with y value equal to the second parameter, along the line between this vector and the
  390. * passed in vector, or null if not possible.
  391. *
  392. *
  393. * @param Vector3 $v
  394. * @param float $y
  395. * @return Vector3
  396. */
  397. public function getIntermediateWithYValue(Vector3 $v, $y)
  398. {
  399. $xDiff = $v->x - $this->x;
  400. $yDiff = $v->y - $this->y;
  401. $zDiff = $v->z - $this->z;
  402. if (($yDiff * $yDiff) < 0.0000001) {
  403. return null;
  404. }
  405. $f = ($y - $this->y) / $yDiff;
  406. if ($f < 0 or $f > 1) {
  407. return null;
  408. } else {
  409. return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f);
  410. }
  411. }
  412. /**
  413. * Returns a new vector with z value equal to the second parameter, along the line between this vector and the
  414. * passed in vector, or null if not possible.
  415. *
  416. *
  417. * @param Vector3 $v
  418. * @param float $z
  419. * @return Vector3
  420. */
  421. public function getIntermediateWithZValue(Vector3 $v, $z)
  422. {
  423. $xDiff = $v->x - $this->x;
  424. $yDiff = $v->y - $this->y;
  425. $zDiff = $v->z - $this->z;
  426. if (($zDiff * $zDiff) < 0.0000001) {
  427. return null;
  428. }
  429. $f = ($z - $this->z) / $zDiff;
  430. if ($f < 0 or $f > 1) {
  431. return null;
  432. } else {
  433. return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f);
  434. }
  435. }
  436. /**
  437. *
  438. * @param unknown $x
  439. * @param unknown $y
  440. * @param unknown $z
  441. * @return Vector3
  442. */
  443. public function setComponents($x, $y, $z)
  444. {
  445. $this->x = $x;
  446. $this->y = $y;
  447. $this->z = $z;
  448. return $this;
  449. }
  450. /**
  451. *
  452. * @return unknown
  453. */
  454. public function __toString()
  455. {
  456. return "Vector3(x=" . $this->x . ",y=" . $this->y . ",z=" . $this->z . ")";
  457. }
  458. }