跟诸子学游戏|跟诸子学游戏 群组算法

群组算法:表示一群动物的行为,需要设置一个目标点,主要有分离,队列,聚合3中状态;
分离:物体之间保持的距离较近,需要给其一个力进行移动;
队列:物体偏离目标点的航向,需要向目标点的航向进行移动,按照时长,方向逐渐靠拢;
聚合:当物体距离群组越来越远的时候,需要将超过一定距离范围的物体重新聚集在一起,这时候求目标点加上周围较近的物体的中心点,让物体朝向这个中心点聚合;
以鸟为例,让群鸟飞行,简单例子:
//在物体上面需要添加碰撞盒public class CrowAI : MonoBehaviour { //速度系数,默认系数为5 public float speed = 3; //每一帧的加速度向量,逐渐增长到当前计算的加速度向量,也即向前飞行的力 public Vector3 velocity = Vector3.forward; //开始的速度向量,在start方法中,得到值,保持恒定 private Vector3 startVelocity; //类似头羊的效果,一群鸟需要有一个跟随一个点 public Transform target; /// /// 当前综合的力,没有力,表示不会受到影响
/// public Vector3 sumForce = Vector3.zero; //当前的质量,利用 F=ma 力等于 质量乘以加速度 来求出加速度public float m = 1; //分离的距离,当2只鸟距离有这么近时,鸟将会分离public float separationDistance = 3; //需要分离的邻居public ListseprationNeighbors = new List(); //当分离的力较小时,此权重即可变大,增加分离的力public float separationWeight = 1; //分离的力public Vector3 separationForce = Vector3.zero; //队列的距离,当2只鸟距离的方向大于6时,需要将其转向public float alignmentDistance = 6; //偏离队列的鸟的列表public ListalignmentNeighbors = new List(); //当队列的力较小时,此权重即可变大,增加队列的力public float alignmentWeight = 1; //队列的力public Vector3 alignmentForce = Vector3.zero; //当聚集的力较小时,此权重即可变大,增加聚集的力public float cohesionWeight = 1; //聚集的力public Vector3 cohesionForce = Vector3.zero; //重复计算群组的力的时间间隔public float checkInterval = 0.2f; //动画播放参数public float animRandomTime = 2f; private Animation anim; private void Start(){target = GameObject.Find("Target").transform; startVelocity = velocity; InvokeRepeating("CalcForce", 0, checkInterval); //鸟的动画播放anim = GetComponentInChildren();
Invoke("PlayAnim", Random.Range(0, animRandomTime));
}
void PlayAnim()
{
anim.Play();
}


void CalcForce()
{
//当前综合的向前的力
sumForce = Vector3.zero;
separationForce = Vector3.zero; //分离的力
alignmentForce = Vector3.zero; //队列的力
cohesionForce = Vector3.zero; //聚合的力
//分离的列表
seprationNeighbors.Clear();
//Physics.OverlapSphere 以transform.position,separationDistance为半径,得到周围的其他物体
Collider[] colliders= Physics.OverlapSphere(transform.position, separationDistance);
foreach(Collider c in colliders)
{
if (c != null && c.gameObject != this.gameObject)
{//将其他物体添加进入需要分离的列表里面
seprationNeighbors.Add(c.gameObject);
}
}
//计算分离的力
foreach(GameObject neighbor in seprationNeighbors)
{//当前的鸟减去周围的鸟的向量,表示一个周围的鸟的相反的向量
Vector3 dir = transform.position - neighbor.transform.position;
//产生分离的力,是距离当前的鸟越近,力越大,距离当前的鸟越远,力越小
separationForce += dir.normalized / dir.magnitude;
}
//如果当前分离的力比较小,可以将分离的力增加几倍,利用权重倍数
if (seprationNeighbors.Count > 0)
{//增加权重
separationForce *= separationWeight;
【跟诸子学游戏|跟诸子学游戏 群组算法】//总体的力添加分离的力
sumForce += separationForce;
}
//计算队列的力
alignmentNeighbors.Clear();
colliders = Physics.OverlapSphere(transform.position, alignmentDistance);
foreach(Collider c in colliders)
{
if (c != null && c.gameObject != this.gameObject)
{
alignmentNeighbors.Add(c.gameObject);
}
}
Vector3 avgDir = Vector3.zero;
foreach(GameObject n in alignmentNeighbors)
{//计算当前的朝向,表示出来总体的朝向,也就是一群鸟中的各个鸟的向前朝向,组合而成当前的朝向
avgDir += n.transform.forward;
}
if (alignmentNeighbors.Count > 0)
{
//一群鸟的平均朝向,即是当前的鸟需要前进的朝向,和当前的鸟的朝向不一直
avgDir /= alignmentNeighbors.Count;
//当前的鸟要向平均朝向扭转,计算公式为 平均朝向-当前的鸟朝向 即是当前的鸟需要扭转的方向
alignmentForce = avgDir - transform.forward;
//增加权重
alignmentForce *= alignmentWeight;
//总体的力添加队列的力
sumForce += alignmentForce;
}
//聚集的力,当队列的列表中有对象,表示距离超过了6米,需要聚合
if ( alignmentNeighbors.Count>0 )
{
Vector3 center = Vector3.zero;
foreach (GameObject n in alignmentNeighbors)
{
//这个地方是求的所有偏离航向的鸟的中心点
center += n.transform.position;
}
//得到这几个鸟的中心点
center /= alignmentNeighbors.Count;
//求出中心点与本物体的偏离的向量,也就是施加力的方向
Vector3 dirToCenter = center - transform.position;
cohesionForce += dirToCenter.normalized * velocity.magnitude;
cohesionForce *= cohesionWeight;
sumForce += alignmentForce;
}
//保持恒定飞行速度的力
Vector3 engineForce = velocity.normalized * startVelocity.magnitude;
sumForce += engineForce * 0.1f;
//距离target距离较远,则让其朝向target进行扭转
Vector3 targetDir = target.position - transform.position;
sumForce += (targetDir.normalized - transform.forward) * speed ;
}
// Update is called once per frame
void Update () {
//求出加速度
Vector3 a = sumForce / m;
//求出当前的加速度向量,每一帧只产生一点影响,不会立即产生影响,逐渐增长到 a 的加速度向量
velocity += a * Time.deltaTime;
//当前鸟的转向,需要向当前加速度的方向扭转
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(velocity), Time.deltaTime*speed);
//当前鸟在世界空间的移动
transform.Translate(transform.forward * Time.deltaTime * velocity.magnitude , Space.World );
}
}




跟诸子学游戏|跟诸子学游戏 群组算法
文章图片
挂载物体上面的参数配置

跟诸子学游戏|跟诸子学游戏 群组算法
文章图片
效果1

跟诸子学游戏|跟诸子学游戏 群组算法
文章图片
效果2

跟诸子学游戏|跟诸子学游戏 群组算法
文章图片
效果3

    推荐阅读