2
\$\begingroup\$

I'm creating a game where the player ascends a tower by wall jumping his way to the top. When the player has collided with the right wall they can only jump left and vice versa. Here is my current implementation:

    if(wallCollision() == "left"){
        player.setPosX(0);
        player.setVelX(0);
        ignoreCollisions = true;
        player.setCanJump(true);
        player.setFacingLeft(false);
    } else if (wallCollision() == "right"){
        player.setPosX(screenWidth-playerWidth*2);
        player.setVelX(0);
        ignoreCollisions = true;
        player.setCanJump(true);
        player.setFacingLeft(true);
    } else{
        player.setVelY(player.getVelY() + gravity);
    } 

and

private String wallCollision(){
    if(player.getPosX() < playerWidth && !ignoreCollisions)
        return "left";
    else if(player.getPosX() > screenWidth - playerWidth*2 && !ignoreCollisions)
        return "right";
    else{       
        timeToJump += Gdx.graphics.getDeltaTime();
        if(timeToJump > 0.50f){
            timeToJump = 0;
            ignoreCollisions = false;
        }
        return "jumping";
    }
}

If the player is colliding with the left wall it will switch between the states left and jumping repeatedly due to the varible ignoreCollisions being switched repeatedly in collision checks. This will give a chance to either jump as intended or simply ascend vertically instead of diagonally.

I can't figure out an implementation that will reliably make sure the player jumps as intended. Does anyone have any pointers?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

It seems your left limit for detecting collision is playerWidth, but when you detect a collision on the left, you reset the x coordinate to 0.

if(wallCollision() == "left"){
    player.setPosX(0); //Should be player.setPosX(playerWidth);

[EDIT]

After seen your images I notice playerWidth actually represents half the player width, and your character X coordinate is that of it's left side. In that case, try this, detect left collision when player.getPosX() < 0, and reset the pos to 0 as you're doing now:

if(wallCollision() == "left"){
    player.setPosX(0);
    player.setVelX(0);
    ignoreCollisions = true;
    player.setCanJump(true);
    player.setFacingLeft(false);
} else if (wallCollision() == "right"){
    player.setPosX(screenWidth-playerWidth*2);
    player.setVelX(0);
    ignoreCollisions = true;
    player.setCanJump(true);
    player.setFacingLeft(true);
} else{
    player.setVelY(player.getVelY() + gravity);
} 

and:

private String wallCollision(){
    if(player.getPosX() < 0 && !ignoreCollisions)
        return "left";
    else if(player.getPosX() > screenWidth - playerWidth*2 && !ignoreCollisions)
        return "right";
    else{       
        timeToJump += Gdx.graphics.getDeltaTime();
        if(timeToJump > 0.50f){
            timeToJump = 0;
            ignoreCollisions = false;
        }
        return "jumping";
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ This isn't the issue. My co-ordinates and detection are fine. It's a problem with my logic that I don't know how to fix. Thanks though. \$\endgroup\$
    – Nanor
    May 14, 2014 at 17:00
  • \$\begingroup\$ Obviously we can't see the rest of your code, Nanor, but the inconsistency between handling a right-side collision (where you set the player's position to be just outside of the right-wall threshold) and left-side collision (where you set the player's position to be a full character width inside the left-wall threshold) does seem problematic. \$\endgroup\$
    – Victor T.
    May 14, 2014 at 18:08
  • \$\begingroup\$ @Nanor Did you try the fix I proposed? does it change the behavior somehow? \$\endgroup\$
    – jspurim
    May 14, 2014 at 19:28
  • \$\begingroup\$ Hi guys, thanks for your help. First @VictorT. - Player position for both boundaries sets the position of the player's sprite to be just within the boundaries of the wall so there is no gap and none of the sprite is offscreen. See the first two images of this gallery: imgur.com/a/aQjwb jspurim I did and the effect it had can be seen in the third image of this gallery: imgur.com/a/aQjwb Thanks \$\endgroup\$
    – Nanor
    May 15, 2014 at 8:42
  • \$\begingroup\$ I'm curious, Nanor: Where exactly is the player position in those images? Can you add code to draw a single red pixel at player.getPosX(), player.getPosY() and re-post the image titled "Left Wall Collision"? \$\endgroup\$
    – Victor T.
    May 16, 2014 at 18:06
1
\$\begingroup\$

I agree with @jspurim that your collision bounds checking, but that is another matter. You are asking about inconsistent jumping off of walls due to the ignoreCollision flag flip-flopping, right?

From what I gather reading the code you have posted, when the player jumps and collides with a wall, they stick to that wall and begin to climb vertically. After a brief moment the player can then jump off the wall, or continue climbing the wall they are currently stuck to. The flip-flopping is happening because after the short pause you say it is ok to stop ingnoring collision, when in fact the player is still running up the wall and there is no need to check for wall collisions yet.

Try moving ignoreCollisions = false; out of wallCollision() and put it in the code where the player actually jumps off of the wall.

\$\endgroup\$

You must log in to answer this question.

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