MathUtils.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { _decorator, Component, Node, Vec2, Vec3 } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. export class MathUtils{
  4. /**
  5. * 获取速度分量 从2个点及速度计算
  6. * @param currentPoint
  7. * @param targetPoint
  8. * @param speed
  9. * @param result
  10. */
  11. static getSpeed2dByPoint(currentPoint:Vec2,targetPoint:Vec2,speed:number,result?:Vec2):Vec2{
  12. if(result==null){
  13. result=new Vec2();
  14. }
  15. let angle:number=this.getRadian(targetPoint.x,targetPoint.y,currentPoint.x,currentPoint.y);
  16. this.getSpeed2DR(angle,speed,result);
  17. return result;
  18. }
  19. /**
  20. * 速度转2维速度
  21. * @param angle 角度
  22. * @param speed 速度
  23. * @param result 结果
  24. */
  25. public static getSpeed2D(angle:number,speed:number,result:Vec2=null):Vec2{
  26. //角度转弧度
  27. let radian:number= angle * (Math.PI /180);
  28. return this.getSpeed2DR(radian,speed,result);
  29. }
  30. public static getSpeed2DR(radian:number,speed:number,result:Vec2=null):Vec2{
  31. if(result==null){
  32. result=new Vec2();
  33. }
  34. result.x=Math.cos(radian)*speed;
  35. result.y=Math.sin(radian)*speed;
  36. return result;
  37. }
  38. /**
  39. * 根据角度和X轴计算Y轴速度
  40. * @param angle
  41. * @param speedX
  42. * @param result
  43. */
  44. public static getSpeed2DByX(angle:number,speedX:number,result:Vec2=null):Vec2{
  45. if(result==null){
  46. result=new Vec2();
  47. }
  48. //角度转弧度
  49. let radian:number= angle * (Math.PI /180);
  50. //求出斜边的长度
  51. let leg:number=speedX/Math.cos(radian);
  52. result.x=speedX;
  53. result.y=Math.sin(radian)*leg;
  54. return result;
  55. }
  56. /**
  57. * 求旋转后的点坐标
  58. * @param angle 角度
  59. * @param point 旋转前的坐标点
  60. * @param result
  61. */
  62. public static rotationPoint(angle:number,point:Vec2,result:Vec2):Vec2{
  63. //角度转弧度
  64. angle=angle*(Math.PI/180);
  65. if(result==null){
  66. result=new Vec2();
  67. }
  68. result.x = Math.cos(angle) * point.x - Math.sin(angle) * point.y;
  69. result.y = Math.cos(angle) * point.y + Math.sin(angle) * point.x;
  70. return result;
  71. }
  72. public static getAngle(a:Vec2,b:Vec2):number{
  73. return this.getRadianByPoint(a,b)*(180/Math.PI);
  74. }
  75. public static getRadianByPoint(a:Vec2,b:Vec2):number{
  76. return Math.atan2((a.y-b.y), (a.x-b.x));
  77. }
  78. public static getRadian(ax:number,ay:number,bx:number,by:number):number{
  79. return Math.atan2(ay-by, ax-bx);
  80. }
  81. static angle2Radian(angle:number):number{
  82. return angle*(Math.PI/180);
  83. }
  84. static radian2Angle(radian:number):number{
  85. return radian*(180/Math.PI);
  86. }
  87. /**
  88. * 计算两线段相交点坐标
  89. * @param line1Point1
  90. * @param line1Point2
  91. * @param line2Point1
  92. * @param line2Point2
  93. * @return 返回该点
  94. */
  95. static getIntersectionPoint(line1Point1:Vec2, line1Point2:Vec2, line2Point1:Vec2, line2Point2:Vec2,result?:Vec2):Vec2{
  96. if(!result){
  97. result=new Vec2();
  98. }
  99. let x1:number,y1:number,x2:number,y2:number,x3:number,y3:number,x4:number,y4:number;
  100. x1 = line1Point1.x;
  101. y1 = line1Point1.y;
  102. x2 = line1Point2.x;
  103. y2 = line1Point2.y;
  104. x3 = line2Point1.x;
  105. y3 = line2Point1.y;
  106. x4 = line2Point2.x;
  107. y4 = line2Point2.y;
  108. result.x=Math.ceil((x1 - x2) * (x3 * y4 - x4 * y3) - (x3 - x4) * (x1 * y2 - x2 * y1)) / ((x3 - x4) * (y1 - y2) - (x1 - x2) * (y3 - y4));
  109. result.y=Math.ceil((y1 - y2) * (x3 * y4 - x4 * y3) - (x1 * y2 - x2 * y1) * (y3 - y4)) / ((y1 - y2) * (x3 - x4) - (x1 - x2) * (y3 - y4));
  110. return result;
  111. }
  112. /**
  113. * 判断点是否在线段内
  114. * @param Pi
  115. * @param Pj
  116. * @param Q
  117. */
  118. static onSegment(Pi:Vec2 , Pj:Vec2 , Q:Vec2):boolean
  119. {
  120. Q.x=Math.round(Q.x);
  121. Q.y=Math.round(Q.y);
  122. let xValue:number=(Q.x - Pi.x) * (Pj.y - Pi.y) - (Pj.x - Pi.x) * (Q.y - Pi.y);
  123. if(Math.floor(xValue)==0
  124. //保证Q点坐标在pi,pj之间
  125. && Math.min(Pi.x , Pj.x) <= Q.x && Q.x <= Math.max(Pi.x , Pj.x)
  126. && Math.min(Pi.y , Pj.y) <= Q.y && Q.y <= Math.max(Pi.y , Pj.y)){
  127. return true;
  128. }
  129. return false;
  130. }
  131. /**
  132. * 求两个向量之间的夹角
  133. * @param av 单位向量
  134. * @param bv 单位向量
  135. */
  136. static calculateAngle(av:Vec3,bv:Vec3):number{
  137. //cos=a.b/(|a||b|);
  138. return Math.acos(Vec3.dot(av,bv)/(av.length()*bv.length()));
  139. }
  140. static calculateAngleByPoints(a:Vec3,b:Vec3,c:Vec3){
  141. let av:Vec3=new Vec3();
  142. let bv:Vec3=new Vec3();
  143. Vec3.subtract(b,a,av);
  144. Vec3.subtract(b,c,bv);
  145. return this.calculateAngle(av,bv);
  146. }
  147. }