1
Conrad Parker's boids pseudocode을 구현하는 데 문제가 있습니다.속도를 맞추면 내 보이드가 세계 원점으로 돌진하는 이유는 무엇입니까?
저는 rule1, rule2 및 rule3을 구현하고 있습니다. 문제는 rule3이 활성화 될 때마다 (즉, 아래 코드에서 matchSpeed), boid가 세계 중심 (0, 0, 0)으로 돌입 한 다음 그 지점에서 무리를 지어 나간다는 것입니다. 이것은 세계 어디에서 시작하든 상관없이 발생합니다.
그러나 규칙 3이 실행되지 않으면 boid가 무리를 지어 예상대로 표류합니다. 내가 도대체 뭘 잘못하고있는 겁니까?
내 코드가 스칼라에 있고 jMonkeyEngine을 사용하고 있지만 문제가 일반적인 것 같습니다.
val sepDistance = 10f
val clumpFactor = 100f
val avoidFactor = 3f
val alignFactor = 800f
val speedLimit = 2f
def moveAgents(target: Node)
{
agents.foreach(a => {
a.velocity.addLocal(clump(a)) //rule1
a.velocity.addLocal(keepAway(a)) //rule2
a.velocity.addLocal(matchSpeed(a)) //rule3
a.velocity = limitSpeed(a.velocity)
a.move(a.velocity)
})
}
def clump (a: Agent): Vector3f = // rule1
{
val centre = Vector3f.ZERO.clone
for (oA <- agents if oA != a) yield
centre.addLocal(oA.position)
centre.divideLocal(agents.length.toFloat - 1f)
centre.subtractLocal(a.position)
centre.divideLocal(clumpFactor)
return centre
}
def keepAway (a: Agent): Vector3f = // rule2
{
val keepAway = Vector3f.ZERO.clone
for (oA <- agents if oA != a) {
if (Math.abs(oA.position.distance(a.position)) < sepDistance)
keepAway.subtractLocal(oA.position.subtract(a.position))
}
return keepAway.divide(avoidFactor)
}
def matchSpeed (a: Agent): Vector3f = // rule3
{
val matchSpeed = Vector3f.ZERO.clone
for (oA <- agents if oA != a)
matchSpeed.addLocal(oA.velocity)
matchSpeed.divideLocal(agents.length.toFloat - 1f)
matchSpeed.subtractLocal(a.position)
matchSpeed.divideLocal(alignFactor)
return matchSpeed
}