Itās a bit too hard-coded to just check if the tile is 0 or 1, if you want to have several types of tiles. If you only need 2 types of tiles (walls and not walls) then it is fine.
If you have more types of tiles, like grass, forest (makes player move slower or leaves could fly around the player), or water (stops the player unless his name is Jesus) or whatever, then Iād recommend a Tile-class, which has an int ID (which refers to the number used in the map[][]), and an int type to recognize which kind of tile it is, and lastly a boolean passable indicating if it can be traversed at all.
Using simple rectangles to check for collisions is fine, but I believe thereās an easier method, which also makes it much easier to control what to do, depending on which side of the player is colliding with a tile. Not to mention the freedom for precision, in that you can put the points as close to the actual player-part of your image, if he doesnāt fill out the entire image.
What I did for my game, was to check 8 points around the player (see image below), and react if one of these points are within a tile that cannot be traversed. If any of the 4 top/bottom points are within a tile, I move the player back to his previous Y-position (which I save in a variable every update). If any of the 4 left/right points are within a tile, I move the player back to his previous X-position (which I save in a variable every update).
It is important to do the X and Y separately, or your player will get stuck in weird ways, meaning you canāt be walking left into a wall and still move upwards or downwards along it.
http://img534.imageshack.us/img534/3953/collisionthingy.jpg
This is what I used in my very first game, and it works perfectly. (See for yourself.
Extremely simplified code to move the player. Use whatever youād like:
public void movePlayer(){
if(keys[KeyEvent.VK_UP]){
player.setPlayerPositionY(player.getPlayerPositionY()-3);
}
if(keys[KeyEvent.VK_DOWN]){
player.setPlayerPositionY(player.getPlayerPositionY()+3);
}
if(keys[KeyEvent.VK_LEFT]){
player.setPlayerPositionX(player.getPlayerPositionX()-3);
}
if(keys[KeyEvent.VK_RIGHT]){
player.setPlayerPositionX(player.getPlayerPositionX()+3);
}
}
When the player has been moved, we do the collision-checking.
Code to check all points for collisions and reacting to them (the offsets for the points are hardcoded after the size of my player image; the playerPosition is the dead center of the player image in my game):
public void checkCollisionsAroundPlayer(){
int tileCoordX = player.getPlayerPositionX();
int tileCoordY = player.getPlayerPositionY();
Point upRight = new Point(), upLeft = new Point(), rightTop = new Point(), rightBot = new Point(), leftTop = new Point(), leftBot = new Point(), downRight = new Point(), downLeft = new Point();
upRight.setLocation(tileCoordX+9,tileCoordY-13);
upLeft.setLocation(tileCoordX-9,tileCoordY-13);
rightTop.setLocation(tileCoordX+13,tileCoordY-9);
rightBot.setLocation(tileCoordX+13,tileCoordY+9);
leftTop.setLocation(tileCoordX-13,tileCoordY-9);
leftBot.setLocation(tileCoordX-13,tileCoordY+9);
downRight.setLocation(tileCoordX-9,tileCoordY+13);
downLeft.setLocation(tileCoordX+9,tileCoordY+13);
if(checkCollisionForPoint(upRight) || checkCollisionForPoint(upLeft)){
player.setPlayerPositionY(player.getLastY());
}
if(checkCollisionForPoint(downRight) || checkCollisionForPoint(downLeft)){
player.setPlayerPositionY(player.getLastY());
}
if(checkCollisionForPoint(leftTop) || checkCollisionForPoint(leftBot)){
player.setPlayerPositionX(player.getLastX());
}
if(checkCollisionForPoint(rightTop) || checkCollisionForPoint(rightBot)){
player.setPlayerPositionX(player.getLastX());
}
}
Code to check a single point for collisions. The tiles are referenced by their ID in the ground[][], which is looked up in my TileList, which is a list of all loaded Tile-objects. Each Tile-object knows if it is passable:
public boolean checkTileCollisionsFromMapCoordinates(int tileCoordX, int tileCoordY){
boolean collides = false;
int tileX = getTilePosX(tileCoordX);
int tileY = getTilePosY(tileCoordY);
if(!tileList.getTileList().get(map.getGround()[tileY][tileX]).isPassable()) collides = true;
return collides;
}