Im new to programming in c++ and trying to get a some boids all move to center, I have two methods updateboid and cohesion. In cohesion Im trying to return a normalized vector into updateBoid, and when I do the boids just all move sideways and not towards center. Im doing something silly here, some help will be greatly appreciated.
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;
}
}
}
Previous Code in cohesion()
m->dir =center - m->pos; //length between the two objects
m->dir.normalize();
m->pos+=m->dir * 0.25f; //speed
This worked, but want another approach by using another method to update.