angle.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * ===========================================================================
  3. *
  4. * Wolf3D Browser Version GPL Source Code
  5. * Copyright (C) 2012 id Software LLC, a ZeniMax Media company.
  6. *
  7. * This file is part of the Wolf3D Browser Version GPL Source Code ("Wolf3D Browser Source Code").
  8. *
  9. * Wolf3D Browser Source Code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * Wolf3D Browser Source Code is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License version 2
  20. * along with Wolf3D Browser Source Code. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * If you have questions concerning this license, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  23. *
  24. * ===========================================================================
  25. */
  26. /**
  27. * @namespace
  28. * @description Angle math
  29. */
  30. Wolf.Angle = (function() {
  31. Wolf.setConsts({
  32. DEG2RAD : function(a) { return a * 0.01745329251994329576; }, // a * M_PI / 180.0f
  33. RAD2DEG : function(a) { return a / 0.01745329251994329576; }, // a * 180.0f / M_PI
  34. ANGLE2SHORT : function(x) { return ((x * 65536 / 360)>>0) & 65535; },
  35. SHORT2ANGLE : function(x) { return x * 360.0 / 65536; }
  36. });
  37. /**
  38. * @description Finds the difference between two angles.
  39. * @memberOf Wolf.Angle
  40. * @param {number} angle1 Angle in radians.
  41. * @param {number} angle2 Angle in radians.
  42. * @returns {number} The absolute difference between two angles, this will always be between 0 and 180 degrees.
  43. */
  44. function diff(angle1, angle2) {
  45. var d;
  46. if (angle1 > angle2) {
  47. d = angle1 - angle2;
  48. } else {
  49. d = angle2 - angle1;
  50. }
  51. if (d > Math.PI) {
  52. return 2 * Math.PI - d;
  53. } else {
  54. return d;
  55. }
  56. }
  57. /**
  58. * @description Clockwise distance between two angles.
  59. * @memberOf Wolf.Angle
  60. * @param {number} angle1 Angle in radians.
  61. * @param {number} angle2 Angle in radians.
  62. * @returns {number} The clockwise distance from angle2 to angle1, this may be greater than 180 degrees.
  63. */
  64. function distCW(angle1, angle2) {
  65. if (angle1 > angle2) {
  66. return angle1 - angle2;
  67. } else {
  68. return angle1 + 2 * Math.PI - angle2;
  69. }
  70. }
  71. /**
  72. * @description Linear interpolate between angle from and to by fraction frac.
  73. * @memberOf Wolf.Angle
  74. * @param {number} from Angle in radians.
  75. * @param {number} to Angle in radians.
  76. * @param {number} frac Fraction.
  77. * @returns {number}
  78. */
  79. function interpolate(from, to, frac) {
  80. var d = diff(from, to) * frac;
  81. if (distCW(to, from) >= Math.PI) {
  82. return from - diff;
  83. } else {
  84. return from + diff;
  85. }
  86. }
  87. /**
  88. * @description Normalize angle.
  89. * @memberOf Wolf.Angle
  90. * @param {number} angle
  91. * @returns {number}
  92. */
  93. function normalize(angle) {
  94. while (angle < 0) {
  95. angle += (2 * Math.PI);
  96. }
  97. while (angle >= (2 * Math.PI)) {
  98. angle -= (2 * Math.PI);
  99. }
  100. return angle;
  101. }
  102. /**
  103. * @description Linear interpolate allowing for the Modulo 360 problem.
  104. * @memberOf Wolf.Angle
  105. * @param {number} from Angle in radians.
  106. * @param {number} to Angle in radians.
  107. * @param {number} frac fraction.
  108. * @returns {number}
  109. */
  110. function lerp(from, to, frac) {
  111. if (to - from > 180) {
  112. to -= 360;
  113. }
  114. if (to - from < -180) {
  115. to += 360;
  116. }
  117. return from + frac * (to - from);
  118. }
  119. return {
  120. diff : diff,
  121. distCW : distCW,
  122. normalize : normalize,
  123. interpolate : interpolate,
  124. lerp : lerp
  125. }
  126. })();