Gilad Naaman
Gilad Naaman

Reputation: 6550

Unsmooth movement of sprite

I am developing a 2D platformer game in Java/Slick2D.

Up until now my character moved a constant amount of pixels per frame. I've tried switching to using the 'delta' variable (amount of time between frames), like advised, but the character's movement seems awfully jittery.

What can I do to smoothen the character's movement?

    private static final float DEFAULT_SPEED = 0.15f;

    Vector2f trans = new Vector2f();
    Input i = gc.getInput();
    boolean run = false;

    // X-Axis Movement
    if (i.isKeyDown(Input.KEY_D)){
        trans.x += DEFAULT_SPEED * delta;
        lastMoveDirection = Direction.RIGHT;
    }
    if (i.isKeyDown(Input.KEY_A)){
        trans.x -= DEFAULT_SPEED * delta;
        lastMoveDirection = Direction.LEFT;
    }
    if (i.isKeyDown(Input.KEY_LSHIFT)){
        trans.x *= RUN_SPEED_MULTIPLIER;
        run = true;
    }

Upvotes: 0

Views: 162

Answers (1)

MrLeap
MrLeap

Reputation: 506

How are you defining delta? It should be how long it took a frame to draw / distance moved per second, or similar.

Upvotes: 1

Related Questions