Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;

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 .

share|improve this question
They wont move any more because they 'hit' the platform, so you're moving them back to their previous position. You need to check their new position against the old position. If they moved down into the platform from the top (as with gravity), you just need to use their old Y position, not the X. – DrDeth Apr 16 '11 at 15:06
Aw man, I knew it was something simple. Fixed everything up. Only problem now is that if you keep moving towards a platform when you hit the side of it, you can just stick to it until you let go of the arrow key. I'll have to figure that out next, thanks for the help! If you post the comment as an answer, I'll mark it as such. – Mike Lentini Apr 16 '11 at 15:20
up vote 2 down vote accepted

A little late, but posted as an answer as requested.

They wont move any more because they 'hit' the platform, so you're moving them back to their previous position. You need to check their new position against the old position. If they moved down into the platform from the top (as with gravity), you just need to use their old Y position, not the X.

To solve the 'sticking to the side' issue, you'd do the same as the 'sticking to the top' solution, but just revert the X and leave the Y in tact.

In the case where you move the character too far back, you could just calculate the exact X or Y position using the width/height of the bounding boxes.

Eg. If you're using the centroid as your position, and the player is moving left-to-right and hits a wall:

player.X = wall.X - (wall.Width / 2) - (player.width / 2);
share|improve this answer
You're missing a close-bracket! – doppelgreener Apr 19 '11 at 4:00
Not any more! ;) Thanks. – DrDeth Apr 19 '11 at 19:42

You really just got to do the inverse for side collisions, go back to the old X and let the Y go on to continue the fall. Beware of corners where you hit two different blocks, there you got to make the full stop.

And by the way, ideally you shouldn't move all the way back to the old position, but just far enough that you don't collide.

share|improve this answer
Yeah, don't move back to the original position, or when holding a button down to continually run into a wall, the character will "vibrate" as it gets its position set to when it was not colliding. – AttackingHobo Apr 16 '11 at 22:29
@AttackingHobo, the "vibrating" does not happen because the player's sprite doesn't actually show a position change until the collision and everything is sorted out. Also, I fixed my original problems using DrDeth's suggestion so I would give him credit for the answer, but alas he only posted a comment. I'll give it to eBusiness because he/she sparked the idea that if the player hits the side of the platform, I can just reset his x-position, then call a method I have called fall(), which... make's the character fall. Everything works now, thanks guys! – Mike Lentini Apr 16 '11 at 22:33
You could see the character floating a little and registering as airborne a few frames after having fallen on a surface, especially if the fall was high. Basically because it's repositioned to a point in the air, and then has to fall again. Worst thing that could come of this is sluggish controls at those moments. You decide if it's worth doing something about. – aaaaaaaaaaaa Apr 16 '11 at 23:52
Exactly that @eBusiness. Lets say a char is at x 100, and the wall is at x 105, lets say a frame of movement sets the character to 106, the character would teleport back to 100. That would happen over and over again. – AttackingHobo Apr 17 '11 at 0:08
I haven't experienced this yet, so as of now it's not something I'm going to try to fix. The characters never move fast enough to make that happen, from what I've seen so far. I'll have to keep that in mind for future games though, thanks guys. – Mike Lentini Apr 17 '11 at 14:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.