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.

Player movement causes its sprite to shake a bit. I don't what is causing it or how to fix it.

I recorded a video of the problem. In the recording, it looks like the world is shuddering, but only the player sprite shows the effect in the game.

I don't remember this happening when I first implemented the class. The player moves horizontally and vertically (platformer movement). The player's update method is where everything happens that pertains to the player's movement.

//logic for the player
public void update(GameContainer gc, int delta, Map m){

    //update all of the player's projectiles
    for(Projectile p : projectiles)
        p.update(delta);

    //update flag based on whether the player's movements will cause him to hit something
    boolean updateX = true;
    boolean updateY = true;

    //deceleration
    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;
    }
    //handle falling
    boolean fallFlag = true;
    if(inAir)
        dy += .075f;

    //detect the player's collisions with things on the map
    Rectangle prx = new Rectangle(x + dx, y, 10, 10);
    Rectangle pry = new Rectangle(x, y + dy, 10, 10);
    //collides with platforms
    for(Platform p : m.platforms){
        Rectangle r = new Rectangle(p.getX() - 2, p.getY() - 2, p.width + 2, p.height + 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 = -Helper.GRAVITY;
        }
    }


    //Collision with the collectible blocks, implement this with "to the side" pickup later
    for(CollectableBlock b : m.collectableBlocks){
        Rectangle r = new Rectangle(b.getX() - 2, b.getY() - 2, b.width + 2, b.height + 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 = -Helper.GRAVITY;
        }
    }
    inAir = fallFlag;

    if(updateX)
        x+=dx*(delta/10);
    if(updateY)
        y+=dy*(delta/10);
}
share|improve this question
1  
What you've posted here is a large wall of code, which you are then asking us to debug for you, with very little context or related information. That's very hard for us to do. Why do you even think this code is related? Have you profiled your game and identified this as a bottleneck? If not, you should start there. –  Josh Petrie Jun 20 '13 at 4:48
4  
Your delta is an int?. The way you update the player at the end of the loop is where Id be throwing some debug breakpoints as I suspect you're always moving very small amounts and suffering from sub pixel accuracy issues. –  Matt D Jun 20 '13 at 4:49
3  
As Matt suggested, your "delta" variable is int. You are using this in division by other int (at the end of function), so floating part will be just removed. You should definitely try to change "int" to "float" or "double". –  zacharmarz Jun 20 '13 at 5:16
5  
When you notice that something that used to work has broken, you should update to an older revision where it worked and bisect the revision that actually broke it. This can help you figure out the problem. If you are not using version control yet, you should start using one immediately. –  msell Jun 20 '13 at 6:52
1  
@Brandon Perhaps a video of the problem will be helpful. I only mentioned getting the reputation back so you'd have enough to get into chat (20 rep). –  Byte56 Jun 20 '13 at 22:11
show 11 more comments

1 Answer

I had a shuddering issue and I tried all sorts of changes to my physics update method. The problem was with the update order of the player and camera. See my answer here: The ship "shudders" in scrolling Asteroids

share|improve this answer
    
I moved the player.update and map.update before anything else in my games update method. This still didnt fix it! –  Brandon Jun 22 '13 at 5:10
add comment

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.