I'm building a multiplayer game of tanks in node js. at the moment I have the movement smooth and mastered but my problem is that at them moment it's basically just unrealistic.

The tanks starts at 0 km/h but upon pressing w/Arrow up key it goes straight to 10 km/h, as you can imagine with any vehicle that's just unrealistic to do.

I want to know how to make that speed up happen without it being too bulky or confusing. I'm a beginner programmer so I'm not too informed in the world of programming. Can somebody help me with this?

I figured it would have to do with an acceleration command but I have no idea how to do that either.

  • \$\begingroup\$ I don't know which platform you are working on for your clients but simple sending and receiving data doesn't work for most of real time games. you need some techniques like movement predictions and online physics simulations and... there are lots of resources on internet to learn from. a good and short one is gafferongames.com \$\endgroup\$
    – virtouso
    Commented Oct 16, 2017 at 8:17
  • \$\begingroup\$ I am using server.io for the server side of things and for the client handler. \$\endgroup\$
    – J.R
    Commented Oct 16, 2017 at 8:25
  • \$\begingroup\$ Would you already know how to do this in a single-player game? \$\endgroup\$
    – Philipp
    Commented Oct 16, 2017 at 8:26
  • \$\begingroup\$ I have no idea how to do it at all. \$\endgroup\$
    – J.R
    Commented Oct 16, 2017 at 8:39

1 Answer 1


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.

  • \$\begingroup\$ Oh, I kind of get it now. I'll try doing that instead of what I've been playing with. \$\endgroup\$
    – J.R
    Commented Oct 16, 2017 at 8:39

You must log in to answer this question.

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