Sponsored By

Using Vectors For Smooth Movement

Where I go over a simple method to improving the look and feel of movement for your 2d games. Simple vector addition.

Jeremiah Goerdt, Blogger

July 14, 2013

2 Min Read
Game Developer logo in a gray background | Game Developer

There are a few ways to get movement working in a 2D game. One of which is simply adding a specific amount to the position of the player. If you want a fast-paced game, this might not be the best approach.

Another way to take care of movement is almost as simple but makes a huge difference. When you add vectors into the mix you can make movement look much smoother and have some cool effects.

Using Vectors For Movement

I'm still working on my local multiplayer "taglike" game and until yesterday I had only thrown in the most basic movement. Since I was using a high speed for the players it was looking a bit rigid and unnatural.

My good friend Robert mentioned that it didn't look quite as good as it could so I decided to take his advice and change the mechanics behind movement. The best part is that as soon as I added in the new movement system I found a game-breaking bug with collision. Oops...

Let's go over exactly how to upgrade your movement:

  1. Create a velocity vector to be used for acceleration

  2. Create a max speed value to cap the velocity vector

  3. Add a small value to the velocity vector when player hits a movement button

  4. Add the velocity vector x and y values to the players position (which can also be a vector)

  5. Continuously reduce the velocity vector by a very small amount each frame (this adds a slow-down effect)

  6. Once the velocity gets to a small number (ie 0.1) you can just set it to 0 so that your players actually stop

For my game, Rigged 2 Blow, my max speed is 10. This means that the player will never go more than 10 pixels at a time. When the players move I add 1 to the velocity vector each frame. This creates a quick acceleration effect which is good because I like the fast-paced feel to my game.

When it comes to decreasing the velocity values, what I do is multiply them by 0.9 each frame. This will ensure that you slow to a stop when you release the movement keys and it looks pretty damn sharp

Give It a Shot In Your Game

If you're making a simple 2D game that doesn't need advanced movement functions, give this a shot and see how it changes the feel of the game. You might be surprised.

Do you use any other simple movement functions for your games?

Read more about:

Blogs
Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like