2016-04-17 68 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

一個)你不限定的條件,其中平鋪== null - 這是進一步考慮的

b)測試每個事件的邏輯如下 - 一次一個鍵

 //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 keyPressed(event) { 
    sendEvent(event) 
} 

其中

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


....... 

} 

替換while循環。