2013-08-01 2 views
0

지금 안드로이드 게임을 작성 중이며 화면에 벽 충돌시 도움이 필요합니다. 나는 상단과 오른쪽에서 공을 드래그하면 그것은 수 벽에 충돌하는하지만 난 빠르게 드래그 할 때 벽안드로이드 비트 맵 : 충돌 감지

public boolean onTouchEvent(MotionEvent event) { 
      int x = (int) event.getX(); 
      int y = (int) event.getY(); 
      switch (event.getAction()) { 
      // if the player moves 
      case MotionEvent.ACTION_MOVE: { 
       if (playerTouchRect.contains(x, y)) { 
        boolean left = false; 
        boolean right = false; 
        boolean up = false; 
        boolean down = false; 
        boolean canMove = false; 
        boolean foundFinish = false; 
        if (x != pLastXPos) { 
         if (x < pLastXPos) { 
          left = true; 
         } else { 
          right = true; 
         } 
         pLastXPos = x; 
        } 
        if (y != pLastYPos) { 
         if (y < pLastYPos) { 
          up = true; 
         } else { 
          down = true; 
         } 
         pLastYPos = y; 
        } 

        plCellRect = getRectFromPos(x, y); 
        newplRect.set(playerRect); 
        newplRect.left = x - (int) (playerRect.width()/2); 
        newplRect.right = x + (int) (playerRect.width()/2); 
        newplRect.top = y - (int) (playerRect.height()/2); 
        newplRect.bottom = y + (int) (playerRect.height()/2); 
        int currentRow = 0; 
        int currentCol = 0; 
        currentRow = getRowFromYPos(newplRect.top); 
        currentCol = getColFromXPos(newplRect.right); 



        if(!canMove){ 
        canMove = mapManager.getCurrentTile().pMaze[currentRow][currentCol] == Cell.wall; 
        canMove =true; 
        } 
        finishTest = mapManager.getCurrentTile().pMaze[currentRow][currentCol]; 
        foundA = finishTest == Cell.valueOf(letterNotGet + ""); 



        canMove = mapManager.getCurrentTile().pMaze[currentRow][currentCol] != Cell.wall; 
        canMove = (finishTest == Cell.floor || finishTest == Cell.pl) && canMove; 
        if (canMove) { 
         invalidate(); 
         setTitle(); 
        } 
        if (foundA) { 
         mapManager.getCurrentTile().pMaze[currentRow][currentCol] = Cell.floor; 
         // finishTest 
         letterGotten.add(letterNotGet); 
         playCurrentLetter(); 
         /*sounds.play(sExplosion, 1.0f, 1.0f, 0, 0, 1.5f);*/ 
         foundS = letterNotGet == 's'; 
         letterNotGet++; 
        }if(foundS){ 
         AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity); 
         builder.setTitle(mainActivity.getText(R.string.finished_title)); 
         LayoutInflater inflater = mainActivity.getLayoutInflater(); 
         View view = inflater.inflate(R.layout.finish, null); 
         builder.setView(view); 
         View closeButton =view.findViewById(R.id.closeGame); 
         closeButton.setOnClickListener(new OnClickListener() { 
          @Override 
          public void onClick(View clicked) { 
           if(clicked.getId() == R.id.closeGame) { 
            mainActivity.finish(); 
           } 
          } 
         }); 
         AlertDialog finishDialog = builder.create(); 
         finishDialog.show(); 
        } 
        else { 
         Log.d(TAG, "INFO: updated player position"); 
         playerRect.set(newplRect); 
         setTouchZone(); 
         updatePlayerCell(); 
        } 
       } // end of (CASE) if playerTouch 
       break; 
      } // end of (SWITCH) Case motion 
      }//end of Switch 
      return true; 
     }//end of TouchEvent 
     private void finish() { 
      // TODO Auto-generated method stub 
     } 
     public int getColFromXPos(int xPos) { 
      val = xPos/(pvWidth/mapManager.getCurrentTile().pCols); 
      if (val == mapManager.getCurrentTile().pCols) { 
       val = mapManager.getCurrentTile().pCols - 1; 
      } 
      return val; 
     } 
     /** 
     * Given a y pixel position, return the row of the cell it is in This is 
     * used when determining the type of adjacent Cells. 
     * 
     * @param yPos 
     *   y position in pixels 
     * @return The cell this position is in 
     */ 
     public int getRowFromYPos(int yPos) { 
      val = yPos/(pvHeight/mapManager.getCurrentTile().pRows); 
      if (val == mapManager.getCurrentTile().pRows) { 
       val = mapManager.getCurrentTile().pRows - 1; 
      } 
      return val; 
     } 
     /** 
     * When preserving the position we need to know which cell the player is in, 
     * so calculate it from the centre on its Rect 
     */ 
     public void updatePlayerCell() { 
      plCell.x = (playerRect.left + (playerRect.width()/2)) 
        /(pvWidth/mapManager.getCurrentTile().pCols); 
      plCell.y = (playerRect.top + (playerRect.height()/2)) 
        /(pvHeight/mapManager.getCurrentTile().pRows); 
      if (mapManager.getCurrentTile().pMaze[plCell.y][plCell.x] == Cell.floor) { 
       for (int row = 0; row < mapManager.getCurrentTile().pRows; row++) { 
        for (int col = 0; col < mapManager.getCurrentTile().pCols; col++) { 
         if (mapManager.getCurrentTile().pMaze[row][col] == Cell.pl) { 
          mapManager.getCurrentTile().pMaze[row][col] = Cell.floor; 
          break; 
         } 
        } 
       } 
       mapManager.getCurrentTile().pMaze[plCell.y][plCell.x] = Cell.pl; 
      } 
     } 
     public Rect getRectFromPos(int x, int y) { 
      calcCell.left = ((x/cellWidth) + 0) * cellWidth; 
      calcCell.right = calcCell.left + cellWidth; 
      calcCell.top = ((y/cellHeight) + 0) * cellHeight; 
      calcCell.bottom = calcCell.top + cellHeight; 
      Log.d(TAG, "Rect: " + calcCell + " Player: " + playerRect); 
      return calcCell; 
     } 
     public void setPlayerRect(Rect newplRect) { 
      playerRect.set(newplRect); 
     } 
     private void setTouchZone() { 
      playerTouchRect.set(
        playerRect.left - playerRect.width()/TOUCH_ZONE, 

        playerRect.top - playerRect.height()/TOUCH_ZONE, 
        playerRect.right + playerRect.width()/TOUCH_ZONE, 
        playerRect.bottom + playerRect.height()/TOUCH_ZONE); 
     } 
     public Rect getPlayerRect() { 
      return playerRect; 
     } 
     public Point getPlayerCell() { 
      return plCell; 
     } 
     public void setPlayerCell(Point cell) { 
      plCell = cell; 
     } 
    }* 

답변

0

이 건축 문제에 중첩 할 수 있었다. 자세한 내용은 here을 참조하십시오.

본질적으로 물리 시뮬레이션 (간단 할 수도 있음)이 프레임 속도 (60fps로 제한됨)에 결합됩니다. 60Hz보다 빠른 이벤트는 정확하게 처리 할 수 ​​없으므로 버그가 발생합니다.

해결 방법은 독립적 인 스레드에서 충돌 감지를 실행하고 60fps로 계산하는 상태를 그리는 것입니다.

또한 동일한 기사를 참조하는 these gamedev 질문을 살펴보십시오.

+0

하지만 TouchEvent에 문제가 있습니까? – user2505374