I'm having some troubles with a 2D platformer type game (there's more to it, but that's all that matters for this question). I had collision with the platforms working fine, but if the character hit the side of the platform, they would be automatically be placed on top of the platform. This is bad, for obvious reasons. I want the player to hit the side and fall, like one would expect. So what I'm trying to do is simply keep track of the previous position of the player, and if the player's bounding box overlaps the platform's bounding box, move the player back to their previous position. The problem is, when the player is on the platform, they cannot move left or right. They can jump, they can move while they're in the air, and their falls are stopped by the platforms. I just can't figure out why they won't move anymore. Here's some relevant code:
// previous position is stored
previousPos = new Vector2(playerPosition.x, playerPosition.y);
....
// game checks input and calculates next position and
// stores it in playerPosition. only changes the Vector2
// playerPosition, does not display the change via the sprite yet,
// so it doesn't look like it's stuttering to the player
....
if (!isJumping) {
// loop through the platform list
for (Platform platform : platformList) {
// if the player is touching a platform, then they are on a platform
// and they are not falling
if (platform.getBoundingBox().contains(playerBoundingBox)) {
playerPosition = new Vector2(previousPos.x, previousPos.y);
boundingBoxPositionLower = new Vector3(playerPosition.x, playerPosition.y, 0);
boundingBoxPositionUpper = new Vector3(playerPosition.x+playerSprite.getWidth(), playerPosition.y+playerSprite.getHeight(), 0);
playerBoundingBox.set(boundingBoxPositionLower, boundingBoxPositionUpper);
isOnPlatform = true;
isFalling = false;
playerDirection.y = 0;
break;
}
}
}
I'm not sure where to go from here. Maybe ideas on how to debug? Or maybe it's just a simple problem I'm overlooking. If you need any other code snippets, let me know.
Thanks guys!
PS - Sorry I don't follow the 80 characters per line standard, it all fits on my 1080p monitor fine in my IDE :P .