1
\$\begingroup\$

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

}
\$\endgroup\$
4
  • \$\begingroup\$ 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(); \$\endgroup\$ Commented Apr 6, 2012 at 20:57
  • \$\begingroup\$ Even without the conversion it still doesn't work. I don't call world.clearForces() either. I am a little confused right now. \$\endgroup\$
    – Romeo
    Commented Apr 6, 2012 at 21:02
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Apr 6, 2012 at 21:12
  • \$\begingroup\$ Added the game loop and body creation. \$\endgroup\$
    – Romeo
    Commented Apr 6, 2012 at 21:19

1 Answer 1

0
\$\begingroup\$

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();
\$\endgroup\$
8
  • \$\begingroup\$ Setting the velocity is fine for doing things such enforcing a max velocity. \$\endgroup\$ Commented Apr 6, 2012 at 21:58
  • \$\begingroup\$ @ClassicThunder: tinyurl.com/7fwfn53 :D I think it's a bad idea. Isn't the rule to only set velocity when using kinematic bodies? \$\endgroup\$ Commented Apr 6, 2012 at 22:00
  • \$\begingroup\$ 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. \$\endgroup\$
    – Romeo
    Commented Apr 6, 2012 at 22:11
  • \$\begingroup\$ Are you sure that your delta value is correct? Delta should be 1/fps. So for 60 fps, your delta should be: 0.0167 \$\endgroup\$ Commented Apr 6, 2012 at 22:18
  • \$\begingroup\$ 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. \$\endgroup\$
    – Romeo
    Commented Apr 6, 2012 at 22:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .