Well, I implemented a AABB system of collision detection, and it works fine, but now I need to make it so that the player can move after he collides with an object. Right now, when the player collides with an object, he just gets stuck and you can’t move him. I realize why this is happening, I just don’t know how to solve it because of the algorithm I’m using. Here’s the player movement code:
public void update(Mob mob) {
if (player) {
if (pos.getX() < 0)
pos.setX(0);
if (pos.getX() > 1216)
pos.setX(1216);
if (pos.getY() < 0)
pos.setY(0);
if (pos.getY() > 656)
pos.setY(656);
if (Keyboard.isKeyDown(Keyboard.KEY_W) && !Collision.isColliding(box, mob.box)) {
pos.setPosition(pos.getX(), pos.getY() + MOVESPEED);
}
if (Keyboard.isKeyDown(Keyboard.KEY_S) && !Collision.isColliding(box, mob.box)) {
pos.setPosition(pos.getX(), pos.getY() - MOVESPEED);
}
if (Keyboard.isKeyDown(Keyboard.KEY_D) && !Collision.isColliding(box, mob.box)) {
pos.setPosition(pos.getX() + MOVESPEED, pos.getY());
}
if (Keyboard.isKeyDown(Keyboard.KEY_A) && !Collision.isColliding(box, mob.box)) {
pos.setPosition(pos.getX() - MOVESPEED, pos.getY());
}
}
}
And then the collision detection algorithm:
public static boolean isColliding(AABB a, AABB b) {
if (a.x + a.size < b.x || b.x + b.size < a.x || a.y + a.size < b.y || b.y + b.size < a.y) {
return false;
}
return true;
}
AABB is just a class that holds the position and size of the mob, nothing special. But, as you can see my code only checks if both axis are colliding or not, but I need to test it its colliding on only one at a time. How can I do this?