2d836b3dc11c70cd1a8621bc539f2b1ca2080115.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, misc, Vec2, Vec3, MathUtil, _crd, tempVec3, rad2Deg, deg2Rad;
  4. _export("MathUtil", void 0);
  5. return {
  6. setters: [function (_cc) {
  7. _cclegacy = _cc.cclegacy;
  8. __checkObsolete__ = _cc.__checkObsolete__;
  9. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  10. misc = _cc.misc;
  11. Vec2 = _cc.Vec2;
  12. Vec3 = _cc.Vec3;
  13. }],
  14. execute: function () {
  15. _crd = true;
  16. _cclegacy._RF.push({}, "5df23qNkJ5Jco4b72bZLJJf", "MathUtil", undefined);
  17. /** 用于计算的临时工 */
  18. __checkObsolete__(['misc', 'Rect', 'Vec2', 'Vec3']);
  19. tempVec3 = new Vec3();
  20. /** 用于弧度转角度 */
  21. rad2Deg = 180 / Math.PI;
  22. /** 用于角度转弧度 */
  23. deg2Rad = Math.PI / 180;
  24. _export("MathUtil", MathUtil = class MathUtil {
  25. /**
  26. * 用于弧度转角度
  27. */
  28. static get rad2Deg() {
  29. return rad2Deg;
  30. }
  31. /**
  32. * 用于角度转弧度
  33. */
  34. static get deg2Rad() {
  35. return deg2Rad;
  36. }
  37. /**
  38. * 弧度转角度
  39. * @param radians
  40. */
  41. static radiansToDegrees(radians) {
  42. return radians * rad2Deg;
  43. }
  44. /**
  45. * 角度转弧度
  46. * @param degree
  47. */
  48. static degreesToRadians(degree) {
  49. return degree * deg2Rad;
  50. }
  51. /**限制数大小 */
  52. static Clamp(value, min, max) {
  53. if (value < min) {
  54. value = min;
  55. } else if (value > max) {
  56. value = max;
  57. }
  58. return value;
  59. }
  60. static Clamp01(value) {
  61. return this.Clamp(value, 0, 1);
  62. }
  63. static PingPong(t, length) {
  64. t = this.Repeat(t, length * 2);
  65. return length - Math.abs(t - length);
  66. }
  67. static Repeat(t, length) {
  68. return t - Math.floor(t / length) * length;
  69. }
  70. static Round(num) {
  71. return Math.floor(num + 0.5);
  72. }
  73. static Sign(num) {
  74. if (num > 0) {
  75. num = 1;
  76. } else if (num < 0) {
  77. num = -1;
  78. } else {
  79. num = 0;
  80. }
  81. return num;
  82. }
  83. static InverseLerp(from, to, value) {
  84. if (from < to) {
  85. if (value < from) return 0;
  86. if (value > to) return 1;
  87. value = value - from;
  88. value = value / (to - from);
  89. return value;
  90. }
  91. if (from <= to) return 0;
  92. if (value < to) return 1;
  93. if (value > from) return 0;
  94. return 1 - (value - to) / (from - to);
  95. }
  96. static Lerp(from, to, t) {
  97. return from + (to - from) * this.Clamp01(t);
  98. }
  99. static LerpUnclamped(a, b, t) {
  100. return a + (b - a) * t;
  101. }
  102. static DeltaAngle(current, target) {
  103. var num = this.Repeat(target - current, 360);
  104. if (num > 180) num = num - 360;
  105. return num;
  106. }
  107. static LerpAngle(a, b, t) {
  108. var num = this.Repeat(b - a, 360);
  109. if (num > 180) num = num - 360;
  110. return a + num * this.Clamp01(t);
  111. }
  112. /**
  113. * 在最小值和最大值之间进行插值,并在极限处进行平滑处理
  114. * @param from
  115. * @param to
  116. * @param t
  117. */
  118. static smoothStep(from, to, t) {
  119. t = this.Clamp01(t);
  120. t = -2.0 * t * t * t + 3.0 * t * t;
  121. return to * t + from * (1.0 - t);
  122. }
  123. /**
  124. * 平滑控制
  125. * @param current 当前值
  126. * @param target 目标值
  127. * @param currentVelocity 当前速度
  128. * @param smoothTime 平滑时间
  129. * @param maxSpeed 最大速度
  130. * @param deltaTime 时间增量
  131. */
  132. static smoothDamp(current, target, currentVelocity, smoothTime, deltaTime, maxSpeed) {
  133. if (maxSpeed === void 0) {
  134. maxSpeed = Number.POSITIVE_INFINITY;
  135. }
  136. smoothTime = Math.max(0.0001, smoothTime);
  137. var num1 = 2 / smoothTime;
  138. var num2 = num1 * deltaTime;
  139. var num3 = 1 / (1 + num2 + 0.47999998927116394 * num2 * num2 + 0.23499999940395355 * num2 * num2 * num2);
  140. var num4 = current - target;
  141. var num5 = target;
  142. var max = maxSpeed * smoothTime;
  143. var num6 = this.Clamp(num4, -max, max);
  144. target = current - num6;
  145. var num7 = (currentVelocity + num1 * num6) * deltaTime;
  146. var velocity = (currentVelocity - num1 * num7) * num3;
  147. var num8 = target + (num6 + num7) * num3;
  148. if (num5 - current > 0 === num8 > num5) {
  149. num8 = num5;
  150. velocity = (num8 - num5) / deltaTime;
  151. }
  152. return {
  153. value: num8,
  154. velocity: velocity
  155. };
  156. }
  157. /**
  158. * 垂直于原向量V的单位向量
  159. * @param v 一个向量
  160. * @returns
  161. */
  162. static getPerpendicular(v) {
  163. var perpendicular = new Vec2(-v.y, v.x); //计算垂直向量
  164. var normal = perpendicular.normalize(); //归一化
  165. return normal;
  166. } //角度转向量
  167. static angleToVector(angle) {
  168. return this.zore.clone().rotate(-misc.degreesToRadians(angle));
  169. } // 向量转角度
  170. static vectorToAngle(dir) {
  171. return -misc.radiansToDegrees(dir.signAngle(this.zore));
  172. } // 角度转向量
  173. static angle_to_vector(angle) {
  174. // tan = sin / cos
  175. // 将传入的角度转为弧度
  176. var radian = this.degreesToRadians(angle); // 算出cos,sin和tan
  177. var cos = Math.cos(radian); // 邻边 / 斜边
  178. var sin = Math.sin(radian); // 对边 / 斜边
  179. var tan = sin / cos; // 对边 / 邻边
  180. // 结合在一起并归一化
  181. var vec = new Vec2(cos, sin).normalize(); // 返回向量
  182. return vec;
  183. } // !!!!!!!!其实使用Math.atan2求出弧度再转角度一样的效果
  184. // 向量转角度
  185. static vector_to_angle(dir) {
  186. // 将传入的向量归一化 一般已经归一化了
  187. //dir.normalize();
  188. // 计算出目标角度的弧度
  189. var radian = dir.signAngle(this.zore); // 把弧度计算成角度
  190. var angle = -this.radiansToDegrees(radian); // 返回角度
  191. return angle;
  192. } //向量转弧度
  193. static vector_to_radian(dir) {
  194. return dir.signAngle(this.zore);
  195. } /// <summary>
  196. /// 旋转向量,使其方向改变,大小不变
  197. /// </summary>
  198. /// <param name="v">需要旋转的向量</param>
  199. /// <param name="angle">旋转的角度</param>
  200. /// <returns>旋转后的向量</returns>
  201. static RotationMatrix(x, y, angle) {
  202. var radian = this.degreesToRadians(angle);
  203. var sin = Math.sin(radian);
  204. var cos = Math.cos(radian);
  205. var newX = x * cos + y * sin;
  206. var newY = x * -sin + y * cos; //var newX = x * cos - y * sin;
  207. //var newY = x * sin + y * cos;
  208. return new Vec2(newX, newY);
  209. }
  210. static RotationDir(dir, angle) {
  211. var radian = this.radiansToDegrees(angle);
  212. var sin = Math.sin(radian);
  213. var cos = Math.cos(radian);
  214. var newX = dir.x * cos + dir.y * sin;
  215. var newY = dir.x * -sin + dir.y * cos;
  216. dir.x = newX;
  217. dir.y = newY;
  218. return dir;
  219. } //扇形范围
  220. static CheckInView(mPos, faceDir, tPos, dis, angle) {
  221. //let t = tPos.subtract(mPos);
  222. //let distance = Vec2.lengthSqr(t);
  223. //if(distance<dis*dis)
  224. var distance = Vec2.distance(mPos, tPos);
  225. if (distance < dis) {
  226. if (angle >= 360) return true;
  227. var dir = tPos.subtract(mPos).normalize();
  228. var ang = this.radiansToDegrees(Vec2.angle(faceDir, dir));
  229. if (ang <= angle * 0.5) {
  230. return true;
  231. }
  232. }
  233. return false;
  234. } //检测点是否在一个凸四边形内
  235. static InConvexQuad(point, pointA, pointB, pointC, pointD) {
  236. var vec1, vec2;
  237. vec1.x = pointB.x - pointA.x;
  238. vec1.y = pointB.y - pointA.y;
  239. vec2.x = point.x - pointA.x;
  240. vec2.y = point.y - pointA.y;
  241. if (this.cross2DPoint(vec2.x, vec2.y, vec1.x, vec1.y)) return false;
  242. vec1.x = pointC.x - pointB.x;
  243. vec1.y = pointC.y - pointB.y;
  244. vec2.x = point.x - pointB.x;
  245. vec2.y = point.y - pointB.y;
  246. if (this.cross2DPoint(vec2.x, vec2.y, vec1.x, vec1.y)) return false;
  247. vec1.x = pointD.x - pointC.x;
  248. vec1.y = pointD.y - pointC.y;
  249. vec2.x = point.x - pointC.x;
  250. vec2.y = point.y - pointC.y;
  251. if (this.cross2DPoint(vec2.x, vec2.y, vec1.x, vec1.y)) return false;
  252. vec1.x = pointA.x - pointD.x;
  253. vec1.y = pointA.y - pointD.y;
  254. vec2.x = point.x - pointD.x;
  255. vec2.y = point.y - pointD.y;
  256. if (this.cross2DPoint(vec2.x, vec2.y, vec1.x, vec1.y)) return false;
  257. return true;
  258. }
  259. /**等同于Vec2.cross */
  260. static cross2DPoint(x1, y1, x2, y2) {
  261. return x1 * y2 - x2 * y1;
  262. }
  263. /**检测两个2D包围盒(xy为中心点 wh分别为x轴半径宽 y轴半径宽) 是否相交*/
  264. static AABBAABB2D(pointX1, pointY1, w1, h1, pointX2, pointY2, w2, h2) {
  265. if (Math.abs(pointX1 - pointX2) > w1 + w2) return false;
  266. if (Math.abs(pointY1 - pointY2) > h1 + h2) return false;
  267. return true;
  268. }
  269. /**检测两个3D包围盒 是否相交*/
  270. static AABBAABB3D(pointX1, pointY1, pointZ1, w1, h1, t1, pointX2, pointY2, pointZ2, w2, h2, t2) {
  271. if (Math.abs(pointX1 - pointX2) > w1 + w2) return false;
  272. if (Math.abs(pointY1 - pointY2) > h1 + h2) return false;
  273. if (Math.abs(pointZ1 - pointZ2) > t1 + t2) return false;
  274. return true;
  275. }
  276. /**点与点*/
  277. static pointPoint(point1, point2) {
  278. if (point1.x == point2.x && point1.y == point2.y) {
  279. return true;
  280. }
  281. return false;
  282. } //判断点是否在线上,在返回1,不在返回0
  283. static onSegement(Q, p1, p2) {
  284. var maxx, minx, maxy, miny;
  285. maxx = p1.x > p2.x ? p1.x : p2.x; //矩形的右边长
  286. minx = p1.x > p2.x ? p2.x : p1.x; //矩形的左边长
  287. maxy = p1.y > p2.y ? p1.y : p2.y; //矩形的上边长
  288. miny = p1.y > p2.y ? p2.y : p1.y; //矩形的下边长
  289. if ((Q.x - p1.x) * (p2.y - p1.y) == (p2.x - p1.x) * (Q.y - p1.y) && Q.x >= minx && Q.x <= maxx && Q.y >= miny && Q.y <= maxy) {
  290. return true;
  291. }
  292. return false;
  293. } //点与圆的碰撞
  294. //通过计算点到圆心的距离与半径比较判断circle(由中心点和半径构成)
  295. static pointInCircle(point, center, radius) {
  296. return Vec2.squaredDistance(point, center) <= radius * radius;
  297. } //点与矩形的碰撞
  298. //通过判断点坐标是否在矩形四个顶点围成的坐标区域内
  299. static pointInRect(point, rect) {
  300. if (point.x >= rect.x && point.x <= rect.x + rect.width && point.y >= rect.y && point.y <= rect.y + rect.height) return true;
  301. return false;
  302. } //线与矩形的碰撞
  303. static lineInRect(p1, p2, rect) {
  304. var height = p1.y - p2.y;
  305. var width = p2.x - p1.x;
  306. var c = p1.x * p2.y - p2.x - p1.y; //计算叉乘
  307. if (height * rect.xMin + width * rect.yMin + c >= 0 && height * rect.xMax + width * rect.yMax + c <= 0 || height * rect.xMin + width * rect.yMin + c <= 0 && height * rect.xMax + width * rect.yMax + c >= 0 || height * rect.xMin + width * rect.yMax + c >= 0 && height * rect.xMax + width * rect.yMin + c <= 0 || height * rect.xMin + width * rect.yMax + c <= 0 && height * rect.xMax + width * rect.yMin + c >= 0) {
  308. if (p1.x < rect.xMin && p2.x < rect.xMin || p1.x > rect.xMax && p2.x > rect.xMax || p1.y > rect.yMin && p2.y > rect.yMin || p1.y < rect.yMax && p2.y < rect.yMax) {
  309. return false;
  310. } else {
  311. return true;
  312. }
  313. } else {
  314. return false;
  315. }
  316. } //两个rect是否相交
  317. static collisionRectWithRect(rect1, rect2) {
  318. //计算相交部分的矩形
  319. //左下角坐标:( lx , ly ) //右上角坐标:( rx , ry )
  320. var lx = Math.max(rect1.xMin, rect2.xMin);
  321. var ly = Math.max(rect1.yMin, rect2.yMin);
  322. var rx = Math.min(rect1.xMax, rect2.xMax);
  323. var ry = Math.min(rect1.yMax, rect2.yMax); //判断是否能构成小矩形
  324. if (lx > rx || ly > ry) return false; //矩形不相交
  325. return true; //发生碰撞
  326. } //圆与矩形是否相交
  327. static collisionRectWithCircle(rect, p, r) {
  328. //获取矩形信息
  329. //左下角坐标:( lx , ly )
  330. //右上角坐标:( rx , ry )
  331. var lx = rect.xMin;
  332. var ly = rect.yMin;
  333. var rx = rect.xMax;
  334. var ry = rect.yMax; //计算圆心到四个顶点的距离
  335. var d1 = Vec2.distance(p, new Vec2(lx, ly));
  336. var d2 = Vec2.distance(p, new Vec2(lx, ry));
  337. var d3 = Vec2.distance(p, new Vec2(rx, ly));
  338. var d4 = Vec2.distance(p, new Vec2(rx, ry)); //判断是否碰撞//判断距离是否小于半径
  339. if (d1 < r || d2 < r || d3 < r || d4 < r) return true; //是否在圆角矩形的,横向矩形内
  340. if (p.x > lx - r && p.x < rx + r && p.y > ly && p.y < ry) return true; //是否在圆角矩形的,纵向矩形内
  341. if (p.x > lx && p.x < rx && p.y > ly - r && p.y < ry + r) return true; //不发生碰撞
  342. return false;
  343. }
  344. /**
  345. * 计算向量在指定平面上的投影
  346. * @param vector 被投影的向量
  347. * @param planeNormal 平面法线
  348. */
  349. static projectOnPlane(vector, planeNormal) {
  350. // 也可以直接用 Vec3 自带的平面投影函数
  351. // return Vec3.projectOnPlane(new Vec3, targetDir, planeNormal);
  352. // 使用点乘计算方向矢量在平面法线上的投影长度
  353. var projectionLength = Vec3.dot(vector, planeNormal); // 平面法线与长度相乘得到方向矢量在平面法线上的投影矢量
  354. var vectorOnPlane = tempVec3.set(planeNormal).multiplyScalar(projectionLength); // 方向矢量减去其在平面法线上的投影矢量即是其在平面上的投影矢量
  355. return Vec3.subtract(new Vec3(), vector, vectorOnPlane);
  356. }
  357. /**
  358. * 计算两个向量基于指定轴的夹角(逆时针方向为正方向,值范围 -180 ~ 180)
  359. * @param a 向量 a
  360. * @param b 向量 b
  361. * @param axis 参照轴向量(请确保是归一化的)
  362. */
  363. static signedAngle(a, b, axis) {
  364. // 将向量 a 和 b 分别投影到以 axis 为法线的平面上
  365. var aOnAxisPlane = this.projectOnPlane(a, axis);
  366. var bOnAxisPlane = this.projectOnPlane(b, axis); // 归一化处理
  367. var aNormalized = aOnAxisPlane.normalize();
  368. var bNormalized = bOnAxisPlane.normalize(); // 求出同时垂直于 a 和 b 的法向量
  369. var abNormal = Vec3.cross(new Vec3(), aNormalized, bNormalized).normalize(); // 将法向量到 axis 上的投影长度
  370. // 若投影长度为正值(+1)则表示法向量与 axis 同向(向量叉乘的右手法则)
  371. var sign = Vec3.dot(abNormal, axis); // 求出向量 a 和 b 的夹角
  372. var radian = Math.acos(Vec3.dot(aNormalized, bNormalized)); // 混合在一起!
  373. return radian * sign * this.rad2Deg;
  374. } //获取某点到某点的Y轴方向
  375. static GetYDir(from, to) {
  376. var dir = to.subtract(from);
  377. dir.y = from.y;
  378. return dir.normalize();
  379. } //向量绕Y轴转一个角度
  380. static RotateAroundAxisY(dir, angle) {
  381. dir = dir.clone();
  382. var rad = this.degreesToRadians(angle);
  383. var cos = Math.cos(rad);
  384. var sin = Math.sin(rad);
  385. dir.x = dir.x * cos + dir.z * sin;
  386. dir.z = dir.x * -sin + dir.z * cos;
  387. return dir;
  388. } //将一个向量围绕X轴旋转angle个角度
  389. static RotateAroundAxisX(dir, angle) {
  390. dir = dir.clone();
  391. var rad = this.degreesToRadians(angle);
  392. var cos = Math.cos(rad);
  393. var sin = Math.sin(rad);
  394. dir.y = dir.y * cos - dir.z * sin;
  395. dir.z = dir.y * sin + dir.z * cos;
  396. return dir;
  397. } //将一个向量围绕Z轴旋转angle个角度
  398. static RotateAroundAxisZ(dir, angle) {
  399. dir = dir.clone();
  400. var rad = this.degreesToRadians(angle);
  401. var cos = Math.cos(rad);
  402. var sin = Math.sin(rad);
  403. dir.x = dir.x * cos - dir.y * sin;
  404. dir.y = dir.x * sin + dir.y * cos;
  405. return dir;
  406. } //线段是否与矩形相交
  407. static LineRectIntersection(lineStartPoint, lineEndPoint, rectMinX, rectMaxX, rectMinY, rectMaxY) {
  408. //针对四种不同的情况,进行碰撞检测
  409. var minXLinePoint = lineEndPoint;
  410. if (lineStartPoint.x <= lineEndPoint.x) {
  411. minXLinePoint = lineStartPoint;
  412. }
  413. var maxXLinePoint = lineStartPoint;
  414. if (lineStartPoint.x <= lineEndPoint.x) {
  415. maxXLinePoint = lineEndPoint;
  416. }
  417. var minYLinePoint = lineEndPoint;
  418. if (lineStartPoint.y <= lineEndPoint.y) {
  419. minYLinePoint = lineStartPoint;
  420. }
  421. var maxYLinePoint = lineStartPoint;
  422. if (lineStartPoint.y <= lineEndPoint.y) {
  423. maxYLinePoint = lineEndPoint;
  424. }
  425. if (minXLinePoint.x <= rectMinX && rectMinX <= maxXLinePoint.x) {
  426. var m = (maxXLinePoint.y - minXLinePoint.y) / (maxXLinePoint.x - minXLinePoint.x);
  427. var intersectionY = (rectMinX - minXLinePoint.x) * m + minXLinePoint.y;
  428. if (minYLinePoint.y <= intersectionY && intersectionY <= maxYLinePoint.y && rectMinY <= intersectionY && intersectionY <= rectMaxY) {
  429. return true; //new Vec2(rectMinX, intersectionY)
  430. }
  431. }
  432. if (minXLinePoint.x <= rectMaxX && rectMaxX <= maxXLinePoint.x) {
  433. var _m = (maxXLinePoint.y - minXLinePoint.y) / (maxXLinePoint.x - minXLinePoint.x);
  434. var _intersectionY = (rectMaxX - minXLinePoint.x) * _m + minXLinePoint.y;
  435. if (minYLinePoint.y <= _intersectionY && _intersectionY <= maxYLinePoint.y && rectMinY <= _intersectionY && _intersectionY <= rectMaxY) {
  436. return true; //new Vec2(rectMaxX, intersectionY)
  437. }
  438. }
  439. if (minYLinePoint.y <= rectMaxY && rectMaxY <= maxYLinePoint.y) {
  440. var rm = (maxYLinePoint.x - minYLinePoint.x) / (maxYLinePoint.y - minYLinePoint.y);
  441. var intersectionX = (rectMaxY - minYLinePoint.y) * rm + minYLinePoint.x;
  442. if (minXLinePoint.x <= intersectionX && intersectionX <= maxXLinePoint.x && rectMinX <= intersectionX && intersectionX <= rectMaxX) {
  443. return true; //new Vec2(intersectionX, rectMaxY)
  444. }
  445. }
  446. if (minYLinePoint.y <= rectMinY && rectMinY <= maxYLinePoint.y) {
  447. var _rm = (maxYLinePoint.x - minYLinePoint.x) / (maxYLinePoint.y - minYLinePoint.y);
  448. var _intersectionX = (rectMinY - minYLinePoint.y) * _rm + minYLinePoint.x;
  449. if (minXLinePoint.x <= _intersectionX && _intersectionX <= maxXLinePoint.x && rectMinX <= _intersectionX && _intersectionX <= rectMaxX) {
  450. return true; //new Vec2(intersectionX, rectMinY)
  451. }
  452. }
  453. return false;
  454. }
  455. });
  456. MathUtil.zore = new Vec2(1, 0);
  457. _cclegacy._RF.pop();
  458. _crd = false;
  459. }
  460. };
  461. });
  462. //# sourceMappingURL=2d836b3dc11c70cd1a8621bc539f2b1ca2080115.js.map