【问题标题】:trying to return a vector试图返回一个向量
【发布时间】: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 是什么

标签: c++ vector return


【解决方案1】:
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 已经指出的那样,for 循环只会执行一次。 我称之为错误。从修改代码开始。

【讨论】:

    【解决方案2】:

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

    问题是你还没有决定cohesion 应该做什么。如果它应该返回 one boid 的目标方向,那么你必须告诉它 which boid 要考虑。您也可以直接在cohesion 中更新m-&gt;acc,然后离开updateBoid 以修改m-&gt;pos(并且只读取m-&gt;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;
    }
    

    【讨论】:

    • 对不起,我是 C++ 新手,我还是不明白。我试图为整个系列做,而不仅仅是一个 boid
    • 内聚应该返回哪个boid?
    • 我试图返回一个标准化的向量,以便它可以随着 update() 函数中的位置而增加
    • @ClarenceRajaratnam 当函数返回时,它结束。如果您总是m_collection[i] 中的第一个m 返回m-&gt;dir,您将永远不会到达第二个m,因为该函数已经完成。如果您有三个 boid 并且始终返回第一个的方向(这就是您目前所做的),那么您将永远无法在 updateBoid 中获得其他两个的正确方向。
    • 很清楚你想要做什么,但是(相对而言)很清楚你并不真正了解函数在 C++ 中是如何工作的。我建议使用调试器单步执行您的代码,以查看计算机实际执行的操作。
    猜你喜欢
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    • 2017-03-09
    • 2023-02-05
    • 1970-01-01
    相关资源
    最近更新 更多