# Collision Avoidance Question

## Recommended Posts

Steedie    122
Hey guys I'm trying to work on getting the collision avoidance working in my Crowd Simulation. Currently it works to the point where one will stop for the other let it pass then carry on when a certain distance parameter is met. However I want it to work so that the agent will move out the way so to speak so move around a sort of "repel sphere" to keep the distance. I dont really know how I'd go about this though
//================================================
void agent::avoidOtherAgents()
{
if(this->collisionAgent)
{
return;
}

for(int i = 0; i < NUM_AGENTS; i++)
{
if(this != glWindow->pAgent[i])
{
float distanceCheck = cVector.getDistance(glWindow->pAgent[i]->getPos(), this->getPos());

if(distanceCheck < 5.0f)
{

glWindow->pAgent[i]->collisionAgent = TRUE;
break;
}
else
{

glWindow->pAgent[i]->collisionAgent = FALSE;
}
}
}
}
//================================================

I made my own vector math library from pretty much the standard vector functions, add/subtract multiply/scalar multiply/divide and so on. But I dont know what I'd need to do to try and get my problem working Oh thought I'd add this, this is how the agents get their velocity, which I'm guessing is going to need to be changed to
//Subtract the currentPos from the targetPos
svect3 temp;
temp = cVector.vector_SUB(&targetList[index], &pos);
// Normalise the vector for use with the setVel
cVector.Normalise(&temp);

if(collisionAgent == FALSE)
setVel(temp.x / 2, temp.y / 2, temp.z);
else if(collisionAgent == TRUE)
setVel(0.0f, 0.0f, 0.0f);

Even if my code doesnt really help, does anyone know the rough idea of how to do it, or any tutorials/lessons on it? Many thanks

##### Share on other sites
Cowboy Coder    365
I'd avoid this "stopping", as it's very unnatural. Try to focus first on applying accelerations that move them around each other.

If (x,y) is the normalized vector from A to B, then (y,-x) will move A around B (and (-y,x) will move them in the other direction.

##### Share on other sites
Steedie    122
Ok cool I sort of understand, could you sort of point me in the right direction of how I'd implement that roughly to what I have now

Also the stopping was purely just so I could see that the colide flag was being set correctly :) Will definatley get rid of it once this works