Vector2.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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
  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. this.x = x;
  95. this.y = y;
  96. }
  97. /**
  98. * Creates a Vector2 with de dimentions equal to the absolute value of
  99. * this Vector2's dimentions.
  100. *
  101. * @returns {Vector2} created Vector2.
  102. */
  103. abs()
  104. {
  105. return new Vector2(abs(this.x), abs(this.y));
  106. }
  107. /**
  108. * Returns this Vector2's angle in radians with respect to the positive X axis,
  109. * or the (1, 0) vector.
  110. *
  111. * @returns {number} angle in radian between this Vector2 and the positive X axis.
  112. */
  113. angle()
  114. {
  115. return atan2(this.y, this.x);
  116. }
  117. /**
  118. * Returns this Vector2's length squared.
  119. *
  120. * @returns {number} this Vector2's length squared.
  121. */
  122. lengthSquared()
  123. {
  124. return this.x * this.x + this.y * this.y;
  125. }
  126. /**
  127. * Returns this Vector2's length.
  128. *
  129. * @returns {number} this Vector2's length.
  130. */
  131. length()
  132. {
  133. return sqrt(this.lengthSquared());
  134. }
  135. /**
  136. * Creates a Vector2 with length 1 pointing in the same direction of this Vector2.
  137. *
  138. * @returns {Vector2} normalized vector.
  139. */
  140. normalized()
  141. {
  142. let len = this.length();
  143. return new Vector2(this.x / len, this.y / len);
  144. }
  145. /**
  146. * Returns this Vector2's distance squared to another Vector2.
  147. *
  148. * @param {Vector2} v Vector2 to calculate the distance to.
  149. *
  150. * @returns {number} distance squared between this Vector2 and the provided
  151. * Vector2.
  152. */
  153. distanceSquaredTo(v)
  154. {
  155. return new Vector2(v.x - this.x, v.y - this.y).length();
  156. }
  157. /**
  158. * Returns this Vector2's distance to another Vector2.
  159. *
  160. * @param {Vector2} v Vector2 to calculate the distance to.
  161. *
  162. * @returns {number} distance between this Vector2 and the provided
  163. * Vector2.
  164. */
  165. distance(v)
  166. {
  167. return sqrt(this.distanceSquaredTo(v));
  168. }
  169. }