1
\$\begingroup\$

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 .

\$\endgroup\$
2
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – DrDeth
    Apr 16, 2011 at 15:06
  • \$\begingroup\$ 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. \$\endgroup\$ Apr 16, 2011 at 15:20

2 Answers 2

2
\$\begingroup\$

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);
\$\endgroup\$
1
  • \$\begingroup\$ You're missing a close-bracket! \$\endgroup\$ Apr 19, 2011 at 4:00
4
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ 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. \$\endgroup\$ Apr 16, 2011 at 22:29
  • \$\begingroup\$ @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! \$\endgroup\$ Apr 16, 2011 at 22:33
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Apr 16, 2011 at 23:52
  • \$\begingroup\$ 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. \$\endgroup\$ Apr 17, 2011 at 0:08
  • \$\begingroup\$ 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. \$\endgroup\$ Apr 17, 2011 at 14:41

You must log in to answer this question.

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