Adding movement algorithms to sprites
I said I would get back to some more interesting gameplay-related enhancements to Critterbits, and this morning I deliver! For a while now I’ve had player-controlled movement in the engine, which was easy enough to implement as simple velocity changes in response to keyboard or controller input. However, it was about time to give some life to my non-player-controlled sprites, so I went ahead and implemented a couple of algorithms to help with that.
The simplest algorithm for this sort of animation tweening is called linear interpolation. It looks like this:
new_position = start_position + percent × (end_position - start_position)
Essentially we’re just dividing up the movement from the start to the end evenly over time, hence the “linear.” It looks like this in the engine:
Sorry for not perfectly looping it… I know it’s a bit distracting. But the elk is moving from a start position to an end position and then back again, evenly divided over 1.5 seconds of travel. I also added another algorithm called quadratic ease-in.