47ffdbe9496b14c1b1c3a26ce79f8df526beea9b.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, ProjectileMathUtil, _crd, rad2Deg, deg2Rad;
  4. _export("default", void 0);
  5. return {
  6. setters: [function (_cc) {
  7. _cclegacy = _cc.cclegacy;
  8. }],
  9. execute: function () {
  10. _crd = true;
  11. _cclegacy._RF.push({}, "16ac3tSOM5FwpZe8CFKhU2G", "ProjectileMathUtil", undefined);
  12. /** 用于弧度转角度 */
  13. rad2Deg = 180 / Math.PI;
  14. /** 用于角度转弧度 */
  15. deg2Rad = Math.PI / 180;
  16. /**
  17. * 抛射运动的数学工具
  18. */
  19. _export("default", ProjectileMathUtil = class ProjectileMathUtil {
  20. /**
  21. * 计算耗时
  22. * @param x 水平位移
  23. * @param angle 初始角度
  24. * @param velocity 初始速度
  25. */
  26. static calculateTotalTime(x, angle, velocity) {
  27. // 初始角度(弧度制)
  28. const θ = angle * deg2Rad; // 时间
  29. // t = x / ( v * cos(θ) )
  30. const t = x / (velocity * Math.cos(θ));
  31. return t;
  32. }
  33. /**
  34. * 计算指定时刻的运动角度
  35. * @param angle 初始角度
  36. * @param velocity 初始速度
  37. * @param time 时间
  38. * @param gravity 重力加速度
  39. * @param returnInRadians 是否返回弧度制结果
  40. */
  41. static calculateAngleAtMoment(angle, velocity, time, gravity, returnInRadians = false) {
  42. // 重力加速度(垂直向下)
  43. const g = gravity; //
  44. // 初始角度(弧度制)
  45. const θ = angle * deg2Rad; // 水平瞬时速度
  46. // vx = v * cos(θ)
  47. const vx = velocity * Math.cos(θ); // 垂直瞬时速度
  48. // vy = v * sin(θ) - g * t
  49. const vy = velocity * Math.sin(θ) - g * time; // 该时刻的运动角度(弧度制)
  50. const θt = Math.atan(vy / vx);
  51. return returnInRadians ? θt : θt * rad2Deg;
  52. }
  53. /**
  54. * 计算指定时刻的位移距离
  55. * @param angle 初始角度
  56. * @param velocity 初始速度
  57. * @param gravity 重力加速度
  58. * @param time 时间点
  59. */
  60. static calculateDisplacementAtMoment(angle, velocity, gravity, time) {
  61. // 重力加速度(垂直向下)
  62. const g = gravity; // 初始角度(弧度制)
  63. const θ = angle * deg2Rad; // 水平位移
  64. // x = v * cos(θ) * t
  65. const x = velocity * Math.cos(θ) * time; // 垂直位移
  66. // y = v * sin(θ) * t - 0.5 * g * t^2
  67. const y = velocity * Math.sin(θ) * time - 0.5 * g * Math.pow(time, 2);
  68. return {
  69. x,
  70. y
  71. };
  72. }
  73. /**
  74. * 根据初始角度计算初始速度
  75. * @param x 水平距离
  76. * @param y 垂直距离
  77. * @param gravity 重力加速度
  78. * @param angle 初始角度(角度制)
  79. */
  80. static calculateWithAngle(x, y, gravity, angle) {
  81. // 重力加速度(垂直向下)
  82. const g = Math.abs(gravity); // 初始角度(弧度制)
  83. const θ = angle * deg2Rad; // 速度公式
  84. // v = sqrt( ( x^2 * g ) / ( 2 * x * sin(θ) * cos(θ) - 2 * y * cos(θ)^2 ) )
  85. // 部分计算结果
  86. const p1 = 2 * x * Math.sin(θ) * Math.cos(θ) - 2 * y * Math.pow(Math.cos(θ), 2); // 负数没有平方根
  87. if (p1 < 0) {
  88. return NaN;
  89. } // 速度
  90. const v = Math.sqrt(g * Math.pow(x, 2) / p1);
  91. return v;
  92. }
  93. /**
  94. * 根据初始速度计算初始角度
  95. * @param x 水平距离
  96. * @param y 垂直距离
  97. * @param gravity 重力加速度
  98. * @param velocity 初始速度
  99. */
  100. static calculateWithVelocity(x, y, velocity, gravity) {
  101. // 重力加速度(垂直向下)
  102. const g = gravity; // 初始速度
  103. const v = velocity; // 角度公式
  104. // θ = atan( ( -v^2 ± sqrt( v^4 - g * ( g * x^2 + 2 * y * v^2 ) ) / ( -g * x ) ) )
  105. // 部分计算结果
  106. const p1 = Math.pow(v, 2);
  107. const p2 = Math.pow(v, 4) - g * (g * Math.pow(x, 2) + 2 * y * p1); // 负数没有平方根
  108. if (p2 < 0) {
  109. return {
  110. angle1: NaN,
  111. angle2: NaN
  112. };
  113. } // 部分计算结果
  114. const p3 = Math.sqrt(p2); // 角度(两个解)
  115. const θ1 = Math.atan((-p1 + p3) / (-g * x));
  116. const θ2 = Math.atan((-p1 - p3) / (-g * x));
  117. return {
  118. angle1: θ1 * rad2Deg,
  119. angle2: θ2 * rad2Deg
  120. };
  121. }
  122. /**
  123. * 根据最大高度计算速度和角度
  124. * @param x 水平距离
  125. * @param y 垂直距离
  126. * @param gravity 重力加速度
  127. * @param maxHeight 最大高度
  128. */
  129. static calculateWithMaxHeight(x, y, maxHeight, gravity) {
  130. // 重力加速度(垂直向下)
  131. const g = gravity; // 最大高度
  132. const h = maxHeight; // 最大高度不能小于 0,也不能够小于垂直距离
  133. if (h < 0 || h - y < 0) {
  134. return {
  135. angle: NaN,
  136. velocity: NaN,
  137. time: NaN
  138. };
  139. } // 部分计算结果
  140. const p1 = Math.sqrt(2 * g * h);
  141. const p2 = Math.sqrt(2 * g * (h - y)); // 时间公式
  142. // t = ( -sqrt( 2 * g * h ) ± sqrt( 2 * g * ( h - y ) ) ) / -g
  143. const t1 = (-p1 + p2) / -g;
  144. const t2 = (-p1 - p2) / -g; // 始终使用较大的解
  145. const t = Math.max(t1, t2); // 角度公式
  146. // θ = atan( ( sqrt( 2 * g * h ) * t ) / x )
  147. const θ = Math.atan(p1 * t / x); // 速度公式
  148. // v = sqrt( 2 * g * h ) / sin(θ)
  149. const v = p1 / Math.sin(θ);
  150. return {
  151. angle: θ * rad2Deg,
  152. velocity: v,
  153. time: t
  154. };
  155. }
  156. });
  157. _cclegacy._RF.pop();
  158. _crd = false;
  159. }
  160. };
  161. });
  162. //# sourceMappingURL=47ffdbe9496b14c1b1c3a26ce79f8df526beea9b.js.map