Sprite movement (acceleration)

Hey, so far ive only had my sprites move at a constant speed, like this:

x += dx * elapsedTime;
y += dy * elapsedTime;

Where elapsedtime is the time between each render loop and dx and dy is the velocity in y and x direction - simple, working and quite boring to look at. What I would like is a movement that accelerates to a max speed and de-accelerates when the sprite is near its goal, like this:

start position - ||||| | | | | | | | | | | | | | ||||||| - end position

Where the space between the |'s is the position of the sprite. I bet its simple math, but [insert excuse here].

just ramp dx and dy up and down as a factor of your point in time in the entire motion.

So I should store up an array with different speeds which I then plot in at different times? But what if I dont know the end time, say e.g. if it is stopped by a player at an unknown time?

Well, acceleration and deacceleration is linear in most games (eg platformers). So you just add/subtract a constant value to/from the delta (use clamping here) and add that delta to the position.

If you need something more fancy (eg a sorta realistic racing game) you could use some kind of lookup table (with some kind of graph basically) and interpolate linearly. Well, something like this isnt necessary for some fast paced arcade game. Its really as simple as stated above.

maybe this will help you…

Velocity is defined as the change in distance over time.
Accleration is defined as the chang in Velocity over time.

SO if x and y are your position.
And dx and dy are your verlocity per ms, which you add by doing x += dx * elapsed ms
THEN ddx and ddy are you acceleration (or deceleartion if negative) which you add to velocity the same way: dx += ddx* elapsed ms.

Does that help make it clearer?

(This by the way is called the “first derivative of velocity” in calculus, but you dont need to knwo that to use it.)

Thanks I think i got it now.

“first derivative of velocity” - nice to know what I was looking for :wink: