1
\$\begingroup\$

So how exactly does something like \$v_f = v_i +a*t\$ translate to something like self.vel += self.acc in code? or \$d=v_i*t+\frac{a*t^2}{2}\$ translating to self.pos += self.vel + 0.5 * self.acc there seems to be variables missing, and summations happening that aren't part of the formulas.

\$\endgroup\$
  • 1
    \$\begingroup\$ Could you add a bit more context? Where is that code from? \$\endgroup\$ – Alexandre Vaillancourt Jun 4 '17 at 18:45
  • \$\begingroup\$ Where did you find that it's translated like that? \$\endgroup\$ – DH. Jun 4 '17 at 18:52
  • \$\begingroup\$ youtube.com/… \$\endgroup\$ – Nathan Jun 4 '17 at 18:53
  • \$\begingroup\$ github.com/kidscancode/pygame_tutorials/blob/master/platform/… \$\endgroup\$ – Nathan Jun 4 '17 at 18:54
  • 1
    \$\begingroup\$ It looks like this version just assumes your timestep is always one unit (eg. one frame) - so v = v + a*t becomes v = v + a*(1) which is just v += a. I wouldn't recommend this generally, as we usually want our timesteps to be small (eg. 1/30th of a second or less), while keeping our units familiar (eg. metres per second) \$\endgroup\$ – DMGregory Jun 4 '17 at 19:20
1
\$\begingroup\$

The formulas you're showing translate like that because some values are implicit, which removes the need to explicitly use other values. Read on!

Let's recap what these formulas are, based on where you have most likely picked the original images:

\$v_f = v_i +a*t\$ and \$d=v_i*t+\frac{a*t^2}{2}\$

where

  • vf indicates the final velocity of the object;
  • vi indicates the initial velocity of the object;
  • a stands for the acceleration of the object;
  • t stands for the time for which the object moved;
  • d stands for the displacement of the object.

And let's see how they are implemented in the tutorial you've linked to:

self.vel += self.acc
self.pos += self.vel + 0.5 * self.acc

Finally, let's compare these implementations using the formulas:

vf = vi + a
d = vi + 0.5 * a

What's missing? The references to t (time).

It is because, in this particular situation, the time is assumed to be constant (a frame rate of 60Hz, each frame lasting 1/60th of a second). The tutorial is using pygame. Without going into the details, pygame allows to throttle the frame-rate to a desired frequency, and that's what is done in the tutorial. So all the explicit references to time are removed because they're implicit in the other parameters. The acceleration in that tutorial units per 1/60th of a second and not units per second.

This can be done when using a fixed time step, that is, when each frame is expected to have the same duration. Some 3rd party game infrastructure support this (pygame is an example, Unity too, with the FixedUpdate method), while others don't (e.g. the Update method of Unity). In this case, the "delta time" is passed to the Update method and this value should be used with the 'normal' formula to manage the displacement of objects.

As per DMGregory's comment, this can add confusion, specially when the code is not well documented. You should generally use 'standard' units such as meter per seconds, kilometer per hour, radians per seconds, rotations per minut and such, until you're at the point where you have to manage the value on a per-frame basis.

\$\endgroup\$
  • \$\begingroup\$ So self.vel = self.vel + self.acc is the same as vf=vi+a how? Where is the initial and final velocity? \$\endgroup\$ – Nathan Jun 5 '17 at 14:10
  • \$\begingroup\$ @Nathan The self.vel before the equal sign (=) represents the final velocity, while the one after it represents the initial velocity. There is no point in keeping the initial velocity, so the variable is reused. The 'final velocity' of 'this frame' will be the 'initial velocity' of the next frame. \$\endgroup\$ – Alexandre Vaillancourt Jun 5 '17 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.