1
\$\begingroup\$

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) this.Exit(); // TODO: Add your update logic here #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)!

\$\endgroup\$
1
  • 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\$ Commented Feb 3, 2011 at 17:07

2 Answers 2

4
\$\begingroup\$

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.

\$\endgroup\$
0
1
\$\begingroup\$

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?

\$\endgroup\$
3
  • \$\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\$ Commented Feb 3, 2011 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\$ Commented Feb 3, 2011 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\$ Commented Feb 3, 2011 at 14:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.