# ECS: Movement system

I'm a game developer enthusiast. I've decided to implement a game, however I'm pretty confused about all the movement mechanics (kinematics). Here goes a couple questions:

Velocity x acceleration x speed:

1. How do you apply acceleration/deceleration to your movement?
2. When moving (player input), do we either increase the velocity vector or the acceleration/speed value, or actually both? How does it really work?
3. How do you apply object weight to the movement?
4. How do you apply friction & gravity to the movement (should be applied to both axis, as this is going to be a 2D top-down view)?

Questions might be quite simple and/or stupid, but I'm just beginning (and also re-studying physics).

• It looks like you might want to read up on some tutorials for the type of game you're making - searching for "platformer controller" or "fps controller" or similar terms substituting your genre of choice should turn up lots of resources you can learn from. Then, if you have a single focused question not covered by the resources you could find, ask that here. Your question also appears to have nothing whatsoever to do with ECS - the same kinematic formulas apply whether you're using an ECS structure or any other pattern. Commented Jul 17, 2020 at 22:18

Distance is time multiplied by velocity, velocity is time multiplied by acceleration and acceleration is force divided by mass. So you can calculate it in your physics system like this:

acceleration = force / mass;
velocity += acceleration * deltaTime;
position += velocity * deltaTime;


force, acceleration, velocity and position are all two-dimensional vectors in this example: a data-structure with two fields: x and y. Basic math operations on vectors, like adding them or multiplying them by a single number, are not hard to implement. But it's usually easier and more reliable to just use a vector library for your programming language of choice.

Note that not all games simulate all of this. Some games don't bother simulating mass and force and just use constant acceleration. Some games don't even bother with simulating acceleration and just use constant velocity. Some games don't bother with velocity either and just teleport entities to where they are supposed to be. What level of simulation you need depends on the requirements of your game.

When you are following an ECS architecture, then you might want to split these three operations into three different systems ForceSystem, AccelerationSystem and VelocitySystem which each one setting values in the components for the next one.

Gravity is a constant downwards acceleration. So when you have a VelocitySystem, then you just need to create a GravitySystem which adds a constant vector to the velocity (also multiplied by delta-time, of course). On the surface of the real planet Earth, the gravity vector's y-value is -9.81, but that value rarely feels right in games. Which is why most games change that value.

Friction is a force which acts in the opposite direction of the current velocity and increases with the velocity squared. You usually have a friction coefficient which is somewhere between 0 (no friction) and 1 (infinite friction). Unfortunately you can't just multiply the velocity with itself, because then a velocity vector with negative x or y turns positive. So you have to separate it into direction and magnitude, square the magnitude, and then turn it back.

frictionDirection = -normalizeVector(velocity)
frictionMagnitude = magnitudeVector(velocity) * magnitudeVector(velocity)
frictionVector = frictionDirection * frictionMagnitude;
velocity += frictionCoefficient * frictionVector * deltaTime;

• Thanks a lot for your explanation! For now I have a few questions: what exactly is this force and how games usually define/apply it? Also, shouldn't acceleration be a fixed value? Aren't velocity vectors just to represent direction? On player input received, what do we do with velocity and acceleration? I'll ask about the frictions later :) Commented Jul 18, 2020 at 1:04
• @Henri A vector is a direction and a magnitude. Force is usually derived directly from the game mechanics. When it's the player-character, you set it based on the input. For example, when the player presses left and down, you apply a force vector of {x:-5,y:5} to the player entity. That force leads to an acceleration, that acceleration to a change in velocity and that change in velocity to a change in position. I don't understand how you come to the conclusion "acceleration should be a fixed value". It can be if you don't simulate mass. But you wrote you want to. Commented Jul 18, 2020 at 9:05
• Hi, Philipp! I had some work to do on the game engine itself and now I'm back to kinematics again. I think I understand the force part, on which by the way I think you meant x: -5, y: -5, right? How do we use this "force" when dealing with max speeds and acceleration/deceleration factors? How do we calculate them? Suppose I have two objects, o1 and o2. o1 goes from 0 to 100km/h in 1 second, while o2 in 2 seconds. How do we do this? Regarding the friction part, why do we square the velocity vector when finding the friction magnitude? Whos the friction coefficient? Wheres the acceleration? Commented Jul 26, 2020 at 5:03
• @Henri The friction coefficient is the strength of the friction. The reason why things have a max velocity is because friction increases with the velocity squared. The result is that there is a velocity where friction force and acceleration force are equal, so that the object can't accelerate any further. That's the maximum speed. Your question about how to calculate maximum speed from friction coefficient and acceleration force goes beyond your original question and should be asked as a new question. Commented Jul 26, 2020 at 8:31