Vector2.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * Returns this Vector2's distance squared to another Vector2.
  148. *
  149. * @param {Vector2} v Vector2 to calculate the distance to.
  150. *
  151. * @returns {number} distance squared between this Vector2 and the provided
  152. * Vector2.
  153. */
  154. distanceSquaredTo(v)
  155. {
  156. return new Vector2(v.x - this.x, v.y - this.y).length();
  157. }
  158. /**
  159. * Returns this Vector2's distance to another Vector2.
  160. *
  161. * @param {Vector2} v Vector2 to calculate the distance to.
  162. *
  163. * @returns {number} distance between this Vector2 and the provided
  164. * Vector2.
  165. */
  166. distance(v)
  167. {
  168. return sqrt(this.distanceSquaredTo(v));
  169. }
  170. }