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:
- Create a velocity vector to be used for acceleration
- Create a max speed value to cap the velocity vector
- Add a small value to the velocity vector when player hits a movement button
- Add the velocity vector x and y values to the players position (which can also be a vector)
- Continuously reduce the velocity vector by a very small amount each frame (this adds a slow-down effect)
- 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?