[LibGDX] Player Movement lagging

So I’m trying to make the player move using Box2D. The player moves left and right using ‘A’ and ‘B’ keys.
When I press ‘A’ and ‘D’ very fast the movement lags or should i say twitches. Here is the code.

The Player class extends InputAdapter and I Override 2 methods, KeyDown and KeyUp.
The movement is Vector2 variable.

@Override
	public boolean keyUp(int keycode) {
		if(Keys.A == keycode || Keys.D == keycode && movement.x > 0) {
			body.setAngularVelocity(0);
			movement.x = 0f;
			body.setLinearVelocity(movement);
		}else
			return false;
		
		return true;
	}
	
	@Override
	public boolean keyDown(int keycode) {
		switch(keycode) {
		case Keys.A:
			movement.x = -velocity;
			break;
		case Keys.D:
			movement.x = velocity;
			break;
		default:
			return false;
		}
		return true;
	}

The Player class has also update method where I apply the movement

public void update() {
			body.applyLinearImpulse(movement, new Vector2(), true);
	}

And then I’ve just used the players update method in the render method like so player.update();
of course the i’ve added listenenrs and so on. Is there something that I’ve forgotten?

Here is the .jar file.
http://www.mediafire.com/download/x5aogpoqahcb87h/test.jar
Is it normal that the file is 18Mb?? :smiley:

Thank you very much.

hey,

im also working with box2d for some time.
can you explain your lagging ? what is lagging ? and how ?
also, try applying force instead of an impulse.
And youve really gotta watch out that u use the right values for your player or entities box2d defs.

Edit
just tryed your jar, i thought you mean lag in a other way :smiley:
now i see what you mean.
But i dont see any twiching there.
The bad thing is the switch case there. Because if the user presses both buttons, or one button directly after the other button. Or he lets on pressed after that you get strange results, like holding “A” button and the player standing still and stuff. And by pressing the buttons really fast you create situations like these.

Thanks for the reply. I know that my explanation isn’t the best, but the .jar says it all ;D

I removed the switch case in replaced it with if statements (without else) and it still does not work.
I dont want to use applyForce(); for the player.I’ve had this with all my projects and because I just can’t fix it I usually stop doing those projects :(.

ive got a similar programm and do it like this :

if (Gdx.input.isKeyPressed(Keys.W) && Gdx.input.isKeyPressed(Keys.A)) {
			DO-Stuff;
			return;
		}
if (Gdx.input.isKeyPressed(Keys.W)) { 
DO-Stuff;
return;

and so on, i tryed it before with key up and keydown in a similair way like your trying it and it caused problems.
but i think you could try to solve it with some booleans.
Like :


boolean aPressed = false;
boolean dPressed = false;

and the keydowns set these, the keyups reset these and if “a” was pressed, pressing “d” gets ignored and if “d” was pressed “a” gets ignored. until they got resseted with the keyup.
But no idea if this works.

Fixed it finally. I removed the keyDown,keyUp and update method for player and instead replaced them with this.

public void move() {
			if(Gdx.input.isKeyPressed(Keys.A)) {
				movement.x = -velocity;
				body.applyLinearImpulse(movement, new Vector2(), true);
			}
			
			if(Gdx.input.isKeyPressed(Keys.D)) {
				movement.x = velocity;
				body.applyLinearImpulse(movement, new Vector2(), true);
			}
			
			if(!(Gdx.input.isKeyPressed(Keys.A)) && !(Gdx.input.isKeyPressed(Keys.D)))
				body.setLinearVelocity(new Vector2());
	}

I’m just wondering is there a better way to do this? Anyways Thanks alot for helping out.

yeah i think it may be possible to solve this with booleans the way i described it in my previous post, but i havent tryed it.