Collisions.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /************************************************************************
  2. * Collisions.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 Collisions} singleton provides an interface for the engine to calculate collisions
  23. * with various differente shapes. They are used in the calculations of the Area2D GameObject,
  24. * to check collisions with the mouse. They can be utilized by the user if it fits the need.
  25. *
  26. * @author Pedro Schneider
  27. *
  28. * @namespace
  29. */
  30. const Collisions = {
  31. /**
  32. * Rotates the (x, y) vector by a degrees clockwise, around the origin.
  33. *
  34. * @param {number} x X coordinate of the vector to rotate.
  35. * @param {number} y Y coordinate of the vector to rotate.
  36. * @param {number} a angle in degrees to rotate the vector.
  37. *
  38. * @returns {Array} static array containing the tow coordinates of the rotated vector.
  39. * Can be deconstructed.
  40. */
  41. rotate(x, y, a)
  42. {
  43. var rotx = x,
  44. roty = y;
  45. if (a % 360 != 0)
  46. {
  47. rotx = x * cos(-radians(a)) - y * sin(-radians(a));
  48. roty = x * sin(-radians(a)) + y * cos(-radians(a));
  49. }
  50. return Object.freeze([rotx, roty]);
  51. },
  52. /**
  53. * Namespace to separate rectangle collision detection.
  54. *
  55. * @namespace
  56. */
  57. Rect:
  58. {
  59. /**
  60. * Calculates if a point is inside a rect.
  61. *
  62. * @param {number} posx global X-axis position of the rect.
  63. * @param {number} posy global Y-axis position of the rect.
  64. * @param {number} rot global rotation in degrees of the rect.
  65. * @param {number} sx global X-axis scale of the rect.
  66. * @param {number} sy global Y-axis scale of the rect.
  67. * @param {Rect} rect Shape component with the rect data.
  68. * @param {number} x X-axis position of the point.
  69. * @param {number} y Y-axis position of the point.
  70. *
  71. * @returns {boolean} true if the point is inside the rect, false otherwise.
  72. */
  73. point(posx, posy, rot, sx, sy, rect, x, y)
  74. {
  75. x -= posx;
  76. y -= posy;
  77. var [rotx, roty] = Collisions.rotate(x, y, rot);
  78. return abs(rotx) <= rect.w / 2 * sx && abs(roty) <= rect.h / 2 * sy;
  79. },
  80. },
  81. /**
  82. * Namespace to separate ellipse collision detection.
  83. *
  84. * @namespace
  85. */
  86. Ellipse:
  87. {
  88. /**
  89. * Calculates if a point is inside a ellipse.
  90. *
  91. * @param {number} posx global X-axis position of the ellipse.
  92. * @param {number} posy global Y-axis position of the ellipse.
  93. * @param {number} rot global rotation in degrees of the ellipse.
  94. * @param {number} sx global X-axis scale of the ellipse.
  95. * @param {number} sy global Y-axis scale of the ellipse.
  96. * @param {Ellipse} e Shape component with the ellipse data.
  97. * @param {number} x X-axis position of the point.
  98. * @param {number} y Y-axis position of the point.
  99. *
  100. * @returns {boolean} true if the point is inside the ellipse, false otherwise.
  101. */
  102. point(posx, posy, rot, sx, sy, e, x, y)
  103. {
  104. x -= posx;
  105. y -= posy;
  106. var [rotx, roty] = Collisions.rotate(x, y, rot);
  107. var srx = e.rx * sx, sry = e.ry * sy;
  108. return (rotx * rotx) * (sry * sry) + (roty * roty) * (srx * srx) <= (srx * srx) * (sry * sry);
  109. }
  110. }
  111. }