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
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

        // TODO: Add your update logic here
        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
        else {
            if (Keyboard.GetState().IsKeyDown(Keys.Left))

        // Move the sprite around.


    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.


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)!

  • 1
    \$\begingroup\$ Please, at least leave a comment when downvoting! I don't get the fact that this question deserves a downvote, so please explain! \$\endgroup\$ – 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.


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;
    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?

  • \$\begingroup\$ +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. \$\endgroup\$ – Will Marcouiller Feb 3 '11 at 13:51
  • \$\begingroup\$ 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. \$\endgroup\$ – tenpn Feb 3 '11 at 14:44
  • \$\begingroup\$ ...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. \$\endgroup\$ – tenpn Feb 3 '11 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.