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.

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);

}
share|improve this question
    
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

1 Answer 1

up vote 0 down vote accepted

Adding your update loop was useful :D

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();
share|improve this answer
    
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

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.