SDL smooth velocity decreasing

Hello. I am making tiny game for just my own pleasure. I have one tiny problem with movement. When I stop holding keys for movement my character stops immediately but I want it to be more smooth. I want to see decreasing velocity not instant stopping. That's code. What should I do to get result I want?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  void CharProp::handleEvent(SDL_Event e)
{
	if (e.type == SDL_KEYDOWN && e.key.repeat == 0)
	{
		switch (e.key.keysym.sym)
		{
		case SDLK_w:
		case SDLK_UP:
			cVelY -= CharVel;
			break;
		case SDLK_s:
		case SDLK_DOWN:
			cVelY += CharVel;
			break;
		case SDLK_a:
		case SDLK_LEFT:
			cVelX -= CharVel;
			break;
		case SDLK_d:
		case SDLK_RIGHT:
			cVelX += CharVel;
			break;
		}
	}
	else if (e.type == SDL_KEYUP && e.key.repeat == 0)
	{
		switch (e.key.keysym.sym)
		{
		case SDLK_w:
		case SDLK_UP:
			cVelY += CharVel;
			break;
		case SDLK_s:
		case SDLK_DOWN:
			cVelY -= CharVel;
			break;
		case SDLK_a:
		case SDLK_LEFT:
			cVelX += CharVel;
			break;
		case SDLK_d:
		case SDLK_RIGHT:
			cVelX -= CharVel;
			break;
		}
	}
}
It might help to create a pair of "target velocity" variables that you set instead of your character's actual velocity, and have your character's velocity converge on the target velocity in your game loop somewhere (ideally in CharProp's update member function, if it has one).

-Albatross
Topic archived. No new replies allowed.