【发布时间】:2018-12-17 16:41:50
【问题描述】:
我是 C++ 编程新手,并试图让一些 boid 都移到中心,我有两种方法 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
这行得通,但想要通过使用另一种方法来更新的另一种方法。
【问题讨论】:
-
你应该添加更多代码,因为现在我不知道应该工作的 normalize() 做什么,也不知道 m 是什么