2016-04-17 14 views
0

私の小さなゲームの種類のための衝突検出システムです。それは、プレーヤーがいつ壁にまっすぐ進むかを伝えることができます。しかし、プレーヤーが斜めになると、壁を通ってクリップします。問題がどこで発生しているのかは分かりません。対角線移動の衝突検出

以下のコードは、プレーヤを扱うクラスからのものである:

public void run() 
    { 
     running = true; 

     while(running) 
     { 

      //Get the key/s pressed 
      controls.update(); 


      //Move down 
      if (controls.down) 
      { 
       movePlayerDown(this.thisPlayer.getSpeed()); 
      } 
      //Move up 
      if (controls.up) 
      { 
       movePlayerUp(this.thisPlayer.getSpeed()); 
      } 

      //Move right 
      if (controls.right) 
      { 
       movePlayerRight(this.thisPlayer.getSpeed()); 
      } 
      //Move left 
      else if (controls.left) 
      { 
       movePlayerLeft(this.thisPlayer.getSpeed()); 
      } 



      try 
      { 
       Thread.sleep(this.sleep); 
      } 
      catch (InterruptedException e) 
      { 
       e.printStackTrace(); 
      } 

     } 
    } 


    public void movePlayerRight(int dx) 
    { 
     //Gets the tile at what would be the new coordinates of the player 
     Tile tile = currentLevel.getTile(this.thisPlayer.getX() + dx + this.thisPlayer.getWidth(), this.thisPlayer.getY()); 
     //IF the tile is null or not solid then the player is actually moved 
     if (tile == null || !tile.isSolid()) 
     { 
      this.thisPlayer.setX(this.thisPlayer.getX() + dx); 
     } 
    } 


    public void movePlayerLeft(int dx) 
    { 
     //Gets the tile at what would be the new coordinates of the player 
     Tile tile = currentLevel.getTile(this.thisPlayer.getX() - dx, this.thisPlayer.getY()); 
     //IF the tile is null or not solid then the player is actually moved 
     if (tile == null || !tile.isSolid()) 
     { 
      this.thisPlayer.setX(this.thisPlayer.getX() - dx); 
     } 
    } 


    public void movePlayerDown(int dy) 
    { 
     //Gets the tile at what would be the new coordinates of the player 
     Tile tile = currentLevel.getTile(this.thisPlayer.getX(), this.thisPlayer.getY() + dy + this.thisPlayer.getHeight()); 
     //IF the tile is null or not solid then the player is actually moved 
     if (tile == null || !tile.isSolid()) 
     { 
      this.thisPlayer.setY(this.thisPlayer.getY() + dy); 
     } 
    } 


    public void movePlayerUp(int dy) 
    { 
     //Gets the tile at what would be the new coordinates of the player 
     Tile tile = currentLevel.getTile(this.thisPlayer.getX(), this.thisPlayer.getY() - dy); 
     //IF the tile is null or not solid then the player is actually moved 
     if (tile == null || !tile.isSolid()) 
     { 
      this.thisPlayer.setY(this.thisPlayer.getY() - dy); 
     } 
    } 

コードの次のブロックがgetTile()メソッドである:

public Tile getTile(int x, int y) 
    { 
     for (int r = 0; r < worldTiles.length; r++) 
     { 
      for (int c = 0; c < worldTiles[r].length; c++) 
      { 
       if (worldTiles[r][c] != null) 
       { 
        int right = worldTiles[r][c].getX() + worldTiles[r][c].getWidth(); 
        int bottom = worldTiles[r][c].getY() + worldTiles[r][c].getHeight(); 

        if (x > worldTiles[r][c].getX() && x < right && y > worldTiles[r][c].getY() && y < bottom) 
        { 
         return worldTiles[r][c]; 
        } 
       } 
      } 
     } 
+0

あなたは衝突検出コードを追加するのを忘れていますが、そこに問題があります。投稿してください。また、どのように斜めに動いていますか?上記のコードは、上下左右のコントロールのみを示しています。 – 11thdimension

+0

ユーザーが上キーと右キーを保持している場合、プレーヤーは斜め方向に動きます。 – Hobbs2000

+0

プログラムの観点から見ると、まだ 'up'キーと' right'キーの組み合わせです。速くて斜めに動いているように見えますが、速くてもCPUには関係ありません。つまり、衝突検出コードにエラーがあります。これは、 'up'の後に' right'キーが続くか、またはその逆を扱うことができません。 – 11thdimension

答えて

0

A)どこ条件を定義いけませんタイル==ヌル - それ以上検討するため

b)各イベントをテストするロジックは、次のとおりです - 一度に1つのキー

 //Move down 
     if (down) 
     { 
      movePlayerDown(this.thisPlayer.getSpeed()); 
     } 
     //Move up 
     else if (up) 
     { 
      movePlayerUp(this.thisPlayer.getSpeed()); 
     } 

     //Move right 
     else if (right) 
     { 
      movePlayerRight(this.thisPlayer.getSpeed()); 
     } 
     //Move left 
     else if (left) 
     { 
      movePlayerLeft(this.thisPlayer.getSpeed()); 
     } 

スリープは、プログラムとイベントが不足している可能性があるため、そこから削除する必要があります。

c)イベントは、キーイベントハンドラから起動され、そこに移動してフェッチする必要がありません。次のように

実際に私は、全体の論理を逆になります

void sendEvent(event) { 
    if(event==down) ...... 


....... 

} 

があなたのwhileループを置き換え

void keyPressed(event) { 
    sendEvent(event) 
}