Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图返回一个向量 我是C++编程新手,想让一些BoID全部移动到中心,我有两种方法:UpDeBOEID和内聚。在内聚中,我试图将一个规范化的向量返回到updateBoid中,当我这样做时,Boid只是向侧面移动,而不是向中心移动。我在做一些傻事,如果能帮上忙,我将不胜感激 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; } } }_C++_Vector_Return - Fatal编程技术网

试图返回一个向量 我是C++编程新手,想让一些BoID全部移动到中心,我有两种方法:UpDeBOEID和内聚。在内聚中,我试图将一个规范化的向量返回到updateBoid中,当我这样做时,Boid只是向侧面移动,而不是向中心移动。我在做一些傻事,如果能帮上忙,我将不胜感激 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; } } }

试图返回一个向量 我是C++编程新手,想让一些BoID全部移动到中心,我有两种方法:UpDeBOEID和内聚。在内聚中,我试图将一个规范化的向量返回到updateBoid中,当我这样做时,Boid只是向侧面移动,而不是向中心移动。我在做一些傻事,如果能帮上忙,我将不胜感激 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; } } },c++,vector,return,C++,Vector,Return,这是可行的,但需要使用另一种方法进行更新。在每个内聚调用中总是(基本上)返回相同的方向,因此将所有m->acc设置为相同的值。这是因为您在内聚性中返回的已经为第一个boid退出 问题在于,您还没有决定内聚性应该做什么。如果要返回一个BOID的目标方向,那么你必须告诉它要考虑哪个BOID。您也可以直接在内聚中更新m->acc,只需离开updatebook修改m->pos(并且只阅读m->acc),这可能是更好的解决方案 代码: 选项1:不返回任何内容。只修改每个boid。不要在updateBoi

这是可行的,但需要使用另一种方法进行更新。

在每个
内聚
调用中总是(基本上)返回相同的方向,因此将所有
m->acc
设置为相同的值。这是因为您在
内聚性
中返回的
已经为第一个boid退出

问题在于,您还没有决定
内聚性
应该做什么。如果要返回一个BOID的目标方向,那么你必须告诉它要考虑哪个BOID。您也可以直接在
内聚
中更新
m->acc
,只需离开
updatebook
修改
m->pos
(并且只阅读
m->acc
),这可能是更好的解决方案

代码:


选项1:不返回任何内容。只修改每个boid。不要在
updateBoid
内调用
内聚
,只调用一次

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();
        }
    }
}
void场景::updateBoid()
{
对于(尺寸i=0;idir;
m->pos+=acc*0.05f;
}
}
}
void场景::updateconsesion()
{
对于(规模i=0;首次公开募股;
}
center/=m_collection[i].size();//这样做会得到中心
///Boid移动到其平均位置的中心
用于(自动&m:m_集合[i])
{
m->dir=center-m->pos;//两个对象之间的向量
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;
}
void场景::updateBoid()
{
对于(规模i=0;IPO+=acc*0.05f;
}
}
}
//这些类型是什么?我们不知道,这是问题中遗漏的。
Vec3场景::CohensionForboid(?集合,?m)
{
Vec3中心;
for(auto _m:collection)//所有的boid
{
中心+=\u m->pos;
}
center/=collection.size();//这样做会使
Vec3 dir=center-m->pos;//两个对象之间的向量
dir.normalize();
返回目录;
}
用于(自动&m:m_集合[i])
{
m->dir=center-m->pos;//两个对象之间的向量
m->dir.normalize();

返回m->dir;//对不起,我对C++是新的,我仍然没有得到它。我试图为整个集合而不仅仅是一个BOID应该返回的BoID?我试图返回一个标准化的向量,以便它可以在UpDeDeO()中增加位置。 function@ClarenceRajaratnam当函数返回时,它将结束。如果您总是在
m\u集合[i]
中为第一个
m
返回
m->dir
,您将永远无法到达第二个
m
,因为函数已经完成。如果您有三个BOID,并且总是返回第一个BOID的方向(这是您当前所做的),那么您将永远无法在
updatebook
中为其他两个找到正确的方向。您想做的事情非常清楚,但是(恕我直言)也很清楚,你不太明白C++中函数是如何工作的。我建议用调试器来调试你的代码,看看计算机到底在做什么。你应该增加更多代码。因为现在我不知道应该是什么工作的正规化(),也不知道M是什么。
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;
}
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
}