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.

My player is getting caught on something on the floor. My whole environment is just blocks. I have looked through (and tried to change) just about everything I can think of in my player update method. Any idea what could be solving this? Here is the code:

public void update(GameContainer gc, int delta, Map m){
    boolean updateX = true;
    boolean updateY = true;

    if(!movingLeft && dx < 0f){
        dx += .2f;
    }
    if(!movingRight && dx > 0f){
        dx -= .2f;
    }

    if(!movingLeft && !movingRight && ((dx < 0 && dx > -.2f) || (dx > 0 && dx < .2f))){
        dx = 0f;
    }
    boolean fallFlag = true;
    prx = new Rectangle(x + dx, y, 10, 10);
    pry = new Rectangle(x, y + dy, 10, 10);
    for(Platform p : m.platforms){
        r = new Rectangle(p.getX() - 2, p.getY() - 2, 20 + 2, 20 + 2);
        if(prx.intersects(r)){
            updateX = false;
            dx = 0;
        }
        //check if my dy will cause me to hit a platform
        if(pry.intersects(r)){
            //if I am moving down, set falling to false
            if(dy > 0)
                fallFlag = false;
            updateY = false;
            dy = .025f;
        }
    }
    inAir = fallFlag;
    if(inAir)
        dy += .025f;
    if(updateX)
        x+=dx*(delta/10);
    if(updateY)
        y+=dy*(delta/10);
}
share|improve this question

1 Answer 1

up vote 6 down vote accepted

I think the problem is that you are not taking gravity into consideration when checking for collision. For example, you update dy as follows:

if(inAir)
    dy += .025f;

However, for your collision check rectangle, you do the following:

pry = new Rectangle(x, y + dy, 10, 10);

This takes into account the movement of your object based on input, but it doesn't take into account the extra 0.025f accumulated by your falling acceleration.

Instead, create your rectangle as follows, and it may fix your issue:

if (inAir)
   pry = new Rectangle(x, y + dy + 0.025f, 10, 10);
else 
   pry = new Rectangle(x, y + dy, 10, 10);
share|improve this answer
1  
That fixed it, thanks Jon –  Brandon May 23 '13 at 16:03
    
Awesome. Glad I was right :) –  Jon May 23 '13 at 16:06
    
I got 15 reputation and upvoted you! :) –  Brandon May 23 '13 at 18:35
    
@Brandon Welcome to the community. –  Jon May 23 '13 at 19:32

Your Answer

 
discard

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.