0
votes

Im trying to make body move with increaseing speed. At first its starts to accelerate but it reaches a constant speed shortly after. How do I make it keep accelearte?

My code looks like this:

 world = new World(new Vector2(0, 0), true);

    if (Gdx.input.isKeyPressed(Input.Keys.D))
        body.applyLinearImpulse(400f, 0, pos.x, pos.y, true);
    if (Gdx.input.isKeyPressed(Input.Keys.A))
        body.applyLinearImpulse(-400f, 0, pos.x, pos.y, true);
    if (Gdx.input.isKeyPressed(Input.Keys.W))
        body.applyLinearImpulse(0, 400f, pos.x, pos.y, true);
    if (Gdx.input.isKeyPressed(Input.Keys.S))
        body.applyLinearImpulse(0, -400f, pos.x, pos.y, true);
1
What is the size of your world ? The default Box2D setting is 1 px = 1 m. That means if you want to run your game on a 720 p screen (for example) your world will be 1280 meters by 720 meters, and a small objet of 100 px x 100 px will have a size of 100 m x 100 m, which is huge. Box2D is only good at dealing with "realistic" sizes and speeds. That means using huge size and speed will results in bad simulations and/or limitations as you can observe. - vdlmrc
The solution for that is simple : create a view port with small size, so every objet will be smalls, and box2d won't have problem. If it is your problem, look at my answer on this message - vdlmrc
So ? Any update on this problem ? Did you try my solution or Springbua's solution ? Did that work ? If no, what do you observe ? - vdlmrc
I solved it by scaling down the screen to 100f, just like you said the ball was huge in box2d. - JohnDev

1 Answers

1
votes

To make a Body accelerate over time, usualy applyForce is used, applyLinearImpulse is used for immediate speed changes instead.
Remeber, that you have to call applyForce, as long as you want to accellerate, while an impulse is usually only applied once.
Think about a starting car: The rotation of the wheels, combined with the friction adds a force to the whole car, accelerating it.
If the car then has got some speed and hits a box, an impulse is applied to the box once, increasing it's speed almost immediately.
So you could try to change the applyLinearImpulse call to applyForce and make sure it get's called every update cycle, as long as you press the given key.

I suggest you to read the Box2D tutorials on iforce2d.net