2

I've been messing around with jbox2d and was suprised when the x-velocity of a body was affected by the gravity of the world. Here's my code:

    //create world
    Vec2 gravity = new Vec2(0, 1);
    boolean sleep = true;
    world = new World(gravity, sleep);
    //create wheel
    BodyDef wheelBodyDef = new BodyDef();
    wheelBodyDef.type = BodyType.DYNAMIC;
    wheelBody = world.createBody(wheelBodyDef);
    CircleShape circleShape = new CircleShape();
    FixtureDef wheelFixtureDef = new FixtureDef();
    wheelFixtureDef.shape = circleShape;
    Fixture wheelFixture = wheelBody.createFixture(wheelFixtureDef);
    wheelBody.setLinearVelocity(new Vec2(50, 0));

The linear velocity only makes a significant difference if I apply it every frame or if I disable gravity. Can anybody figure out what I'm doing wrong?

Matt
  • 21
  • 3

2 Answers2

1

Box2d does not have support for zero-gravity simulations; that is why you see no difference when you disable gravity. Also you probably see little difference because a gravity of (0, 1) is very weak; try (0, 10).

Also setLinearVelocity is a rayCast function. You are probably looking for applyForce().

Ramzi Khahil
  • 4,932
  • 4
  • 35
  • 69
Shel Soloa
  • 104
  • 7
0

If you set linear velocity on a body means that it will move in direction an speed set with the vector. If you disable gravity there is nothing that effects this movement, but with gravity enabled, its movement is influenced by gravity every frame. If you throw an apple in the outerspace it moves infinite in direction of the shoot, but on earth gravity continuously pulls it down again. If you want to move your body straight in the direction you want, than you have to set the linear velocity every frame. watch this tutorials, #2.35 and #2.36, there the proble is nicely explained.

good luck

philipp
  • 15,947
  • 15
  • 61
  • 106