# Collision Detection For A Maze/Cave

I have a simple code written in Processing 3.x that I am using to test out the collision detection for a cave/maze.

If you where to run said code, you'd see that I have the code so that it detects collisions on the x-axis (top of the rectangle) and y-axis (left side of the rectangle), and correctly prevents the movement of the "player" from moving out of the rectangle, but I cannot seem to find a solution for preventing the movement out of the bottom or the right side of the rectangle, even though I am able to detect the collision. Every solution I try seems to lock the "player" to those respective sides. Any suggestions?

You have a logic problem in your code here

if(x>oldx)
{
x=oldx;
x-=2;
}

if(x<oldx)
{
x=oldx;
x+=2;
}


The reason why it only works for left and top is because you are overriding the x value in the second if statement. When going right x will be greater than oldx. You then set x to oldx - 2, the second if checks if x is lower than oldx which in this case it is. The way to solve this is to put else if in front of the second if statement. This also counts for the Y axis.