直接上才艺:
在鱼群、鸟群等群体移动过程中,会发生群聚行为。群聚行为主要有3个约束条件:
- Spearation(分离)
- Alignment(一致性)
- Cohesion(聚集)
1. Spearation
一个个体在移动过程中,尽量避免与周围个体发生碰撞,保持一定距离。
-- 分离
function Bird:separation()
local radius = 2
local force = Vector3.zero
local found = 0
local forceScale = 15
for i = 1, #self.foundedBirds do
if Vector3.Distance(self.gameObject.transform.position, self.foundedBirds[i].gameObject.transform.position) < radius then
force = force + (self.gameObject.transform.position - self.foundedBirds[i].gameObject.transform.position)
found = found + 1
end
end
if found > 0 then
force = force / found * forceScale
return Vector3.Lerp(self.velocity, force, Time.deltaTime)
end
return self.velocity
end
2. Alignment
个体在移动中,移动方向总是趋近群体的平均移动方向
-- 一致性
function Bird:alignment()
local average = Vector3.zero
local found = 0
if self.foundedBirds and #self.foundedBirds > 0 then
for i = 1, #self.foundedBirds do
average = average + self.foundedBirds[i].velocity
found = found + 1
end
end
if found > 0 then
average = average / found
return Vector3.Lerp(self.velocity, average, Time.deltaTime)
end
return self.velocity
end
3. Cohesion
个体在移动过程中,总是趋向于群体的中心方向移动
-- 凝聚力
function Bird:cohesion()
local radius = 5
local averagePos = Vector3.zero
local found = 0
for i = 1, #self.foundedBirds do
if Vector3.Distance(self.gameObject.transform.position, self.foundedBirds[i].gameObject.transform.position) > radius then
averagePos = averagePos + self.foundedBirds[i].gameObject.transform.position
found = found + 1
end
end
if found > 0 then
averagePos = averagePos / found
local targetVelocity = (averagePos - self.gameObject.transform.position).normalized * self.velocity.magnitude
return Vector3.Lerp(self.velocity, targetVelocity, Time.deltaTime)
end
return self.velocity
end
了解了上述的约束条件之后,就可以开始写逻辑了:
- 每个个体有个默认的速度矢量
- 每个个体有一个列表存储这周围所有个体
- 通过周围所有个体按照三个约束条件以及权重计算下一帧移动的速度矢量(我这里只是简单平均了一下)
整体的逻辑很简单,需要调整参数打到一个合理的范围
除了上述三个约束条件外,可以另外添加其他的去约束条件,例如:
- 趋向目标
- 壁障躲避
- 遵循轨迹
- …
优化:
目前demo里的寻找周围个体的是全部计算,这样会有大量的无用计算,建议使用八叉树进行优化,会降低很多计算量
想要看全部代码可以去我的github看具体的Unity项目:传送门