试图返回向量
trying to return a vector
我是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;
}
相关文章:
- 矩阵向量乘法(cublasDgemv)返回零
- C++中函数的向量返回类型引发错误
- 元组由 Swig 生成的 Python 包装器返回,用于C++向量
- 在 C++11 中,如何查找并返回以给定字符串开头的字符串向量中的所有项?
- 从 std<Derived>::shared_ptr 的向量返回 std::shared_ptr<Base>
- 从对象的向量返回某个类的对象
- C++ 将向量中出现 n 次的所有元素作为向量返回
- 向量返回负大小 c++
- 如何从向量返回给定类型的元素?
- 将对象的向量返回到函数
- 尝试从向量返回对象时出现"没有可行的重载运算符 [] 错误
- 调用函数时,如何通过向量返回类型
- 向量返回/加法算法的问题
- 在向量返回上移动语义行为
- 从int c++的向量返回一个无符号长
- 向量返回空 C++
- 从融合向量的std向量返回列,无需复制
- C++为空向量上的向量返回什么::back()
- 函数的向量返回
- 如何用适当的OO有效地将100万的私有向量返回给其他几个类