Move a character mario style on multi world step call?

Hello,

I think this question has been answered 100 times but I couldn't find a real answer,
especially because I call world.Step more than 1 time in Update()
because this http://gafferongames.com/game-physics/f ... -timestep/ says so.

I want to move my character that it only moves during I press the movement keys.
So, I thought about a simple procedure like
Code:
case onleftkeydown:
body->applyimpulse
case onleftkeyup:
body->clearvelocity


but this would also clear the force which was added through other sources.
And because I call step() more than one time in an update and the force gets cleared after each call, its only useful for the first call to step().

If I only apply/set the force/impulse/velocity but dont clear it, the player doesn't stop to move.
no one? 🙁
You could do a counter impulse, or increase linear damping in the character.
What do you mean with counter impulse?

And with damping you mean friction?
That doesn't help, because it only needs more force to move then.
I meant apply an impulse in the opposite direction the character is moving.

And damping I mean damping. There is a linear damping value you can specify in the body definition.
well, increasing the linear damping to 10.f helped, but it also changes the gravitational force and other forces :S
Hm, I guess you'll have to apply a counter impulse (probably just a horizontal counter impulse (leftwards or rightwards?)). You'll have to do something like:
Code:
//get the negative x-component and multiply by the mass to
//get the amount of impulse needed to stop the character from
//moving it horizontally
float amountToStop = -body->GetLinearVelocity().x*body->GetMass();

//apply a gradual stop so it's not a sudden stop.
amountToStop *= 0.8;

//apply impulse
body->ApplyImpulse( b2Vec2(amountToStop,0), b2Vec2(0,0) );

That code may not work at all, but the idea is there.
I know I'm resurrecting this topic, but I was looking for something like that and I just realized how to do it. I'm posting it here as a possible solution for the next one looking for it.

Code:
vel = body->GetLinearVelocity();
vel.x = 0;
body->SetLinearVelocity(vel);

Original thread: https://box2d.org/forum/viewtopic.php?t=8240