# Agent collision avoidance java

I am working with a project which is a agent based pedestrian simulation in Java and its is animated with the help of JavaFX. I've tried to read all the social force model papers but my understanding of those articles are none. So I tried an own approach which got trashed after failing time after time.

My approach was that each agent calculated its surrounding and first calculated the distance to each of the agents on the field and if that distance was below a constant then the agent calculates the angle of that agent which is too close to it and moves accordingly to the calculated angle.

This approach didnt work for me because the "avoidance code" is not efficient enough and the agents just dont know where to go when they meet and just stays in place.

I am asking for guidance to how I can approach this problem in a better way.

double[] check(Vector<Pedestrian> peds, Pedestrian p1){
for (Pedestrian p : peds){
if (p.getPedestrianId() != this.id){
double distance = IPedestrian.distance_formula(getTranslateX(), getTranslateY(), p.getTranslateX(), p.getTranslateY());
if (distance <= DANGER){
System.out.println("DANGER");
return IPedestrian.angle(getTranslateX(), getTranslateY(), p.getTranslateX(), p.getTranslateY(), p1);
}
}
}
return new double[] {SPEED, 0};


}

public void move(Vector<Pedestrian> peds, Pedestrian p) {
double[] new_steps = this.check(peds, p);
if (side == SideChooser.Left){
setTranslateX(getTranslateX() + new_steps[0]);
setTranslateY(getTranslateY() + new_steps[1]);
} else {
setTranslateX(getTranslateX() - new_steps[0]);
setTranslateY(getTranslateY() - new_steps[1]);
}


}

Math formulas:

static double distance_formula(double thisX, double thisY, double otherX, double otherY){
return Math.sqrt(Math.pow(otherX - thisX, 2) + Math.pow(otherY - thisY, 2));


}

static double[] angle(double x1, double y1, double x2, double y2, Pedestrian p){
double angle = Math.toDegrees(Math.atan2(y2-y1, x2-x1));
angle += Math.ceil(-angle/360) * 360;

//double angle = Math.toDegrees(Math.atan2(y2-y1, x2-x1));

if (p.getSideChoosen() == SideChooser.Left){//if the pedestrian is from the left side
if (angle < 45 || angle > 315)//front
return new double[]{-SPEED/5, 0};

else if (angle >= 135 || angle <= 225 ) //back
return new double[]{SPEED*1.4, 0};

else if (angle >= 45 || angle <= 90)//North-East
return new double[]{0, SPEED};

else if (angle > 90 || angle <= 135) //North-West
return new double[]{SPEED*1.2 , SPEED};

else if (angle >= 270 || angle <= 315) //South-East
return new double[]{0, -SPEED};

else if (angle > 225 || angle <= 270) //South-West
return new double[]{SPEED*1.2, -SPEED};

else
return new double[]{SPEED, 0};
} else {
if (angle < 45 || angle > 315)//back
return new double[]{SPEED*1.4, 0};

else if (angle >= 135 || angle <= 225 ) //front
return new double[]{-SPEED/5, 0};

else if (angle >= 45 || angle <= 90)//North-West
return new double[]{SPEED*1.2, -SPEED};

else if (angle > 90 || angle <= 135) //North-East
return new double[]{0 , -SPEED};

else if (angle >= 270 || angle <= 315) //South-West
return new double[]{SPEED*1.2, SPEED};

else if (angle > 225 || angle <= 270) //South-East
return new double[]{0, SPEED};

else
return new double[]{SPEED, 0};
}


}