当前位置:   article > 正文

laya2d 物体从起点移动到目标点(按一定角速度平滑偏转)_laya 椭圆形移动

laya 椭圆形移动

可方便的实现火箭弹追踪效果

  1. import MathE from "./MathE";
  2. import Script = Laya.Script;
  3. import Image = Laya.Image;
  4. import Point = Laya.Point;
  5. export default class MoveAngleToTarget extends Script {
  6. private _targetPos: Point = null;
  7. public get targetPos(): Point {
  8. return this._targetPos;
  9. }
  10. public set targetPos(val: Point) {
  11. this._targetPos = val;
  12. }
  13. private img: Image;
  14. /** 每秒移动多少像素 */
  15. private speed: number = 200;
  16. /** 每秒偏移多少角度 */
  17. private rotateSpeed: number = 120;
  18. // 最小旋转距离, 如果在这个距离内 向目标点偏转 会转一个圆圈,从而永远到不了目标点
  19. private minRotateDistance: number;
  20. constructor() {
  21. super();
  22. }
  23. onAwake() {
  24. this.img = this.owner as Image;
  25. }
  26. onStart() {
  27. // 周长 = pi * 直径 = 速度 * 时间, 转一周的时间t = 360 / 角速度, 最短偏转距离就是直径
  28. this.minRotateDistance = this.speed * (360 / this.rotateSpeed) / Math.PI;
  29. }
  30. onUpdate() {
  31. if (this.targetPos == null) return;
  32. let delta: number = Laya.timer.delta;
  33. let ownerPos: Point = this.getPos();
  34. let distance: number = MathE.distanceV2(ownerPos, this.targetPos);
  35. let angle: number = MathE.angle(ownerPos, this.targetPos);
  36. if (distance > this.minRotateDistance || (Math.abs(angle - this.img.rotation) < 90 || Math.abs(this.img.rotation - angle) < 90)) { // 到目标点的距离大于最大偏转距离时再进行偏转
  37. if (this.img.rotation != angle) {
  38. // 慢慢朝目标点偏移过去
  39. if (Math.abs(angle - this.img.rotation) <= 180) {
  40. if (angle - this.img.rotation > 0) {
  41. if (this.img.rotation + this.rotateSpeed * delta / 1000 < angle) {
  42. this.img.rotation += this.rotateSpeed * delta / 1000;
  43. } else {
  44. this.img.rotation = angle;
  45. }
  46. } else {
  47. if (this.img.rotation - this.rotateSpeed * delta / 1000 > angle) {
  48. this.img.rotation -= this.rotateSpeed * delta / 1000;
  49. } else {
  50. this.img.rotation = angle;
  51. }
  52. }
  53. } else { // 相隔超过180度应该朝接近的地方转
  54. if (angle - this.img.rotation > 0) { // 倒转
  55. let targetRotation: number = this.img.rotation - this.rotateSpeed * delta / 1000;
  56. if (targetRotation < 0) {
  57. targetRotation += 360;
  58. if (targetRotation <= angle) {
  59. this.img.rotation = angle;
  60. } else {
  61. this.img.rotation -= this.rotateSpeed * delta / 1000;
  62. }
  63. } else {
  64. this.img.rotation -= this.rotateSpeed * delta / 1000;
  65. }
  66. } else { // 正转
  67. let targetRotation: number = this.img.rotation + this.rotateSpeed * delta / 1000;
  68. if (targetRotation > 360) {
  69. targetRotation -= 360;
  70. if (targetRotation >= angle) {
  71. this.img.rotation = angle;
  72. } else {
  73. this.img.rotation += this.rotateSpeed * delta / 1000;
  74. }
  75. } else {
  76. this.img.rotation += this.rotateSpeed * delta / 1000;
  77. }
  78. }
  79. }
  80. }
  81. if (this.img.rotation < 0) {
  82. this.img.rotation += 360;
  83. } else if (this.img.rotation > 360) {
  84. this.img.rotation -= 360;
  85. }
  86. }
  87. let dir: Point = MathE.angleToV2(this.img.rotation);
  88. let speedX: number = this.speed * dir.x * delta;
  89. let speedY: number = this.speed * dir.y * delta;
  90. // x方向移动
  91. if (speedX / Math.abs(speedX) == (this.targetPos.x - this.img.x) / Math.abs(this.targetPos.x - this.img.x)) { // 相同方向
  92. if (speedX > 0) { // 增加可读性
  93. if (this.img.x + speedX / 1000 >= this.targetPos.x) {
  94. this.img.x = this.targetPos.x;
  95. } else {
  96. this.img.x += speedX / 1000;
  97. }
  98. } else {
  99. if (this.img.x + speedX / 1000 <= this.targetPos.x) {
  100. this.img.x = this.targetPos.x;
  101. } else {
  102. this.img.x += speedX / 1000;
  103. }
  104. }
  105. } else { // 不同方向
  106. this.img.x += speedX / 1000;
  107. }
  108. // y方向移动
  109. if (speedY / Math.abs(speedY) == (this.targetPos.y - this.img.y) / Math.abs(this.targetPos.y - this.img.y)) { // 相同方向
  110. if (speedY > 0) {
  111. if (this.img.y + speedY / 1000 >= this.targetPos.y) {
  112. this.img.y = this.targetPos.y;
  113. } else {
  114. this.img.y += speedY / 1000;
  115. }
  116. } else {
  117. if (this.img.y + speedY / 1000 <= this.targetPos.y) {
  118. this.img.y = this.targetPos.y;
  119. } else {
  120. this.img.y += speedY / 1000;
  121. }
  122. }
  123. } else {
  124. this.img.y += speedY / 1000;
  125. }
  126. if (this.img.x == this.targetPos.x && this.img.y == this.targetPos.y) { // 到达终点停止
  127. this.targetPos = null;
  128. }
  129. }
  130. public getPos(): Point {
  131. return new Point(this.img.x, this.img.y);
  132. }
  133. }

 MathE工具类在这里

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
  

闽ICP备14008679号