打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Unity人工智能学习

CSDN个人博客地址,凯尔八阿哥栏http://blog.csdn.net/zhangxiao13627093203,转载请注明出处

上一篇讲到了追踪算法的比较简单的形式,看上去比较假,因为AI控制的对象过于精确地跟踪目标。一种更自然的追踪方式可以这样做,使得跟踪者的方向矢量与从跟踪目标的中心到跟踪者的中心所定义的方向矢量靠拢。如图所示:



这个算法的基本思路是这样的:假设AI控制的对象即追踪者有如下属性

1、Position:(tracker.x,tracker.y)

2、Velocity:(tracker.vx,tracker.vy)

追踪目标有如下属性:

1、Postion:(target.x,target.y)

2、Velocity:(target.vx,target.vy)

接下来就是调整追踪者的速度向量的常用逻辑:

1、计算从跟踪者到跟踪目标的向量:TV=(target.x-tracker.x,target.y-tracker.y)=(tvx,tvy),归一化TV,这样就可以得到一个单位向量,从而方便计算它与坐标轴的角度。归一化也即是sqrt(x^2+y^2).

2、调整追踪者当前的速度向量,加上一个按rate比例缩放过的TV*

tracker.x+=rate*tvx;

traker.y+=rate*tvy;

注意这一步才是关键,它使得导弹的追踪不在是从前的直接紧密追踪而是会有一个变轨迹的过程,另外当rate等于1的时候,跟踪向量会合的更快,跟踪算法对目标跟踪的根据紧密,并更快地的修正目标的运动。

3、跟踪者的速度向量修改过后,有可能向量的速度会溢出最大值。换言之,跟踪者一旦锁定了目标的方向就会继续沿着该方向加速。所以需要设置一个上限,让追踪者的速度从某处慢下来

在Unity5.1.1实现的效果图如图所示:

在这里我还调整了导弹的追踪方向rotation的变化,其实如果是3D空间就可以直接使用lookAt方法来使得导弹的运动方向始终朝向目标,但是在2D平面上就没有这么好的方法供我们调用了,所以我自己写了一个算法。如果不加这个方向修正算法的结果如图所示:

导弹的运行是不是显得非常的生硬,运动的轨迹和导弹头的朝向并不一致,这一段的修正导弹头的方向的代码如下:

[csharp] view plain copy
  1. void LookAtTarget()  
  2.    {  
  3.        float zAngles;  
  4.        if(moveVy==0)  
  5.        {  
  6.            zAngles = moveVx >= 0 ? -90 : 90;  
  7.        }  
  8.        zAngles = Mathf.Atan(moveVx / moveVy) * (-180 / Mathf.PI);  
  9.       if(moveVy<0)  
  10.       {  
  11.           zAngles = zAngles - 180;  
  12.       }  
  13.        Vector3 tempAngles = new Vector3(0, 0, zAngles);  
  14.        Quaternion tempQua = this.transform.rotation;  
  15.        tempQua.eulerAngles = tempAngles;  
  16.        this.transform.rotation = tempQua;  
  17.    }  
算法的计算思路是:

注意:这个平面上的角度主要是Z轴的角度变化,而Z轴的角度是导弹头方向直线与y轴的夹角,这点比较蛋疼。另外坐标的顶点是位于屏幕的左上角。

1、根据导弹的运动速度矢量来调整导弹头的方向

2、导弹的速度矢量为x和y方向的矢量和,根据反三角函数来计算出导弹与屏幕坐标y轴的夹角

3、要特别注意当moveVy为0的情况,不考虑这个会导致计算反三角的时候分母为零而因溢出而报错,以及moveVy小于0的情况,不考虑这个会使得方向刚好会想法。

最终的代码为:

[csharp] view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using UnityEngine.UI;  
  4.   
  5. public class AITrackAdvanced : MonoBehaviour {  
  6.     public Image target;  
  7.     public float target_moveSpeed;  
  8.     public float MIN_trackingRate;//最小的追踪向量改变率  
  9.     public float MIN_TrackingDis;  
  10.     public float MAX_trackingVel;  
  11.     public float moveVx;//x方向的速度  
  12.     public float moveVy;//y方向的速度  
  13.     // Use this for initialization  
  14.     void Start () {  
  15.       
  16.     }  
  17.       
  18.     // Update is called once per frame  
  19.     void Update () {  
  20.         Debug.Log((Mathf.Atan(moveVx / moveVy) * (-180 / Mathf.PI)));  
  21.   
  22.        //  LookAtTarget();  
  23.       //  this.transform.position += new Vector3(moveVx * Time.deltaTime, moveVy * Time.deltaTime, 0);  
  24.        MoveTarget();  
  25.        Track_AIAdvanced();  
  26.        CheckMoveBoundary();  
  27.     }  
  28.     void LookAtTarget()  
  29.     {  
  30.         float zAngles;  
  31.         if(moveVy==0)  
  32.         {  
  33.             zAngles = moveVx >= 0 ? -90 : 90;  
  34.         }  
  35.         zAngles = Mathf.Atan(moveVx / moveVy) * (-180 / Mathf.PI);  
  36.        if(moveVy<0)  
  37.        {  
  38.            zAngles = zAngles - 180;  
  39.        }  
  40.         Vector3 tempAngles = new Vector3(0, 0, zAngles);  
  41.         Quaternion tempQua = this.transform.rotation;  
  42.         tempQua.eulerAngles = tempAngles;  
  43.         this.transform.rotation = tempQua;  
  44.     }  
  45.     /// <summary>  
  46.     /// 通过键盘来控制移动目标  
  47.     /// </summary>  
  48.     void MoveTarget()  
  49.     {  
  50.         float x = Input.GetAxis("Horizontal") * 100;  
  51.         float y = Input.GetAxis("Vertical") * 100;  
  52.         //如果超出屏幕范围则让它出现在另一面  
  53.         target.transform.Translate(x * Time.deltaTime * target_moveSpeed, y * Time.deltaTime * target_moveSpeed, 0);  
  54.         if (target.transform.position.x >= Screen.width)  
  55.         {  
  56.             //使用了Image的target.rectTransform.lossyScale.x来表示显示的图片宽度  
  57.             target.transform.position = new Vector3(-target.rectTransform.lossyScale.x, target.transform.position.y, 0);  
  58.         }  
  59.         else if (target.transform.position.x < -target.rectTransform.lossyScale.x)  
  60.         {  
  61.             target.transform.position = new Vector3(Screen.width, target.transform.position.y, 0);  
  62.         }  
  63.         if (target.transform.position.y >= Screen.height)  
  64.         {  
  65.             target.transform.position = new Vector3(target.transform.position.x, -target.rectTransform.lossyScale.y, 0);  
  66.         }  
  67.         else if (target.transform.position.y < -target.rectTransform.lossyScale.y)  
  68.         {  
  69.             target.transform.position = new Vector3(target.transform.position.x, Screen.height, 0);  
  70.         }  
  71.     }  
  72.     /// <summary>  
  73.     /// 追踪算法  
  74.     /// </summary>  
  75.     void Track_AIAdvanced()  
  76.     {  
  77.         //计算与追踪目标的方向向量  
  78.         float vx = target.transform.position.x - this.transform.position.x;  
  79.         float vy = target.transform.position.y - this.transform.position.y;  
  80.   
  81.         float length = PointDistance_2D(vx, vy);  
  82.         //如果达到距离就追踪  
  83.         if(length<MIN_TrackingDis)  
  84.         {  
  85.             vx = MIN_trackingRate * vx / length;  
  86.             vy = MIN_trackingRate * vy / length;  
  87.             moveVx += vx;  
  88.             moveVy += vy;  
  89.               
  90.             //增加一点扰动  
  91.             if(Random.Range(1,10)==1)  
  92.             {  
  93.                 vx = Random.Range(-1, 1);  
  94.                 vy = Random.Range(-1, 1);  
  95.                 moveVx += vx;  
  96.                 moveVy += vy;  
  97.             }  
  98.             length = PointDistance_2D(moveVx,moveVy);  
  99.   
  100.             //如果导弹飞的速度太快就让它慢下来  
  101.             if(length>MAX_trackingVel)  
  102.             {  
  103.                //让它慢下来  
  104.                 moveVx *= 0.75f;  
  105.                 moveVy *= 0.75f;  
  106.             }  
  107.               
  108.         }  
  109.          //如果不在追踪范围内,随机运动  
  110.         else  
  111.         {  
  112.            if(Random.Range(1,10)==1)  
  113.             {  
  114.                 vx= Random.Range(-2, 2);  
  115.                 vy = Random.Range(-2, 2);  
  116.                 moveVx += vx;  
  117.                 moveVy += vy;  
  118.             }  
  119.             length = PointDistance_2D(moveVx, moveVy);  
  120.   
  121.             //如果导弹飞的速度太快就让它慢下来  
  122.             if (length > MAX_trackingVel)  
  123.             {  
  124.                 //让它慢下来  
  125.                 moveVx *= 0.75f;  
  126.                 moveVy *= 0.75f;  
  127.             }  
  128.         }  
  129.          
  130.   
  131.         this.transform.position += new Vector3(moveVx * Time.deltaTime, moveVy * Time.deltaTime, 0);  
  132.     }  
  133.     /// <summary>  
  134.     /// 计算从零点到这个点的距离  
  135.     /// </summary>  
  136.     /// <param name="x"></param>  
  137.     /// <param name="y"></param>  
  138.     /// <returns></returns>  
  139.     float PointDistance_2D(float x,float y)  
  140.     {  
  141.         //使用了泰勒展开式来计算,有3.5%的误差,直接使用开方计算会比较慢,但是测试了我的电脑好像没有什么变化可能是数据量不大体现不出来  
  142.         /*x = Mathf.Abs(x); 
  143.         y = Mathf.Abs(y); 
  144.         float mn = Mathf.Min(x, y);//获取x,y中最小的数 
  145.         float result = x + y - (mn / 2) - (mn / 4) + (mn / 8);*/  
  146.   
  147.         float result = Mathf.Sqrt(x * x + y * y);  
  148.         return result;  
  149.     }  
  150.   
  151.     void CheckMoveBoundary()  
  152.     {  
  153.         //检测是否超出了边界  
  154.         if (this.transform.position.x >= Screen.width)  
  155.         {  
  156.             this.transform.position = new Vector3(-this.GetComponent<Image>().rectTransform.lossyScale.x, 0, 0);  
  157.         }  
  158.         else if (this.transform.position.x < -this.GetComponent<Image>().rectTransform.lossyScale.x)  
  159.         {  
  160.             this.transform.position = new Vector3(Screen.width, this.transform.position.y, 0);  
  161.         }  
  162.         if (this.transform.position.y >= Screen.height)  
  163.         {  
  164.             this.transform.position = new Vector3(this.transform.position.x, -this.GetComponent<Image>().rectTransform.lossyScale.y, 0);  
  165.         }  
  166.         else if (this.transform.position.y < -this.GetComponent<Image>().rectTransform.lossyScale.y)  
  167.         {  
  168.             this.transform.position = new Vector3(this.transform.position.x, Screen.height, 0);  
  169.         }  
  170.     }  
  171. }  

最后附上工程的下载地址,里面是我用Unity5.1.1写的如图的演示程序,还包括之前两篇文章中的演示程序。点击打开链接


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
unity 3D将一个物体成为自己的子物体
里程计
Matlab实现求两点间的最短路径
Unity中角色在固定的位置移动的经典代码
实际工程项目中是怎么用卡尔曼滤波的?
在U3D中生成Cubemap所需要的六面图
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服