Let's step away from programming for a second and just look at physics in one dimension. Say you have an object with an x
coordinate. If you add 1 to this value every second, then your object will move at 1 units per second.
Now instead of 1, let's add v
(for velocity). If v
is 1, then you have the same behaviour as before. If you add 2 to v
every second however (call 2 a
for acceleration), then the object will keep moving faster and faster over time; aka accelerating, so 2 units per second per second (i.e. units/second²) is your acceleration. In calculus terms: the second derivative of position, or the first derivative of velocity (which is itself the first derivative of position).
So back to programming. You have an update loop that let's say runs 60 times per second. Instead of seconds, lets call our temporal units "ticks" (1/60 seconds). And instead of meters/units, let's call our spatial units "pixels".
Right now, you're adding a constant to your position poxX
and posY
based on some user input. Instead, if you add a variable to these, call them velX
and velY
, and modify that based on user input, you'll find that the speed of of your tanks ramps up or down based on how long you hold the keys, and when you let go, they move on forever. Now you don't exactly want that, so you need to simulate friction through "damping", e.g. velX *= 0.9
every tick.
Then you can go one step further and add accX
and accY
, and set those to constant values when a key is pressed. From there it's just a matter of tweaking parameters to find the right balance.