I am struggling with some basic rectangle collision in my pacman clone, hoping for some help or simplification.
So I am using libgdx Tiled maps, in which I have made an object layer, and surrounded all the collide-able things with rectangles. My player is also represented as a rectangle, and I use these to handle collision.
Here is my problem graphically (I made debug render classes to help see the problem):
Holding right button, then right + up, when approaching corner.
Holding right button and up button, player now stuck. Must press left, then up, to move up.
Essentially, I have sliding on the walls working, but when I get to a corner, the rectangles mash together and block movement. I want this to slide around the corner, and not get stuck.
I know this is probably something really simple I am overlooking, but I cant quite place my finger on it.
Here is my rough Player movement code:
http://pastebin.java-gaming.org/c3cf63e321b1c
- First I add a number to velX or velY based on input.
- Then I check the future player rectangle, using the velocities from 1. to determine if the player will collide in the X or Y directions.
- Finally, if the player is not colliding in X, move player in X.
- If the player is not colliding in Y, move player in Y.
It seems like I need to have code that says “As soon as the player can move up, move up and stop all X movement”, but I cannot figure out how to implement it.
I would greatly appreciate some help with my problem. Thanks.