Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Please, look at this GIF:

enter image description here

The label on the animation says "Move button is pressed, then released". And you can see when it's pressed (and player's getCenterY() is above wall getCenterY()), gravity doesn't work. I'm trying to fix it since yesterday, but I can't. All methods are called from game loop.

public void move() {
    if (left) {
            switch (game.currentLevel()) {
            case 1:
                for (int i = 0; i < game.lvl1.getX().length; i++)
                    game.lvl1.getX()[i] += game.physic.xVel;
                break;

            }
        } else if (right) {
            switch (game.currentLevel()) {
            case 1:
                for (int i = 0; i < game.lvl1.getX().length; i++)
                    game.lvl1.getX()[i] -= game.physic.xVel;
                break;
            }
        }
    }

    int manCenterX, manCenterY, boxCenterX, boxCenterY;
//gravity stop
    public void checkCollision() {
        for (int i = 0; i < game.lvl1.getX().length; i++) {
            manCenterX = (int) game.man.getBounds().getCenterX();
            manCenterY = (int) game.man.getBounds().getCenterY();
            if (game.man.getBounds().intersects(game.lvl1.getBounds(i))) {
                boxCenterX = (int) game.lvl1.getBounds(i).getCenterX();
                boxCenterY = (int) game.lvl1.getBounds(i).getCenterY();
                if (manCenterY - boxCenterY > 0 || manCenterY - boxCenterY < 0) {
                    game.man.setyPos(-2f);
                    game.man.isFalling = false;
                }
            }
        }
    }
//left side of walls
    public void colliLeft() {
        for (int i = 0; i < game.lvl1.getX().length; i++) {
            if (game.man.getBounds().intersects(game.lvl1.getBounds(i))) {
                if (manCenterX - boxCenterX < 0) {
                    for (int i1 = 0; i1 < game.lvl1.getX().length; i1++) {


                        game.lvl1.getX()[i1] += game.physic.xVel;
                        game.man.isFalling = true;

                    }
                }
            }
        }
    }
    //right side of walls
    public void colliRight() {
        for (int i = 0; i < game.lvl1.getX().length; i++) {
            if (game.man.getBounds().intersects(game.lvl1.getBounds(i))) {
                if (manCenterX - boxCenterX > 0) {
                    for (int i1 = 0; i1 < game.lvl1.getX().length; i1++) {

                        game.lvl1.getX()[i1] += -game.physic.xVel;
                        game.man.isFalling = true;
                    }
                }
            }
        }
    }
public void gravity() {
    game.man.setyPos(yVel);
}

 //not called from gameloop:    public void setyPos(float yPos) {
        this.yPos += yPos;

}
share|improve this question

closed as too localized by Byte56, Jonathan Hobbs, John McDonald, Trevor Powell, jco Sep 29 '12 at 14:30

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
Typically posting your code asking us to find the problem is too localized a question for this site. You need to use the debugger and step through your code. Set a break point for when collision is detected, then step through line by line and see where it goes, and why it's not applying gravity. –  Byte56 Aug 27 '12 at 18:58

1 Answer 1

up vote 1 down vote accepted

Change the code that removes falling if he is on top of the block:

if (manCenterY - boxCenterY > 0 || manCenterY - boxCenterY < 0) 
{
      game.man.setyPos(-2f);
      game.man.isFalling = false;
}

To code that removes falling if he is on top of the block:

if (manAboveBlock && (manCenterY - boxCenterY > 0 || manCenterY - boxCenterY < 0)) 
{
      game.man.setyPos(-2f);
      game.man.isFalling = false;
}

This is treating the symptom and not the problem however. The problem is probably that when the right key is pressed next to a wall, for a split second the player moves inside of that wall and then is corrected by moving back outside the wall. While the player is clipping into the wall his isFalling value is set to false.

share|improve this answer

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