# How can I control a game 2d car mechanic from acceleration & speed to just 1 value?

So I have a car in a 2d game that runs on acceleration and speed, two values that have a max. The acceleration is small, under the value of 1 and it rises by an incrementation. The speed is large number like 100 and it rises by incrementing the current acceleration. They are both incremented relative to the fps but that's not important.

What I am trying is to have a formula with having only one value (with a max) instead of two as I am trying to simplify the UI for the player.

Alternatively, I was thining of having a value (lets call it speed) that gets incremented by something, without a max, but is limited by, lets say road friction (like multipling it with 0.99). Such a thing would be felt lower when the car is slow but eventually the friction would act as a max/limit, the car oscilating to near max values.

I know too little of physics and car stuff to figure this out on my own so I am reaching out for help. Thanks!

I think your idea of simulating friction is on the right track. A stronger limiter of speed in real life is air resistance, which increases with the square of speed.

Air resistance is a type of drag, and to use it you would do something like

DRAG_COEFFICIENT = .0001
drag_acceleration = speed * speed * DRAG_COEFFICIENT
speed = speed - drag_acceleration * timestep


At lower speeds, this drag will have a relatively small effect due to the very small DRAG_COEFFICIENT. As the car moves faster, it will cause the car to accelerate less and less. At some value of speed, the drag will overtake what's being added by your acceleration and cause the car to top out.

I would suggest a Logarithmic function. A logarithmic curve starts to climb quickly, but slows as it tends towards the limit.

You'd have to tinker with the equation to get it just right for what you want, but that's the basic concept. Your increment is the X-axis here, and your speed the Y-axis. As X gets higher, Y tends towards a maximum limit.

• I wish I could also mark your answer as acceptable, its a different, simpler approach, but I do like the involvement of friction Victor suggested. I will, however, try out your suggestion to see how it feels. Thank you non the less. – Discipol May 23 '17 at 17:38
• @Discipol you can always change the accepted answer. – dot_Sp0T May 23 '17 at 18:13
• The only real difference (maybe benefit) my answer offers is that it condenses the problem into a single variable. The increment is the input, the speed is the output, and the top speed is enforced by the function. Victor's answer will almost certainly achieve a more realistic simulation of physics, though, so for the purposes of realism, Victor's answer is better than mine. – DisturbedNeo May 24 '17 at 8:34