Answers.Body: <p>A very efficient approach to what you are trying to do is <em>velocity obstacles</em>.</p>
  
  <p>Assuming two agents use constant velocity motion vectors, a velocity obstacle models a geometric region in which if the endpoint of the velocity vector for agent 1 falls, it will collide with agent 2 (and vice versa). Hence you can predict what velocity vectors will lead to collisions between the agents and choose velocity vectors that do not collide.</p>
  
  <p>There are very good examples and tutorials here:
  <a href="http://gamma.cs.unc.edu/RVO/" rel="nofollow noreferrer">http://gamma.cs.unc.edu/RVO/</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-23T16:08:52.917Z
Answers.LastActivityDate: 2018-01-23T16:08:52.917Z
Answers.UserDisplayName: thayne
Body: <p>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.</p>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>I am asking for guidance to how I can approach this problem in a better way.</p>
  
  <pre><code>double[] check(Vector&lt;Pedestrian&gt; 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 &lt;= DANGER){
              System.out.println("DANGER");
              return IPedestrian.angle(getTranslateX(), getTranslateY(), p.getTranslateX(), p.getTranslateY(), p1);
          }
      }
  }
  return new double[] {SPEED, 0};
  </code></pre>
  
  <p>}</p>
  
  <pre><code>public void move(Vector&lt;Pedestrian&gt; 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]);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Math formulas: </p>
  
  <pre><code>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));
  </code></pre>
  
  <p>}</p>
  
  <pre><code>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 &lt; 45 || angle &gt; 315)//front
          return new double[]{-SPEED/5, 0};
  
      else if (angle &gt;= 135 || angle &lt;= 225 ) //back
          return new double[]{SPEED*1.4, 0};
  
      else if (angle &gt;= 45 || angle &lt;= 90)//North-East
          return new double[]{0, SPEED};
  
      else if (angle &gt; 90 || angle &lt;= 135) //North-West
          return new double[]{SPEED*1.2 , SPEED};
  
      else if (angle &gt;= 270 || angle &lt;= 315) //South-East
          return new double[]{0, -SPEED};
  
      else if (angle &gt; 225 || angle &lt;= 270) //South-West
          return new double[]{SPEED*1.2, -SPEED};
  
      else
          return new double[]{SPEED, 0};
  } else {
      if (angle &lt; 45 || angle &gt; 315)//back
          return new double[]{SPEED*1.4, 0};
  
      else if (angle &gt;= 135 || angle &lt;= 225 ) //front
          return new double[]{-SPEED/5, 0};
  
      else if (angle &gt;= 45 || angle &lt;= 90)//North-West
          return new double[]{SPEED*1.2, -SPEED};
  
      else if (angle &gt; 90 || angle &lt;= 135) //North-East
          return new double[]{0 , -SPEED};
  
      else if (angle &gt;= 270 || angle &lt;= 315) //South-West
          return new double[]{SPEED*1.2, SPEED};
  
      else if (angle &gt; 225 || angle &lt;= 270) //South-East
          return new double[]{0, SPEED};
  
      else
          return new double[]{SPEED, 0};
  }
  </code></pre>
  
  <p>}</p>
  
Tags: <multi-agent-systems>
Title: Agent collision avoidance java
UserDisplayName: Roffe
ThreadText: # Agent collision avoidance java
  
  (Asked by: username_0 on 2018-01-23)
  
  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 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 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};
  }
  
  }
  
  **Tags:** multi-agent-systems
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-01-23. Score: 2
  
  A very efficient approach to what you are trying to do is *velocity obstacles*.
  
  Assuming two agents use constant velocity motion vectors, a velocity obstacle models a geometric region in which if the endpoint of the velocity vector for agent 1 falls, it will collide with agent 2 (and vice versa). Hence you can predict what velocity vectors will lead to collisions between the agents and choose velocity vectors that do not collide.
  
  There are very good examples and tutorials here:
  <http://gamma.cs.unc.edu/RVO/>
  
  
  ---
  
  