I have a platformer game in which I implemented the movement using a wheel attached to the hero. For jumping I use this:

player.body.applyLinearImpulse(new Vec2(0, 30000000), player.body.getPosition());

The problem is that the xVelocity doesn't remain the same during the jump so it isn't looking natural. Is there any way to modify only the x velocity of the body so that before jumping I store it in a variable and after jumping I apply it to the body?

I hope you understand what I am trying to say.

  • \$\begingroup\$ That behaviour looks pretty uncommon. Also your impulse is enormous. In my game, the impulse I used for jumping was literally 1. Try figuring out what is wrong with your Box2D world. \$\endgroup\$ Apr 12 '12 at 9:32
  • \$\begingroup\$ Well, i created the world with the gravity Vector2(0, -10). And for my human i have 2 bodies both with 1f density. I don't know why but a smaller impulse wont move the body. It seemed pretty weird to me too but i didn't know what to do. \$\endgroup\$
    – Romeo
    Apr 12 '12 at 10:02
  • \$\begingroup\$ I think that you are creating your bodies with the size of pixels... Box2D works in meters. You have to scale the scene when you render the it. \$\endgroup\$ Apr 12 '12 at 10:04
  • \$\begingroup\$ That's exaclty what i was doing. Can you please tell me a good ratio pixel to meter? Or is there a standard? \$\endgroup\$
    – Romeo
    Apr 12 '12 at 10:11
  • \$\begingroup\$ I used something like 60. \$\endgroup\$ Apr 12 '12 at 10:12

You are defining your bodies in pixels instead of meters. Box2D uses meters. This way, your bodies got a very very huge mass, which causes you having to apply to big forces / impulses.

You have to define the lengths in meters and upscale your scene when you draw it.

  • \$\begingroup\$ Also if you can help me a little bit. I defined my PIXEL_TO_METER = 60f. How do i need to modify these 2 functions: public Vec2 screenToWorld(Vec2 screenV) { return new Vec2(screenV.x + this.x, yFlip * screenV.y + this.y ); } and public Vec2 worldToScreen(Vec2 worldV) { return new Vec2(worldV.x - this.x, yFlip * worldV.y - this.y); } ? \$\endgroup\$
    – Romeo
    Apr 12 '12 at 10:38
  • 2
    \$\begingroup\$ @Romeo Start a new question. \$\endgroup\$ Apr 12 '12 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.