# XNA: How to make the Vaus Spacecraft move left and right on directional keys pressed?

I'm currently learning XNA per suggestion from this question's accepted answer:
Where to start writing games, any tutorials or the like?

I have then installed everything to get ready to work with XNA Game Studio 4.0.

General Objective

Writing an Arkanoid-like game. I want to make my ship move when I press either left or right keys.

Code Sample

``````    protected override void Update(GameTime gameTime) {
// Allows the game to exit
this.Exit();

#if WINDOWS
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
else {
if (Keyboard.GetState().IsKeyDown(Keys.Left))
MoveLeft(gameTime);
}
#endif

// Move the sprite around.
BounceEnergyBall(gameTime);

base.Update(gameTime);
}

void MoveLeft(GameTime gameTime) {
// I'm not sure how to play with the Vector2 object and its position here!...
_vausSpacecraftPos /= _vausSpacecraftSpeed.X; // This line makes the spacecraft move diagnol-top-left.
}
``````

Question

What formula shall I use, or what algorithm shall I consider to make my spaceship move as expected left and right properly?

Thanks for your thoughts! Any clue will be appreciated. I am on my learning curve though I have years of development behind me (already)!

-
Please, at least leave a comment when downvoting! I don't get the fact that this question deserves a downvote, so please explain! – Will Marcouiller Feb 3 '11 at 17:07

It looks like you want to do something like: `_vausSpacecraftPos.X += _vausSpacecraftSpeed.X;` -- you may also want to scale by the elapsed time stored in the GameTime object so you get frame-rate independent movement.

-
+1 That's it! Thanks! =) – Will Marcouiller Feb 3 '11 at 13:28

You could use some inertia by making the keys apply a force to the spacecraft rather than just applying speed:

``````const float MOVE_FORCE = 5.0f; // tweak this constant for more responsive movement
float force = 0.0f;
if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
force -= MOVE_FORCE;
}
else if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
force += MOVE_FORCE;
}

// we add in air resistence to cap the max speed of the spacecraft
const float AIR_RESISTENCE = 0.1f; // tweak this to change top speed
float airResistenceForce = _spacecraftSpeed.X * _spacecraftSpeed.X * AIR_RESISTENCE;
// air resistence is always applied against the current speed direction
if (_spacecraftSpeed.X < 0.0f)
{
force += airResistenceForce;
}
else
{
force -= airResistenceForce;
}

const float MASS = 1.0f; // tweak for more/less inertia
float acceleration = force / MASS;

_spacecraftSpeed.X += acceleration * timeStep;
_spacecraftPos.X += _spacecraftSpeed.X * timeStep;
``````

You have a few constants to tweak in here to get the handling exactly right. I'd reccomend hooking up some code to tweak them at runtime.

You can go crazy and add more forces to the spacecraft - maybe an "autoaim" force that pushes the spacecraft towards a target point? Or a powerup/punishment random force that makes the spacecraft hard to control?

-
+1 I like this idea, and I seem to have some difficulties to implement it. For instance, I don't yet get how to set a maximum speed so that the spacecraft cannot fly faster, etc. – Will Marcouiller Feb 3 '11 at 13:51
The air resistence will create a maximum speed for you, if always applied against the direction of travel. As it's proportional to the square of your speed, it becomes a large force as your speed increases and cancels out the movement force. – tenpn Feb 3 '11 at 14:44
...the air resistence will also slow down your ship once you remove your finger, but you may want to introduce a braking force to do this quicker. – tenpn Feb 3 '11 at 14:45