1
\$\begingroup\$

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

The code is here: https://github.com/NoahJon3s/New-Journey-The-Game/blob/master/sketch_190820a.pde

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?

Thanks in advance.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .