Click here to Skip to main content
14,641,690 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am writing an Android game right now and I would need some help in the collision of the wall on screen. When I drag the ball in the top and right it able to collide in wall but when I drag it faster it was able to overlap in the wall


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;
		}
	}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100