game acceleration

hello everyone,

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.
Add a friction variable.
when the key to move isn't pressed, multiply the the players speed by friction.

1
2
3
4
const float friction = 0.97;

if(keyIsntPressed)
    playerSpeed *= friction;


That will cause the speed to gradually decrease to a halt

Say your character has a speed of 15

15 * 0.97 = 14.55

14.55 * 0.97 = 14.11

... and so on.
Last edited on
closed account (3TkL1hU5)
edit: ^^ Yeah, like that. :D

_____________
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.
Last edited on
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.


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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  void teleport(float tx, float ty) {
    x = tx;
    y = ty;
    update_off();
  }
  void teleport(position temp) {
    x = temp.x;
    y = temp.y;
    update_off();
  }
virtual void move_vel(float txvel, float tyvel){
      xvel = txvel;
      yvel = tyvel;
      move_vel();
  }
  virtual void jmp_vel(float txvel, float tyvel){
   xvel = txvel;
   yvel = tyvel;
  }
  float ret_xvel(){
  return xvel;
  }
  float ret_yvel(){
  return yvel;
  }
  virtual void change_xvel(float change){
   xvel += change;
  }
  virtual void change_yvel(float change){
   yvel += change;
  }
  void move_slope(float chgy, float chgx, float dist){
      teleport (x + chgx * dist / 100, y + chgy * dist / 100 );
  }
  void move_vel(){
      x += xvel;
      y += yvel;
      if (xvel < .1 && xvel > -.1){
      xvel = 0;
      }
      if (yvel < .1 && yvel > -.1){
      yvel = 0;
      }
      update_off();
  }
  void apply_yfric(float fric){
  if (yvel <= fric * -1){
  yvel += fric;
  }
  else if (yvel <= fric){
  yvel = 0;
  }
  else if (yvel > fric){
  yvel -= fric;
  }
  }
  void apply_xfric(float fric){
  if (xvel <= fric * -1){
  xvel += fric;
  }
  else if (xvel <= fric){
  xvel = 0;
  }
  else if (xvel > fric){
  xvel -= fric;
  }
  }
  void apply_fric(float fric){
  apply_xfric(fric);
  apply_yfric(fric);
  }
  void bounce_y(float bounce = .9){
  yvel *= -1;
  yvel *= bounce;
  }
  void bounce_x(float bounce = .9){
  xvel *= -1;
  xvel *= bounce;
  }


edit: sorry, Reread, looks like that's exactly what you had =D sorry.
Last edited on
If you already have an acceleration variable then you could simply give this a negative value rather than setting the speed variable to zero.
Topic archived. No new replies allowed.