Hey Everyone,
I’m having problems with my collisions. Basically when i colliding against a wall i cant slide if i press another key…For example. if i press The left arrow key and i am on the wall, if i continue to hold this key and try to press the up arrow my player wont move upwards. I hope you know what i mean. I know its probably a really easy fix but i need help.
Here is my player update method:
public void update() {
movePlayer();
// Check Collision;
int curCol = tileMap.getColTile((int) x);
int curRow = tileMap.getRowTile((int) y);
double xDest = x += dx;
double yDest = y += dy;
double tempX = x;
double tempY = y;
collision(x, yDest);
if (dy < 0) {
if (topLeft || topRight) {
dy = 0;
tempY = curRow * tileMap.getTileSize() + height / 2;
} else {
tempY += dy;
}
}
if (dy > 0) {
if (bottomLeft || bottomRight) {
dy = 0;
tempY = (curRow + 1) * tileMap.getTileSize() - height / 2;
} else {
tempY += dy;
}
}
dy = 0;
collision(xDest, y);
if (dx < 0) {
if (topLeft || bottomLeft) {
dx = 0;
tempX = curCol * tileMap.getTileSize() + width / 2;
} else {
tempX += dx;
}
}
if (dx > 0) {
if (topRight || bottomRight) {
dx = 0;
tempX = (curCol + 1) * tileMap.getTileSize() - width / 2;
} else {
tempX += dx;
}
}
dx = 0;
x = tempX;
y = tempY;
tileMap.setX((int) (Game.WIDTH * Game.SCALE / 2 - x));
tileMap.setY((int) (Game.HEIGHT * Game.SCALE / 2 - y));
}
public void movePlayer() {
if (left) {
dx -= moveSpeed;
}
if (right) {
dx += moveSpeed;
}
if (up) {
dy -= moveSpeed;
}
if (down) {
dy += moveSpeed;
}
}
I might have an idea on how to do it. i think you could use something like
if(dx != 0 && dy != 0){
//Something in here
}
Please any help is much appreciated.
Thanks
- GlennBrann