我是 C++ 编程的新手,并试图让一些 boids 都移到中心,我有两种方法 updateboid 和 cohesion。在凝聚力中,我试图将归一化向量返回到 updateBoid,当我这样做时,boids 只是全部侧向移动,而不是向中心移动。我在这里做一些愚蠢的事情,我们将不胜感激。
void Scene::updateBoid()
{
for(size_t i=0; i<m_collection.size(); ++i)
{
for(auto &m :m_collection[i])
{
m->acc = cohesion();
m->pos += m->acc * 0.05f ;
}
}
}
Vec3 Scene::cohesion()
{
const float pi = 3.14f;
Vec3 center;
Vec3 test;
for(size_t i=0; i<m_collection.size(); ++i)
{
for(auto _m :m_collection[i]) // all of the boids
{
center += _m->pos;
}
center /= m_collection[i].size(); // doing this gives the center
/// Boids move to the center of their average positions
for(auto &m :m_collection[i])
{
m->dir =center - m->pos; //vector between the two objects
m->dir.normalize();
return m->dir;
}
}
}
cohesion() 中的先前代码
m->dir =center - m->pos; //length between the two objects
m->dir.normalize();
m->pos+=m->dir * 0.25f; //speed
这行得通,但希望通过使用另一种方法来更新另一种方法。