body keeps moving down after I set Linear velocity to (3,0)!

This is my first post on these forums.

Anyway I have a body and I only want it to move up,down,left, and right. Never diagonal. I use setLinearVelocity to accomplish this but the body still moves diagonally sometimes.
To prove it I used this code around my _world step:

NSLog(@"linear velocity: %f %f", mybody->GetLinearVelocity().x, mybody->GetLinearVelocity().y);
NSLog(@"the x1, y1: %f %f", mybody->GetPosition().x, mybody->GetPosition().y);
_world->Step(dt, velocityIterations, positionIterations);
NSLog(@"the x2, y2: %f %f", mybody->GetPosition().x, mybody->GetPosition().y);

I have my ship moving down using ship->setLinearVelocity(0,-3). Then I make it move left by using setLinearVelocity(-3,0) and when I'm done it prints off this:

linear velocity: -3.000000 0.000000
the x1, y1: 2.144350 4.124759
the x2, y2: 2.063884 4.086793
linear velocity: -3.000000 0.000000
the x1, y1: 2.063884 4.086793
the x2, y2: 1.982696 4.073295
linear velocity: -3.000000 0.000000
the x1, y1: 1.982696 4.073295
the x2, y2: 1.915824 4.062496
linear velocity: -3.000000 0.000000
the x1, y1: 1.915824 4.062496
the x2, y2: 1.865070 4.062496


As you can see even though the program acknowledges that my linear velocity is set to -3,0. It still moves my ship slightly in the downward direction before plateau-ing at the value of 4.062496.

When you go from setLinearVelocity(0,-3) to setLinearVelocity(-3,0) does it take the body a few world steps to stop moving left, thus allowing it to move momentarily diagonally?? And if so how do I make it stop doing that?

Thanks for any help, and I can clarify more if this post is confusing.

EDIT: Moved to general discussion because this was the wrong place
You can try using a bunch of if statements for restrictments....example:

b2Vec2 directions = bodyA->GetLinearVelocity();

if directions.x > 0 and directions.y > 0
//this means its going diagonally up right
sprite.position = ccp(bodyA->GetPosition().x * PTM_RATIO = 0, bodyA->GetPosition().y * PTM_RATIO
//forces x to stay on b2Vec2 0 while y doesn't change thus keeping it straight up

This code might not be correct but the idea is there....hope this helps
I think this has something to do with warm starting? You may just have to set the position directly for a few frames to prevent it.
I was hoping there would be some simple elegant fix for this but oh well. Writing some "correcting" code around the step function won't be too bad. Thanks guys for the help! 🙂
You could try turning off warm starting, b2World::SetWarmStarting(bool)
You could try using a prismatic joint between the body and a ground body. You can recreate the joint as necessary to change the axis.
Erin Catto wrote:
You could try using a prismatic joint between the body and a ground body. You can recreate the joint as necessary to change the axis.

this is even better than turning off warm starting

blackberry developers

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