Collisions.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. var ra = radians(a)
  46. if (a % 360 != 0)
  47. {
  48. rotx = x * cos(-ra) - y * sin(-ra);
  49. roty = x * sin(-ra) + y * cos(-ra);
  50. }
  51. return Object.freeze([rotx, roty]);
  52. },
  53. /**
  54. * Namespace to separate rectangle collision detection.
  55. *
  56. * @namespace
  57. */
  58. Rect:
  59. {
  60. /**
  61. * Calculates if a point is inside a rect.
  62. *
  63. * @param {number} posx global X-axis position of the rect.
  64. * @param {number} posy global Y-axis position of the rect.
  65. * @param {number} rot global rotation in degrees of the rect.
  66. * @param {number} sx global X-axis scale of the rect.
  67. * @param {number} sy global Y-axis scale of the rect.
  68. * @param {Rect} rect Shape component with the rect data.
  69. * @param {number} x X-axis position of the point.
  70. * @param {number} y Y-axis position of the point.
  71. *
  72. * @returns {boolean} true if the point is inside the rect, false otherwise.
  73. */
  74. point(posx, posy, rot, sx, sy, rect, x, y)
  75. {
  76. x -= posx;
  77. y -= posy;
  78. var [rotx, roty] = Collisions.rotate(x, y, rot);
  79. return abs(rotx) <= rect.w / 2 * sx && abs(roty) <= rect.h / 2 * sy;
  80. },
  81. },
  82. /**
  83. * Namespace to separate ellipse collision detection.
  84. *
  85. * @namespace
  86. */
  87. Ellipse:
  88. {
  89. /**
  90. * Calculates if a point is inside a ellipse.
  91. *
  92. * @param {number} posx global X-axis position of the ellipse.
  93. * @param {number} posy global Y-axis position of the ellipse.
  94. * @param {number} rot global rotation in degrees of the ellipse.
  95. * @param {number} sx global X-axis scale of the ellipse.
  96. * @param {number} sy global Y-axis scale of the ellipse.
  97. * @param {Ellipse} e Shape component with the ellipse data.
  98. * @param {number} x X-axis position of the point.
  99. * @param {number} y Y-axis position of the point.
  100. *
  101. * @returns {boolean} true if the point is inside the ellipse, false otherwise.
  102. */
  103. point(posx, posy, rot, sx, sy, e, x, y)
  104. {
  105. x -= posx;
  106. y -= posy;
  107. var [rotx, roty] = Collisions.rotate(x, y, rot);
  108. var srx = e.rx * sx, sry = e.ry * sy;
  109. return (rotx * rotx) * (sry * sry) + (roty * roty) * (srx * srx) <= (srx * srx) * (sry * sry);
  110. }
  111. }
  112. }