pathfinding (Android Studio + libgdx)










0















So my problem is that, I created a turn-based game for two player with Android Studio and libgdx AI package, at this point I can manage to move my players, the only problem that annoy me is that the enemy who plays have a target that's the nearby player, and I can manage to move it up to its target but when it encounters an obstacle it doesn't circumvent it, I've search on internet for path-finding algorithms, but I can't apply them on my game



There the code of the enemy class



public class Enemy extends Entities 


private static final Logger log = new Logger(Enemy.class.getSimpleName(), Logger.DEBUG);

public Player target;
public float movement = GameConfig.ENEMY_MOVEMENTS;


//Obligatoire pour le JSON
public Enemy()


public Enemy(float x, float y, float width, float height,Color color)
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
target= new Player();


/**
* Cette méthode vérifie si l'ennemie est en collision avec soit un obstacle(Murs, caisses) ou un joueur
*/
public boolean isCollidingObstacle(float x, float y, Array<ObstacleBase> obstacles, Array<Enemy> enemies, Player player1, Player player2, int index)

for (ObstacleBase obstacle : obstacles)
if (x >= obstacle.getBounds().getX() && y >= obstacle.getBounds().getY())
if (x <= obstacle.getBounds().getWidth() + obstacle.getBounds().getX() - 1 && y <= obstacle.getBounds().getHeight() + obstacle.getBounds().getY() - 1)
//log.debug("Un obstacle se trouve en " + obstacle.getBounds().getX() + " " + obstacle.getBounds().getY());
return true;




int i = 0;
for (Enemy enemy : enemies)

if (i == index)

else
if (x == enemy.getX() && y == enemy.getY())
return true;


i++;

if (x == player1.getX() && y == player1.getY())
return true;

if (x == player2.getX() && y == player2.getY())
return true;

return false;


/**
* Cette méthode permet de vérifier si l'ennemie se trouve à côté de la cible
* @return
*/
public boolean nextToTarget()

/**
* Cette méthode permet de savoir quel joueur est le plus proche et de le mettre en target pour l'ennemie
*
* @param player1
* @param player2
*/
public void nearPlayer(Player player1, Player player2)
float diffXP1 = (this.getX() - player1.getX());
float diffXP2 = (this.getX() - player2.getX());
float diffYP1 = (this.getY() - player1.getY());
float diffYP2 = (this.getY() - player2.getY());

if (diffXP1 < 0)
diffXP1 *= -1;

if (diffXP2 < 0)
diffXP2 *= -1;

if (diffYP1 < 0)
diffYP1 *= -1;

if (diffYP2 < 0)
diffYP2 *= -1;

float diffP1=diffYP1-diffXP1;
float diffP2=diffYP2-diffXP2;

if(diffP1<0)
diffP1 *= -1;

if(diffP2<0)
diffP2 *= -1;


if(diffP1<diffP2)
target.setX(player1.getX());
target.setY(player1.getY());
else
target.setX(player2.getX());
target.setY(player2.getY());




public void update(final Array<ObstacleBase> obstacles, final Array<Enemy> enemies, Player player1, Player player2, int index)
move(obstacles, enemies, player1, player2, index);
nearPlayer(player1,player2);



public void move(final Array<ObstacleBase> obstacles, final Array<Enemy> enemies, Player player1, Player player2, int index) movements_done == 0)
if (target.getX() > this.getX())
if (!(isCollidingObstacle(getX() + 1, getY(), obstacles, enemies, player1, player2, index)))
movements_done += 1;
setX(getX() + 1);

else if (target.getX() < this.getX())
if (!(isCollidingObstacle(getX() - 1, getY(), obstacles, enemies, player1, player2, index)))
movements_done += 1;
setX(getX() - 1);



if (target.getY() > this.getY())
if (!(isCollidingObstacle(getX(), getY() + 1, obstacles, enemies, player1, player2, index)))
movements_done += 1;
setY(getY() + 1);

else if (target.getY() < this.getY())
if (!(isCollidingObstacle(getX(), getY() - 1, obstacles, enemies, player1, player2, index)))
movements_done += 1;
setY(getY() - 1);








I want to clarify that I also looked in the libgdx AI doc for the path-finding but even then I don't understand how to apply it to the game.



For more precision or code, I'm at your disposal.










share|improve this question




























    0















    So my problem is that, I created a turn-based game for two player with Android Studio and libgdx AI package, at this point I can manage to move my players, the only problem that annoy me is that the enemy who plays have a target that's the nearby player, and I can manage to move it up to its target but when it encounters an obstacle it doesn't circumvent it, I've search on internet for path-finding algorithms, but I can't apply them on my game



    There the code of the enemy class



    public class Enemy extends Entities 


    private static final Logger log = new Logger(Enemy.class.getSimpleName(), Logger.DEBUG);

    public Player target;
    public float movement = GameConfig.ENEMY_MOVEMENTS;


    //Obligatoire pour le JSON
    public Enemy()


    public Enemy(float x, float y, float width, float height,Color color)
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.color = color;
    target= new Player();


    /**
    * Cette méthode vérifie si l'ennemie est en collision avec soit un obstacle(Murs, caisses) ou un joueur
    */
    public boolean isCollidingObstacle(float x, float y, Array<ObstacleBase> obstacles, Array<Enemy> enemies, Player player1, Player player2, int index)

    for (ObstacleBase obstacle : obstacles)
    if (x >= obstacle.getBounds().getX() && y >= obstacle.getBounds().getY())
    if (x <= obstacle.getBounds().getWidth() + obstacle.getBounds().getX() - 1 && y <= obstacle.getBounds().getHeight() + obstacle.getBounds().getY() - 1)
    //log.debug("Un obstacle se trouve en " + obstacle.getBounds().getX() + " " + obstacle.getBounds().getY());
    return true;




    int i = 0;
    for (Enemy enemy : enemies)

    if (i == index)

    else
    if (x == enemy.getX() && y == enemy.getY())
    return true;


    i++;

    if (x == player1.getX() && y == player1.getY())
    return true;

    if (x == player2.getX() && y == player2.getY())
    return true;

    return false;


    /**
    * Cette méthode permet de vérifier si l'ennemie se trouve à côté de la cible
    * @return
    */
    public boolean nextToTarget()

    /**
    * Cette méthode permet de savoir quel joueur est le plus proche et de le mettre en target pour l'ennemie
    *
    * @param player1
    * @param player2
    */
    public void nearPlayer(Player player1, Player player2)
    float diffXP1 = (this.getX() - player1.getX());
    float diffXP2 = (this.getX() - player2.getX());
    float diffYP1 = (this.getY() - player1.getY());
    float diffYP2 = (this.getY() - player2.getY());

    if (diffXP1 < 0)
    diffXP1 *= -1;

    if (diffXP2 < 0)
    diffXP2 *= -1;

    if (diffYP1 < 0)
    diffYP1 *= -1;

    if (diffYP2 < 0)
    diffYP2 *= -1;

    float diffP1=diffYP1-diffXP1;
    float diffP2=diffYP2-diffXP2;

    if(diffP1<0)
    diffP1 *= -1;

    if(diffP2<0)
    diffP2 *= -1;


    if(diffP1<diffP2)
    target.setX(player1.getX());
    target.setY(player1.getY());
    else
    target.setX(player2.getX());
    target.setY(player2.getY());




    public void update(final Array<ObstacleBase> obstacles, final Array<Enemy> enemies, Player player1, Player player2, int index)
    move(obstacles, enemies, player1, player2, index);
    nearPlayer(player1,player2);



    public void move(final Array<ObstacleBase> obstacles, final Array<Enemy> enemies, Player player1, Player player2, int index) movements_done == 0)
    if (target.getX() > this.getX())
    if (!(isCollidingObstacle(getX() + 1, getY(), obstacles, enemies, player1, player2, index)))
    movements_done += 1;
    setX(getX() + 1);

    else if (target.getX() < this.getX())
    if (!(isCollidingObstacle(getX() - 1, getY(), obstacles, enemies, player1, player2, index)))
    movements_done += 1;
    setX(getX() - 1);



    if (target.getY() > this.getY())
    if (!(isCollidingObstacle(getX(), getY() + 1, obstacles, enemies, player1, player2, index)))
    movements_done += 1;
    setY(getY() + 1);

    else if (target.getY() < this.getY())
    if (!(isCollidingObstacle(getX(), getY() - 1, obstacles, enemies, player1, player2, index)))
    movements_done += 1;
    setY(getY() - 1);








    I want to clarify that I also looked in the libgdx AI doc for the path-finding but even then I don't understand how to apply it to the game.



    For more precision or code, I'm at your disposal.










    share|improve this question


























      0












      0








      0








      So my problem is that, I created a turn-based game for two player with Android Studio and libgdx AI package, at this point I can manage to move my players, the only problem that annoy me is that the enemy who plays have a target that's the nearby player, and I can manage to move it up to its target but when it encounters an obstacle it doesn't circumvent it, I've search on internet for path-finding algorithms, but I can't apply them on my game



      There the code of the enemy class



      public class Enemy extends Entities 


      private static final Logger log = new Logger(Enemy.class.getSimpleName(), Logger.DEBUG);

      public Player target;
      public float movement = GameConfig.ENEMY_MOVEMENTS;


      //Obligatoire pour le JSON
      public Enemy()


      public Enemy(float x, float y, float width, float height,Color color)
      this.x = x;
      this.y = y;
      this.width = width;
      this.height = height;
      this.color = color;
      target= new Player();


      /**
      * Cette méthode vérifie si l'ennemie est en collision avec soit un obstacle(Murs, caisses) ou un joueur
      */
      public boolean isCollidingObstacle(float x, float y, Array<ObstacleBase> obstacles, Array<Enemy> enemies, Player player1, Player player2, int index)

      for (ObstacleBase obstacle : obstacles)
      if (x >= obstacle.getBounds().getX() && y >= obstacle.getBounds().getY())
      if (x <= obstacle.getBounds().getWidth() + obstacle.getBounds().getX() - 1 && y <= obstacle.getBounds().getHeight() + obstacle.getBounds().getY() - 1)
      //log.debug("Un obstacle se trouve en " + obstacle.getBounds().getX() + " " + obstacle.getBounds().getY());
      return true;




      int i = 0;
      for (Enemy enemy : enemies)

      if (i == index)

      else
      if (x == enemy.getX() && y == enemy.getY())
      return true;


      i++;

      if (x == player1.getX() && y == player1.getY())
      return true;

      if (x == player2.getX() && y == player2.getY())
      return true;

      return false;


      /**
      * Cette méthode permet de vérifier si l'ennemie se trouve à côté de la cible
      * @return
      */
      public boolean nextToTarget()

      /**
      * Cette méthode permet de savoir quel joueur est le plus proche et de le mettre en target pour l'ennemie
      *
      * @param player1
      * @param player2
      */
      public void nearPlayer(Player player1, Player player2)
      float diffXP1 = (this.getX() - player1.getX());
      float diffXP2 = (this.getX() - player2.getX());
      float diffYP1 = (this.getY() - player1.getY());
      float diffYP2 = (this.getY() - player2.getY());

      if (diffXP1 < 0)
      diffXP1 *= -1;

      if (diffXP2 < 0)
      diffXP2 *= -1;

      if (diffYP1 < 0)
      diffYP1 *= -1;

      if (diffYP2 < 0)
      diffYP2 *= -1;

      float diffP1=diffYP1-diffXP1;
      float diffP2=diffYP2-diffXP2;

      if(diffP1<0)
      diffP1 *= -1;

      if(diffP2<0)
      diffP2 *= -1;


      if(diffP1<diffP2)
      target.setX(player1.getX());
      target.setY(player1.getY());
      else
      target.setX(player2.getX());
      target.setY(player2.getY());




      public void update(final Array<ObstacleBase> obstacles, final Array<Enemy> enemies, Player player1, Player player2, int index)
      move(obstacles, enemies, player1, player2, index);
      nearPlayer(player1,player2);



      public void move(final Array<ObstacleBase> obstacles, final Array<Enemy> enemies, Player player1, Player player2, int index) movements_done == 0)
      if (target.getX() > this.getX())
      if (!(isCollidingObstacle(getX() + 1, getY(), obstacles, enemies, player1, player2, index)))
      movements_done += 1;
      setX(getX() + 1);

      else if (target.getX() < this.getX())
      if (!(isCollidingObstacle(getX() - 1, getY(), obstacles, enemies, player1, player2, index)))
      movements_done += 1;
      setX(getX() - 1);



      if (target.getY() > this.getY())
      if (!(isCollidingObstacle(getX(), getY() + 1, obstacles, enemies, player1, player2, index)))
      movements_done += 1;
      setY(getY() + 1);

      else if (target.getY() < this.getY())
      if (!(isCollidingObstacle(getX(), getY() - 1, obstacles, enemies, player1, player2, index)))
      movements_done += 1;
      setY(getY() - 1);








      I want to clarify that I also looked in the libgdx AI doc for the path-finding but even then I don't understand how to apply it to the game.



      For more precision or code, I'm at your disposal.










      share|improve this question
















      So my problem is that, I created a turn-based game for two player with Android Studio and libgdx AI package, at this point I can manage to move my players, the only problem that annoy me is that the enemy who plays have a target that's the nearby player, and I can manage to move it up to its target but when it encounters an obstacle it doesn't circumvent it, I've search on internet for path-finding algorithms, but I can't apply them on my game



      There the code of the enemy class



      public class Enemy extends Entities 


      private static final Logger log = new Logger(Enemy.class.getSimpleName(), Logger.DEBUG);

      public Player target;
      public float movement = GameConfig.ENEMY_MOVEMENTS;


      //Obligatoire pour le JSON
      public Enemy()


      public Enemy(float x, float y, float width, float height,Color color)
      this.x = x;
      this.y = y;
      this.width = width;
      this.height = height;
      this.color = color;
      target= new Player();


      /**
      * Cette méthode vérifie si l'ennemie est en collision avec soit un obstacle(Murs, caisses) ou un joueur
      */
      public boolean isCollidingObstacle(float x, float y, Array<ObstacleBase> obstacles, Array<Enemy> enemies, Player player1, Player player2, int index)

      for (ObstacleBase obstacle : obstacles)
      if (x >= obstacle.getBounds().getX() && y >= obstacle.getBounds().getY())
      if (x <= obstacle.getBounds().getWidth() + obstacle.getBounds().getX() - 1 && y <= obstacle.getBounds().getHeight() + obstacle.getBounds().getY() - 1)
      //log.debug("Un obstacle se trouve en " + obstacle.getBounds().getX() + " " + obstacle.getBounds().getY());
      return true;




      int i = 0;
      for (Enemy enemy : enemies)

      if (i == index)

      else
      if (x == enemy.getX() && y == enemy.getY())
      return true;


      i++;

      if (x == player1.getX() && y == player1.getY())
      return true;

      if (x == player2.getX() && y == player2.getY())
      return true;

      return false;


      /**
      * Cette méthode permet de vérifier si l'ennemie se trouve à côté de la cible
      * @return
      */
      public boolean nextToTarget()

      /**
      * Cette méthode permet de savoir quel joueur est le plus proche et de le mettre en target pour l'ennemie
      *
      * @param player1
      * @param player2
      */
      public void nearPlayer(Player player1, Player player2)
      float diffXP1 = (this.getX() - player1.getX());
      float diffXP2 = (this.getX() - player2.getX());
      float diffYP1 = (this.getY() - player1.getY());
      float diffYP2 = (this.getY() - player2.getY());

      if (diffXP1 < 0)
      diffXP1 *= -1;

      if (diffXP2 < 0)
      diffXP2 *= -1;

      if (diffYP1 < 0)
      diffYP1 *= -1;

      if (diffYP2 < 0)
      diffYP2 *= -1;

      float diffP1=diffYP1-diffXP1;
      float diffP2=diffYP2-diffXP2;

      if(diffP1<0)
      diffP1 *= -1;

      if(diffP2<0)
      diffP2 *= -1;


      if(diffP1<diffP2)
      target.setX(player1.getX());
      target.setY(player1.getY());
      else
      target.setX(player2.getX());
      target.setY(player2.getY());




      public void update(final Array<ObstacleBase> obstacles, final Array<Enemy> enemies, Player player1, Player player2, int index)
      move(obstacles, enemies, player1, player2, index);
      nearPlayer(player1,player2);



      public void move(final Array<ObstacleBase> obstacles, final Array<Enemy> enemies, Player player1, Player player2, int index) movements_done == 0)
      if (target.getX() > this.getX())
      if (!(isCollidingObstacle(getX() + 1, getY(), obstacles, enemies, player1, player2, index)))
      movements_done += 1;
      setX(getX() + 1);

      else if (target.getX() < this.getX())
      if (!(isCollidingObstacle(getX() - 1, getY(), obstacles, enemies, player1, player2, index)))
      movements_done += 1;
      setX(getX() - 1);



      if (target.getY() > this.getY())
      if (!(isCollidingObstacle(getX(), getY() + 1, obstacles, enemies, player1, player2, index)))
      movements_done += 1;
      setY(getY() + 1);

      else if (target.getY() < this.getY())
      if (!(isCollidingObstacle(getX(), getY() - 1, obstacles, enemies, player1, player2, index)))
      movements_done += 1;
      setY(getY() - 1);








      I want to clarify that I also looked in the libgdx AI doc for the path-finding but even then I don't understand how to apply it to the game.



      For more precision or code, I'm at your disposal.







      android libgdx






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 19:52









      Al Fahad

      725822




      725822










      asked Nov 15 '18 at 19:20









      KaanayciKaanayci

      12




      12






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326555%2fpathfinding-android-studio-libgdx%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326555%2fpathfinding-android-studio-libgdx%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線