I am trying to make boids simulation. What I am currently doing is checking if boids are in each others range and adding their memoery address to a std::vector
called withinSensoryRange
if they are. Here is the code.
struct Boid
{
float sensoryRadius = 50.0f;
std::vector<Boid*> withinSensoryRange;
};
std::vector<Boid> boids;
while (true)
{
for (int i = 0; i < boids.size(); i++)
for (int j = i; j < boids.size(); j++)
{
float distance = sqrtf((boids[i].position.x - boids[j].position.x) * (boids[i].position.x - boids[j].position.x) +
(boids[i].position.y - boids[j].position.y) * (boids[i].position.y - boids[j].position.y));
if (distance > boids[i].sensoryRadius)
{
boids[i].withinSensoryRange.push_back(&boids[j]);
boids[j].withinSensoryRange.push_back(&boids[i]);
}
}
}
My problem is that it just keeps adding to the vector continuously every frame as long as they are within range. Is there a way to detect if it already is in the vector and don't add it if it is? Thanks.