0
\$\begingroup\$

Think a rocket or a missile. Completely don't know how to make it start slow and then get progressively faster in an non-linear fashion, given max speed and base acceleration. Curve like this would look best.

 ^                       .
S|                       .
 |                      .
 |                    ..
 |                ....
 |         .......
 | ........
 +------------------------>
                         T
\$\endgroup\$
2

2 Answers 2

1
\$\begingroup\$

considering time step dt, position x, speed s, acceleration rate a(s) :

s += a(s) * dt;
x += s * dt;

Acceleration rate a(s) can be a constant, then you will have quadratic acceleration.

For exponential :

a(s) = s * factor

if factor is positive, the more the speed, the more the acceleration.

You can give a few tries for the a(s) function until you come to what looks nice to you...

\$\endgroup\$
6
  • \$\begingroup\$ @Kromster I do not really understand what you mean. dt is measured by a clock in the game loop to fit the actual elapsed time between last and current iteration. That way time evolution in the game feels real. \$\endgroup\$
    – Sylvain B.
    Commented Jun 29, 2018 at 9:10
  • 1
    \$\begingroup\$ Lets assume acceleration = 1 and compare 0.5sec of game time with one tick where dt=0.5 and five ticks where dt=0.1 correspondingly. In first case, end result will be x = 0.25, but in second x = 0.15. With inconsistent dt results will be unpredictably different. \$\endgroup\$
    – Kromster
    Commented Jun 29, 2018 at 10:20
  • 1
    \$\begingroup\$ Yes you are right. Usually dtis rather small. I am not sure but I would say that as dtgets smaller, the behavior is less sensitive to the variations of dt. I think the OP is more looking for a nice animated behavior than for an actual true physics simulation. But if one looks for an accurate simulation, then it would be good to take the corrections you mention into account. Most little games I have developed were fine enough with simple Euler integration as I show in my answer. \$\endgroup\$
    – Sylvain B.
    Commented Jun 29, 2018 at 10:44
  • \$\begingroup\$ Hence my comment - it would be nice to mention this detail in the answer ) \$\endgroup\$
    – Kromster
    Commented Jun 29, 2018 at 10:46
  • \$\begingroup\$ @Kromster : I think you have something in your mind. Yet I never took time to analyse precisely the math behind these kinds of correction for step-by-step integration of exponential equations. Maybe you could explain your idea in an other answer ? \$\endgroup\$
    – Sylvain B.
    Commented Jun 29, 2018 at 10:54
0
\$\begingroup\$

Generally, for this "progressively getting faster" behavior to work, you need to have acceleration constant and variable speed. Update speed and objects position each tick (with dt, delta time between this and previous tick) like so:

acceleration := 1; // some constant value
speed := speed + acceleration * dt;
position := position + speed * dt;

To make behavior even more progressive, you can make acceleration to grow over time too:

acceleration_grow := 1; // some constant value
acceleration := acceleration + acceleration_grow * dt;
speed := speed + acceleration * dt;
position := position + speed * dt;

Note that if you have varying dt, then the behavior will vary as well, since every previous step affects next one. E.g. one dt=0.5 update will produce significantly different result than five updates with dt=0.1, despite having the same dt sum.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .