I'm trying to make my character move around a tile map with collisions. Everything works fine except for one thing. I show you a picture with the problem:
https://i.sstatic.net/ZWI7x.jpg
That is, when I reach a tile above then I can not move anywhere. If you come from the left, I can not move either up or down. If you reach the bottom, I can move to the left but not right. And when you reach the right I can move in any direction.
Honestly I have no idea what may be failing. I think it has to do with if (...), because if I change the order, the addresses where I can move change :/
Here I leave some code:
boolean collision = false;
if(Keyboard.isKeyDown(Keyboard.KEY_UP)) {
for(int i = 0; i < map.GetNumLayers(); i++) {
if(UpTile(map, i) > 128) {
collision = true;
}
}
if(!collision) AddPos(0.0f, -vel);
}
if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
for(int i = 0; i < map.GetNumLayers(); i++) {
if(LeftTile(map, i) > 128) {
collision = true;
}
}
if(!collision) AddPos(-vel, 0.0f);
}
if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
for(int i = 0; i < map.GetNumLayers(); i++) {
if(DownTile(map, i) > 128) {
collision = true;
}
}
if(!collision) AddPos(0.0f, vel);
}
if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
for(int i = 0; i < map.GetNumLayers(); i++) {
if(RightTile(map, i) > 128) {
collision = true;
}
}
if(!collision) AddPos(vel, 0.0f);
}
public int UpTile(Map map, int layer) {
return map.GetValue(layer, (int) (x/size), (int) (y/size));
}
public int LeftTile(Map map, int layer) {
return map.GetValue(0, (int) (x/size), (int) (y/size));
}
public int DownTile(Map map, int layer) {
return map.GetValue(layer, (int) (x/size), (int) (y/size + 1));
}
public int RightTile(Map map, int layer) {
return map.GetValue(layer, (int) (x/size + 1), (int) (y/size));
}
public int GetValue(int layer, int x, int y) {
return array[layer][y][x];
}
The map is an array with 3 componentes: [LAYER][Y][X]. So, in RightTile by example, with GetValue, I'm getting the value from the array in the position [layer][y/size][x/size + 1], x/size + 1 because I want to get the kind of tile that is to my right (same with the others). size = 64, that is the width and height of each tile.
Thanks >.<