title: Stuck in making 2D game movement engine
body: <p>I am making a movement engine for my top-down 2D game, but I got stuck trying to solve the following problem:</p>
  
  <ul>
  <li>The player can move using the arrow keys, which accelerate you in the respective directions. There is friction so you stop moving after releasing the keys, though not instantly.</li>
  <li>When you hold two perpendicular keys, you accelerate in this 45° direction the same speed as you would on one axis</li>
  <li>There is a maximum speed which above you cannot accelerate <strong>by walking</strong>, this also limits your maximum walking speed, obviously. You can get knocked away, and thus exceed this speed however.</li>
  <li>If you move faster than max. walkspeed, you can slow down faster if you hold keys in the opposite direction(s)</li>
  </ul>
  
  <p>Pseudocode for the first point, without friction:</p>
  
  <pre><code>gameTick(){
  
    tempX += LeftKeyHeld ? -1 : 0;
    tempX += RightKeyHeld ? 1 : 0;
    tempY += UpKeyHeld ? -1 : 0;
    tempY += DownKeyHeld ? 1 : 0;
    ratio = 0.71;
  
    if( |tempX| == |tempY| ) {
      tempX =tempX* ratio;
      tempY =tempY* ratio;
    }
    player.x += tempX;
    player.y += tempY;
  }
  </code></pre>
  
  <p>I can solve the friction (getting the length of the movement vector, reducing it by the friction, projecting it back with the same x:y ratio), however, I can't wrap my head around getting the maxSpeed done. </p>
  
  <p>I tried a solution of not allowing the player to walk at all when above maxSpeed, but this violates point 4. Also, it had the nasty side-effect, that when you moved at MaxSpeed to the left, and started pressing down, the movement direction didn't, or barely changed.</p>
  
  <p>Then I started thinking about numerous products, differences and other stuff with the vectors, but I mostly couldn't follow it anymore or ran into early problems. </p>
  
  <p>So in conclusion, could someone explain a system which fulfills all the points above, or point to an article which explains how a system like this could be implemented? Don't worry about suggesting something complex, I can grasp even difficult concepts given some time.</p>
  
  <p>Thanks for your help!</p>