# JBox2D applyLinearImpulse doesn't work

So i have this line of code:

if(input.isKeyDown(Input.KEY_W)&&canJump())
{
body.applyLinearImpulse(new Vec2(0, 30), cam.screenToWorld(body.getPosition()));
System.out.println("I can jump!");
}


My problem is that the console display I can jump! but the body doesn't do that. Can you explain to me if i do something wrong?

Some more code. This function creates my 'hero' the one supposed to jump.

private Body setDynamic(float width, float height, float x, float y)
{
PolygonShape shape = new PolygonShape();
shape.setAsBox(width/2, height/2);

BodyDef bd = new BodyDef();
bd.allowSleep = true;
bd.position = new Vec2(cam.screenToWorld(new Vec2(x + width / 2, y + height / 2)));
bd.type = BodyType.DYNAMIC;
bd.userData = new BodyInfo(width, height);

Body body = world.createBody(bd);
body.createFixture(shape, 10);

return body;
}


And this is the main update loop:

if(input.isKeyDown(Input.KEY_A))
{
body.setLinearVelocity(new Vec2(-10*delta, body.getLinearVelocity().y));
}
else if (input.isKeyDown(Input.KEY_D))
{
body.setLinearVelocity(new Vec2(10*delta, body.getLinearVelocity().y));
}
else
{
body.setLinearVelocity(new Vec2(0, body.getLinearVelocity().y));
}

if(input.isKeyDown(Input.KEY_W)&&canJump())
{
body.applyLinearImpulse(new Vec2(0, 30), body.getPosition());
System.out.println("I can jump!");
}
world.step(delta * 0.001f, 10, 5);

}

• Try to not convert the coordinates. Simply keep body.getPosition(). Also, make sure that the order of the params is correct (impulse, point). Also, make sure that you don't call World.clearForces() before you call World.step(); – Martijn Courteaux Apr 6 '12 at 20:57
• Even without the conversion it still doesn't work. I don't call world.clearForces() either. I am a little confused right now. – Romeo Apr 6 '12 at 21:02
• Then we need to see more code. Like the game loop, body creation. Make sure mass of your body is not zero and that you created the body dynamic. – Martijn Courteaux Apr 6 '12 at 21:12
• Added the game loop and body creation. – Romeo Apr 6 '12 at 21:19

The problem is that you are overwriting the linear velocity each step.To move the player, apply forces. And do not set the speed, never.

Try something like this:

if(input.isKeyDown(Input.KEY_A))
{
body.applyLinearForce(new Vec2(-3000, 0), body.getPosition());
}
else if (input.isKeyDown(Input.KEY_D))
{
body.applyLinearForce(new Vec2(3000, 0), body.getPosition());
}

if (input.isKeyDown(Input.KEY_W) && canJump())
{
body.applyLinearImpulse(new Vec2(0, 30), body.getPosition());
System.out.println("I can jump!");
}

world.step(delta, 10, 5);
world.clearForces();

• Setting the velocity is fine for doing things such enforcing a max velocity. – ClassicThunder Apr 6 '12 at 21:58
• @ClassicThunder: tinyurl.com/7fwfn53 :D I think it's a bad idea. Isn't the rule to only set velocity when using kinematic bodies? – Martijn Courteaux Apr 6 '12 at 22:00
• It must be something i did wrong because when i change my code with yours my body will not move left / right anymore. :D I think forces and impulses don't work. – Romeo Apr 6 '12 at 22:11
• Are you sure that your delta value is correct? Delta should be 1/fps. So for 60 fps, your delta should be: 0.0167 – Martijn Courteaux Apr 6 '12 at 22:18
• I got this running, but the thing is i need big values for the parameters. For a body with the density = 0.1f i need a 3k force to move it. It may be the delta wrong. – Romeo Apr 6 '12 at 22:20