Reputation: 19
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);
Upvotes: 0
Views: 2208
Reputation: 9783
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
Upvotes: 1