9
\$\begingroup\$

I saw a number of 2d game tutorials and books where previous position of an object was stored in state instead of velocity vector. Velocity is then calculated each frame as a difference between positions.

What are the advantages of that method?

\$\endgroup\$
  • 2
    \$\begingroup\$ Could you link to the tutorials in question, for context? \$\endgroup\$ – Anko Dec 13 '15 at 18:35
  • \$\begingroup\$ I share the same curiosity of Anko \$\endgroup\$ – dnk drone.vs.drones Dec 13 '15 at 18:47
  • \$\begingroup\$ This tutorial mentions that this is called Verlet integration. I found this article, in which Verlet's advantages are explained. Basically, it's easier to handle collisions and complex constraints. \$\endgroup\$ – Terzalo Dec 13 '15 at 19:10
  • \$\begingroup\$ @Terzalo See the updated version of my answer to see if it helps understanding the general logic behind the specific case at hand \$\endgroup\$ – MAnd Dec 13 '15 at 20:59
1
\$\begingroup\$

Without knowing exactly the tutorials or books you have read, here is what I can tell you.

To be more precise in terms of physics definition: the vector direction is calculated as the difference between current position and last position. The vector velocity is equal that divided by the time elapsed when going from one position to the other. See: http://www.physicsclassroom.com/class/1DKin/Lesson-1/Speed-and-Velocity

However, when the elapsed time is considered to be the generic "1 frame", i.e. from the last frame to the current, then the vector velocity and the vector direction become the same. In pseudo-code:

direction = current_position - past_position
elapsed_time = 1 frame
velocity = direction / elapsed_time

So, in general, the main reason for storing the position in the last frame and getting the position in the current frame is that it can be either the only way or the cheapest way of knowing the updated direction (and therefore the updated velocity), i.e. the movement direction in the current frame.

Suppose that you store the direction (or even the velocity) in a given frame. In the next frame, the only way of knowing if the direction (or even the velocity) had changed is if you calculated it again. The reason being that previous direction or previous velocity do not let you know the current direction or current velocity in case movement changed from one frame to the next.

Now, taking into account the tutorials you have mentioned in the comment to your own question, it becomes even clearer. In one of the tutorials the authors says that:

Now, let's teach our particle how to move with Verlet integration. That's just a fancy way to say that we're going to find out how fast our particle is moving by comparing its position this frame to its position last frame. Why? Because that gives us implicit velocity - any change to the particle's current position will automatically update its velocity

So, as you can see, storing position last frame allows, as I said above, the updated calculation of velocity in the current frame when the object (in the tutorial it's a particle) changes position. If you saved only the direction or velocity in the last frame, you wouldn't be able to update it as easily in case the position changed from past to current frame. Therefore, as also pointed in the other tutorial you linked to, saving previous position allows automatic updating of velocity when positions change.

\$\endgroup\$

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.