I'm trying to make a field runner type game, and im having a hard time making the person accelerate and decelerate. what i want to do is when the player presses the arrows the character accelerates to a certain speed then when the player lets go the character does not just come to a complete stop, but slows to a stop.
what i have so far is an acceleration rate variable and speed variable. now the character gets translated the speed amount, when the player presses the arrow, and the speed increases by the acceleration amount, up to a specific point, but when the player lets go of the key the speed value is reset and the character stops instantly. what i cant figure out is how to make the character decelerate instead of it stopping instantly.
_____________
I'm not sure how it would apply to programming, but deceleration is just acceleration in the opposite direction of travel or negative acceleration.
What if you have a deceleration rate variable?
Then say when key is released the deceleration variable is applied to speed until it is == 0.
I'm just guessing here. I'm a newcomer to programming, but it seems logical at least.
You could have a velocity, and an update position based on velocity function. That's what I do. Here's a sample. This is all in a class called img- from which I derive a class called animate- from which I derive a class called sprite. This is the basic motion for most of my objects.