My friend and I are developing a top down puzzle game.
We have already implemented a player class that can walk and collide the walls, but the problem is after the collision itself.
For example:
When the player hits a wall to its right, we set the velocity on the x axis to be 0, and the player stops. But after that, if the user presses the up key, the player moves one pixel up, and then stops again.
After that if you keep pressing right and up alternately, the player is walking through the wall pixel after pixel.
The player moves through the wall
And here is the collision detection of the player:
for (int i = 0; i < lvl.getSizeInTiles().x; i++) {
for (int j = 0; j < lvl.getSizeInTiles().y; j++) {
if (lvl.getTileFromBuffer(i, j).getType() == "wall") {
if (intersects(lvl.getTileFromBuffer(i, j).getPosition().x, lvl.getTileFromBuffer(i, j).getPosition().y, lvl.getTileFromBuffer(i, j).getSize().x, lvl.getTileFromBuffer(i, j).getSize().y)) {
if (faceDirection == "UP" && vel.y < 0) {
collidesUp = true;
}
else {
collidesUp = false;
}
if (faceDirection == "DOWN" && vel.y > 0) {
collidesDown = true;
}
else {
collidesDown = false;
}
if (faceDirection == "LEFT" && vel.x < 0) {
collidesLeft = true;
}
else {
collidesLeft = false;
}
if (faceDirection == "RIGHT" && vel.x > 0) {
collidesRight = true;
}
else {
collidesRight = false;
}
}
}
}
}
}
if (vel.x > 0 && !collidesRight) {
pos.x += vel.x;
}
if (vel.x < 0 && !collidesLeft) {
pos.x += vel.x;
}
if (vel.y > 0 && !collidesDown) {
pos.y += vel.y;
}
if (vel.y < 0 && !collidesUp) {
pos.y += vel.y;
}
Just a quick note: The intersect(x, y, w, h) function is a part of the player class, and tell us if you need to see it.
We will be happy if you send a reply about our code, or link us to a place we can find a solution.
So thank you for any help, and please ask questions if you need to, Arad and Ron.
Just a guess with the info provided, but notice that when you go through the wall, you're character isn't actually facing to the right. It's facing up, so collidesRight will be false. Instead of inhibiting movement based on which direction your character is facing, inhibit solely based on position i.e.