试图返回向量

trying to return a vector

本文关键字:向量 返回      更新时间:2023-10-16

我是C 编程的新手,并试图使一些bo以全部搬迁到中心,我有两种方法更新和凝聚力。在凝聚力中,我试图将归一化的向量返回到更新时,当我进行托架时,全部侧身移动而不是向中心移动。我在这里做一些愚蠢的事情,将不胜感激。

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;
        }
    }
}

凝聚力()中的先前代码

        m->dir =center - m->pos;        //length between the two objects
        m->dir.normalize();
        m->pos+=m->dir * 0.25f; //speed

这有效,但希望通过使用另一种方法来更新。

for(auto &m :m_collection[i])
{
    m->dir =center - m->pos; //vector between the two objects
    m->dir.normalize();
    return m->dir; // <- Always return on the first iteration
}

正如马克斯·兰霍夫(Max Langhof)已经指出的那样,循环只能执行一次。我称之为错误。首先修改您的代码。

您总是在每个cohesion调用中返回(基本上)相同的方向,因此您将所有m->acc设置为相同的值。那是因为您的return中的CC_3已经退出了第一个Boid。

问题是您还没有下定决心cohesion应该做什么。如果应该返回 boid的目标方向,则必须告诉它要考虑哪个 bo。您也可以直接在cohesion中更新m->acc,然后离开updateBoid修改m->pos(并且仅读取m->acc-这可能是更好的解决方案。

代码:


选项1:不要返回任何东西。仅修改每个boid。请勿在updateBoid内拨打cohesion,仅一次。

void Scene::updateBoid()
{
    for(size_t i=0; i<m_collection.size(); ++i)
    {
        for(auto &m :m_collection[i])
        {
            float acc = m->dir;
            m->pos += acc * 0.05f;
        }
    }
}

void Scene::updateCohesion()
{
    for(size_t i=0; i<m_collection.size(); ++i)
    {
        // This must be in here, otherwise it will be slightly wrong later.
        Vec3 center;
        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();
        }
    }
}

选项2:返回每个boid的方向。这是低效的,因为您每次都会再次计算中心。

void Scene::updateBoid()
{
    for(size_t i=0; i<m_collection.size(); ++i)
    {
        for(auto &m :m_collection[i])
        {
            float acc = cohesionForBoid(m_collection[i], m);
            m->pos += acc * 0.05f;
        }
    }
}
// What are these types? We don't know, that is missing from the question.
Vec3 Scene::cohesionForBoid(??? collection, ??? m)
{
    Vec3 center;
    for(auto _m : collection) // all of the boids
    {
        center += _m->pos;
    }
    center /= collection.size(); // doing this gives the center
    Vec3 dir = center - m->pos; //vector between the two objects
    dir.normalize();
    return dir;
}