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);
}
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(); \$\endgroup\$