Vector2.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /************************************************************************
  2. * Vector2.js
  3. ************************************************************************
  4. * Copyright (c) 2021 Pedro Tonini Rosenberg Schneider.
  5. *
  6. * This file is part of Pandora.
  7. *
  8. * Pandora is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Pandora is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Pandora. If not, see <https://www.gnu.org/licenses/>.
  20. *************************************************************************/
  21. /**
  22. * This {@code Vector2} class provides an interface to store a 2D vector as a component to
  23. * any GameObject.
  24. *
  25. * @author Pedro Schneider
  26. *
  27. * @class
  28. */
  29. class Vector2 extends Component
  30. {
  31. /**
  32. * Creates a Vector2 with length 0.
  33. *
  34. * @returns {Vector2} length 0 Vector2.
  35. */
  36. static ZERO()
  37. {
  38. return new Vector2(0, 0);
  39. }
  40. /**
  41. * Creates a Vector2 with both dimetions equal to 1.
  42. *
  43. * @returns {Vector2} Vector2 with both dimentions equal to 1.
  44. */
  45. static ONE()
  46. {
  47. return new Vector2(1, 1);
  48. }
  49. /**
  50. * Creates a Vector2 pointing right.
  51. *
  52. * @returns {Vector2} Vector2 pointing right.
  53. */
  54. static RIGHT()
  55. {
  56. return new Vector2(1, 0);
  57. }
  58. /**
  59. * Creates a Vector2 pointing left.
  60. *
  61. * @returns {Vector2} Vector2 pointing left.
  62. */
  63. static LEFT()
  64. {
  65. return new Vector2(-1, 0);
  66. }
  67. /**
  68. * Creates a Vector2 pointing up.
  69. *
  70. * @returns {Vector2} Vector2 pointing up.
  71. */
  72. static UP()
  73. {
  74. return new Vector2(0, -1);
  75. }
  76. /**
  77. * Creates a Vector2 pointing down.
  78. *
  79. * @returns {Vector2} Vector2 pointing down.
  80. */
  81. static DOWN()
  82. {
  83. return new Vector2(0, 1);
  84. }
  85. /**
  86. * Initializes a Vector2 with the given dimentions.
  87. * @param {number} x
  88. * @param {number} y
  89. *
  90. * @constructor
  91. */
  92. constructor(x, y)
  93. {
  94. super();
  95. this.x = x;
  96. this.y = y;
  97. }
  98. /**
  99. * Creates a Vector2 with de dimentions equal to the absolute value of
  100. * this Vector2's dimentions.
  101. *
  102. * @returns {Vector2} created Vector2.
  103. */
  104. abs()
  105. {
  106. return new Vector2(abs(this.x), abs(this.y));
  107. }
  108. /**
  109. * Returns this Vector2's angle in radians with respect to the positive X axis,
  110. * or the (1, 0) vector.
  111. *
  112. * @returns {number} angle in radian between this Vector2 and the positive X axis.
  113. */
  114. angle()
  115. {
  116. return atan2(this.y, this.x);
  117. }
  118. /**
  119. * Returns this Vector2's length squared.
  120. *
  121. * @returns {number} this Vector2's length squared.
  122. */
  123. lengthSquared()
  124. {
  125. return this.x * this.x + this.y * this.y;
  126. }
  127. /**
  128. * Returns this Vector2's length.
  129. *
  130. * @returns {number} this Vector2's length.
  131. */
  132. length()
  133. {
  134. return sqrt(this.lengthSquared());
  135. }
  136. /**
  137. * Creates a Vector2 with length 1 pointing in the same direction of this Vector2.
  138. *
  139. * @returns {Vector2} normalized vector.
  140. */
  141. normalized()
  142. {
  143. let len = this.length();
  144. return new Vector2(this.x / len, this.y / len);
  145. }
  146. /**
  147. * Normalizes this vector inplace.
  148. */
  149. normalize()
  150. {
  151. let len = this.length();
  152. this.x /= len;
  153. this.y /= len;
  154. }
  155. /**
  156. * Creates a Vector2 rotated a radians clockwise in relation to this Vector2.
  157. *
  158. * @param {number} a angle in radians to rotate the vector.
  159. *
  160. * @returns {Vector2} rotated Vector2.
  161. */
  162. rotated(a)
  163. {
  164. return new Vector2(this.x * cos(-a) - this.y * sin(-a), this.x * sin(-a) + this.y * cos(-a))
  165. }
  166. /**
  167. * Rotates this Vector2 inplace by a radians clockwise.
  168. *
  169. * @param {number} a angle in radians to rotate this vector.
  170. */
  171. rotate(a)
  172. {
  173. let rotX = this.x * cos(-a) - this.y * sin(-a),
  174. rotY = this.x * sin(-a) + this.y * cos(-a);
  175. this.x = rotX;
  176. this.y = rotY;
  177. }
  178. /**
  179. * Creates a Vector2 based on this Vector2 but scaled by s.
  180. *
  181. * @param {number} s factor to scale the Vector2.
  182. *
  183. * @returns {Vector2} scaled Vector2.
  184. */
  185. scaled(s)
  186. {
  187. return new Vector2(this.x * s, this.y * s);
  188. }
  189. /**
  190. * Scales this Vector2 inplace.
  191. *
  192. * @param {number} s factor to scale the Vector2.
  193. */
  194. scale(s)
  195. {
  196. this.x *= s;
  197. this.y *= s;
  198. }
  199. /**
  200. * Creates a Vector2 based on this Vector2 but translated by x on the X-axis and y on the Y-axis.
  201. *
  202. * @param {number} x X-axis translation.
  203. * @param {number} y Y-axis translation.
  204. *
  205. * @returns {Vector2} translated Vector2.
  206. */
  207. translated(x, y)
  208. {
  209. return new Vector2(this.x + x, this.y + y);
  210. }
  211. /**
  212. * Translates this Vector2 by x on the X-axis and y on the Y-axis.
  213. *
  214. * @param {number} x X-axis translation.
  215. * @param {number} y Y-axis translation.
  216. */
  217. translate(x, y)
  218. {
  219. this.x += x;
  220. this.y += y;
  221. }
  222. /**
  223. * Returns this Vector2's distance squared to another Vector2.
  224. *
  225. * @param {Vector2} v Vector2 to calculate the distance to.
  226. *
  227. * @returns {number} distance squared between this Vector2 and the provided
  228. * Vector2.
  229. */
  230. distanceSquaredTo(v)
  231. {
  232. return new Vector2(v.x - this.x, v.y - this.y).length();
  233. }
  234. /**
  235. * Returns this Vector2's distance to another Vector2.
  236. *
  237. * @param {Vector2} v Vector2 to calculate the distance to.
  238. *
  239. * @returns {number} distance between this Vector2 and the provided
  240. * Vector2.
  241. */
  242. distance(v)
  243. {
  244. return sqrt(this.distanceSquaredTo(v));
  245. }
  246. }