2012-11-08 2 views
7

나는 한동안이 인공 지능 방법을 연구 해왔다. 벽이 플레이어에게 자신의 길을 막고 있다면 적의 방향에 따라 기본적으로 int이 있습니다. 이것은 대부분의 경우 작동하지 않습니다. 때로는 적군이 뚫을 수없는 갈라진 틈을 지나갈 수도 있습니다. 다른 시간에는 벽에 뚜렷한 간격이 생겨 붙어있을 것입니다. 내 코드를 첨부 하겠지만 너무 비효율적이거나 문제를 해결할 방법이 아니라면 내 방식을 완전히 바꾸지 않을 것입니다. 나는이 모든 것이 정상적으로 어떻게 수행되는지를 알고 싶다. 그래서 더 나은 (그리고 작동하는) 방법으로 구현할 수있다.게임 프로그래밍 ai : 플레이어를 찾기 위해 벽을 크기 조정 하시겠습니까?

내 코드 : 그것은 당신이 현재 구현하려고하는 것은 스티어링로 알려진 것 같습니다하지만, 이러한 일들이 일반적으로 수행하는 방식이 Pathfinding을 것

public void update(ArrayList<Wall> walls, Player p){ 

    findPlayer(p.getX(), p.getY()); 

    boolean isCollision = false; 
    System.out.println(isCollision); 
    //if movement straight towards the player is blocked, move along the walls 
    for(Wall w : walls){ 
     if(Helper.isBoundingBoxCollision((int)(x + vectorToPlayer.getDX() * SPEED), (int)(y + vectorToPlayer.getDY() * SPEED), width, height, w.getX(), w.getY(), w.width, w.height)){ 
      isCollision = true; 

      if(Math.abs(vectorToPlayer.getDX()) > Math.abs(vectorToPlayer.getDY())){ 
       if(vectorToPlayer.getDX() > 0) 
        WALL_COLLISION = 3; 
       else 
        WALL_COLLISION = 1; 
      } 
      else if(Math.abs(vectorToPlayer.getDX()) <  Math.abs(vectorToPlayer.getDY())){ 
       if(vectorToPlayer.getDY() > 0) 
        WALL_COLLISION = 0; 
       else 
        WALL_COLLISION = 2; 
      } 

     } 
    } 
    //System.out.println(isCollision); 
    //set the direction to the straight on vector, to be reset if there is a collision on this path 
    direction = vectorToPlayer; 

    if(isCollision){ 
     //reset the variable, don't mind that what this is named is completely opposite = PIMPIN' 
     isCollision = false; 

     //scale dem walls son, and see when the path is clear 
     for(Wall w : walls){ 
      if(WALL_COLLISION == 0 && !Helper.isBoundingBoxCollision(x + SPEED, y, width, height, w.getX(), w.getY(), w.width, w.height)){ 
       WALL_COLLISION = 3; 
       isCollision = true; 
      } 
      else if(WALL_COLLISION == 1 && !Helper.isBoundingBoxCollision(x, y + SPEED, width, height, w.getX(), w.getY(), w.width, w.height)){ 
       WALL_COLLISION--; 
       isCollision = true; 
      } 
      else if(WALL_COLLISION == 2 && !Helper.isBoundingBoxCollision(x - SPEED, y, width, height, w.getX(), w.getY(), w.width, w.height)){ 
       WALL_COLLISION--; 
       isCollision = true; 
      } 
      else if(WALL_COLLISION == 3 && !Helper.isBoundingBoxCollision(x, y - SPEED, width, height, w.getX(), w.getY(), w.width, w.height)){ 
       WALL_COLLISION--; 
       isCollision = true; 
      } 
     } 

     //if there is NOT a wall on the designated side, set the vector accoridingly 
     if(isCollision){ 
      if(WALL_COLLISION == 0) 
       direction = new NVector(0, 1); 
      else if(WALL_COLLISION == 1) 
       direction = new NVector(1, 0); 
      else if(WALL_COLLISION == 2) 
       direction = new NVector(0, -1); 
      else if(WALL_COLLISION == 3) 
       direction = new NVector(-1, 0); 
     } 
    } 
    x += Math.round(direction.getDX()*SPEED); 
    y += Math.round(direction.getDY()*SPEED); 
} 
+1

Eric B가 말한 것처럼 충돌 감지 기능이있는 조향 장치가 목적지에 도달하는 것이 보장되지 않습니다. 제안 된 Pathfinding 알고리즘으로 옮기는 문제는 알고리즘을 구현할 때 (A * 알고리즘이 여러 곳에서 문서화되어 있음) 적은 양이 될 것이지만, 그러한 알고리즘을 작동 시키려면 전 세계를 분리하는 데 어려움이있을 것입니다. isBoundingBoxCollision() 호출에서 엔진 자체를 활용하여 벽을 찾으려고합니다. 많은 게임은 이동 시스템을 쿼리하기가 쉬운 이동 메쉬로 레벨 지오메트리를 보강하여 지오메트리 분석의 복잡성을 피합니다. – Godeke

+0

확인해 주셔서 감사합니다. – ceptno

답변

3

. 사용하기로 결정한 것은 응용 프로그램에 따라 다릅니다. 스티어링은 장애물이있는 경우 방향을 바꾸면서 목표물쪽으로 움직이며 목적지까지 도달 할 수있는 것은 아닙니다. 경로 찾기는 일반적으로 웨이브 포인트 또는 웨이브 포인트를 트래킹하기 위해 an algorithm such as Dijkstra's을 사용하여 그래프를 구성하여 수행 할 수 있습니다.

+0

Mat Buckland의 [Programming Game AI by Example] (http://www.amazon.com/Programming-Game-Example-Mat-Buckland/dp/1556220782)을 참조하십시오. 그것은 아주 좋은 예제, 작업 코드를 가지고 있으며 State Machine, Steering Behaviors, Path Finding 등을 포함합니다. –

+0

감사합니다. 에릭 B, AI에서 그런 종류의 "멍청한"느낌을 유지하면서 더 나은 시스템을 구현할 것입니다. – ceptno

+0

가보 켓, 귀하의 제안에 감사드립니다, 나는 그것을 데리러 올거야. – ceptno

관련 문제