I'm working on a top down game and I was experimenting with ways to stop the cobblestoning/staircasing effect when moving in diagonals, you know, that jitter that occurs when you're not moving at exactly 1 pixel increments diagonally.
I tested a couple of great solutions already out of there (for instance the one lazydevs showed on his shmup tutorial series), but I felt that something different would work better for me.
Basically, it works by making sure you're always moving at whole pixel increments. There's two buffers used to change the x and y coordinates on the screen. The buffers increment towards the integer nearest to the character speed, and movement only happens when that whole number target is reached.
So, if you have a set speed of 0.5, you'll move 1 pixel every two frames, or something like that, you get the idea. It isn't always exact (especially with more exotic speeds, like 0.3, 1.6...), but it always makes you move at whole pixel increments.
The movement system also has an approximation of normalization for diagonals (you can toggle it on or off with a global variable). I call it an approximation because you're always moving at whole pixel increments, and at speeds larger than 1 the moving speed will get rounded to the nearest integer.
In a proper normalization, if you have speed at 2, on the diagonals it would be normalized to 1.4 or something close to that. In my system, it gets rounded to 1 instead. It still gives the effect of a normalized velocity, but it isn't exact, because it has to be an integer to avoid the jitter on the diagonals.
If you look at the code, it will probably be easier to understand lol.
I've also included a simple collision system with map tiles. Oh and the speed increases and decreases with inertia/friction, and you can change it when creating an actor object, to make it slide more or less.
I hope this helps you!
[Please log in to post a comment]