Hey everyone :).
i'm trying to create a flocking simulation but i'm having some problems.
iv been trying to use the programming game AI by example to create it but atm all the boids just end up going to the centre (and shake around on the spot) where im more looking for a result like this
when what I get is the image attached.
I have tried to debug it but i cant see where the error is. ill post the code below and any help you can provide will be very helpful.
constructor for each boid
flockobj::flockobj(modelloader *temp)
{
radiousep = 4.0f;
radiuscohu = 10.0f;
radiusaligh = 10.0f;
speed = 4.0f;
neighbors = 0;
aligvec = glm::vec3(0.0f);
conhesionvec = glm::vec3(0.0f);
sperationvec = glm::vec3(0.0f);
x = rand() % 8 - 4;
y = rand() % 8 - 4;
velocity = glm::vec3(x,y,0.0f);
inside = false;
}
update function
void flockobj::Update(float deltaTs )
{
velocity += sperationvec + aligvec + conhesionvec;
glm::vec3 move = velocity * deltaTs;
_position += move;
}
alignment function
void flockobj::Alignmentcal(std::vector<flockobj*> others) //this is the behaviour that effects the alignment of your objects
{
neighbors = 0;
aligvec = glm::vec3(0,0,0);
for(int i = 0; i < others.size(); i++)
{
float len = glm::length(others[i]->returnposition() - _position);
if(others[i] != this && len < radiusaligh)
{
if(glm::length(others[i]->returnvelocity()) != 0)
{
aligvec += glm::normalize(others[i]->returnvelocity());
if(!_finite(aligvec.x))
{
float a = glm::length(others[i]->returnvelocity());
}
}
neighbors++;
}
}
if(neighbors > 0)
{
aligvec /= neighbors;
if(glm::length(velocity) != 0)
{
aligvec -= glm::normalize(velocity);
}
if(glm::length(aligvec) > speed)
{
aligvec = glm::normalize(aligvec) * speed;
}
}
}
Cohesion function
void flockobj::Cohesioncal(std::vector<flockobj*> others) //cohesion is making sure that our objects gravitate to the center of the flock
{
neighbors = 0;
conhesionvec = glm::vec3(0,0,0);
for(int i = 0; i < others.size(); i++)
{
float len = glm::length(others[i]->returnposition() - _position);
if(others[i] != this && len < radiusaligh)
{
conhesionvec += others[i]->returnposition();
neighbors++;
if(!_finite(conhesionvec.x))
{
int g = 10;
}
}
}
if(neighbors > 0)
{
conhesionvec /= neighbors;
glm::vec3 temp = (conhesionvec - _position);
temp = glm::normalize(temp) * speed;
conhesionvec = temp - velocity;
if(glm::length(conhesionvec) > speed)
{
conhesionvec = glm::normalize(conhesionvec) * speed;
}
}
}
Separation function
void flockobj::Separationcal(std::vector<flockobj*> others) //seperation makes sure that our objects dont just all move the the center of mass
{
sperationvec = glm::vec3(0,0,0);
for(int i = 0; i < others.size(); i++)
{
float len = glm::length(others[i]->returnposition() - _position);
if(others[i] != this && len < radiousep)
{
glm::vec3 dist = _position - others[i]->returnposition();
if(glm::length(dist) != 0)
{
sperationvec += glm::normalize(dist)/glm::length(dist);
}
}
}
if(glm::length(sperationvec) > speed)
{
sperationvec = glm::normalize(sperationvec) * speed;
}
//sperationvec = glm::normalize(sperationvec);
}
thanks in advance.