title: Capping Speed on 2D Spaceship
body: <p>So like just about everyone else learning some programming language, I've given myself a spaceship game project.  So far, I have parallax stars and all the nice things I expect from a spaceship game.  The ship stays in the center of the game screen, can rotate 360 degrees, can thrust and coast, and generally behave properly like an object in outer space.  I am having a small issue, however, with capping the ship's speed.</p>
  
  <p>The ship has a dx and dy value (which it translates to the stars through my update() function, so they do the actual moving), and it applies thrust on either the x or y axis, based on the angle it is facing.  I give the ship a max speed (let's say 5) and an acceleration (0.1 per frame).  When thrusting, the ship accelerates as I expect it to.  The thrusters are always on 100%, no throttle control, since I'm using a digital input device (arrow key).  I check the hypotenuse of the triangle made by the ship's current dx and dy to determine its current velocity, and I track its angle of travel, as well (for use in later AI; weapons fire; and when I press the down arrow, the ship uses this value to know what angle it needs to rotate to in order to face opposite its current trajectory -- a sort of help-you-brake-properly autopilot).</p>
  
  <p>The ship has these variables which are updated whenever necessary (this is an abbreviated list, since the rest of the stuff isn't directly relevant):</p>
  
  <pre><code>x_speed
  y_speed
  speed
  move_angle // Used to track direction of motion
  face_angle // Used to determine x_accel and y_accel when thrusting
  x_accel
  y_accel
  accel = 0.1 // Used to determine x_accel and y_accel by multiplying by the dx and dy returned by getDxDy(face_angle)
  max_x
  max_y
  max_speed = 5 // Used to determine max_x and max_y by multiplying by the dx and dy returned by getDxDy(face_angle)
  </code></pre>
  
  <p>I check my dx and dy for acceleration based on the face_angle using this function:</p>
  
  <pre><code>function getDxDy(degree)
  {
  var dx;
  var dy;
  var degree = degree;
  
  dx = -Math.sin(degToRad(degree));
  dy = Math.cos(degToRad(degree));
  
  var dxdy = {
      dx: dx,
      dy: dy
  };
  
  return dxdy;
  }
  </code></pre>
  
  <p>I update the speed every frame of acceleration like so:</p>
  
  <pre><code>x_speed += x_accel
  y_speed += y_accel
  </code></pre>
  
  <p>I have tried capping the speed with multiple different types of checks, and I have tried this:</p>
  
  <pre><code>if (x_speed &gt; max_x)
  {
      x_speed = max_x
  }
  </code></pre>
  
  <p>and I have tried this:</p>
  
  <pre><code>if (x_speed &gt; max_x)
  {
      x_speed -= x_accel
  }
  </code></pre>
  
  <p>The first version caps the speed as desired, but then I am either completely unable to modify my acceleration further, or when I try to reverse course by facing the opposite direction and thrusting, my speed instantly jumps to the opposite direction's max_speed (since the max_speed is constantly calculated based on the ship's current face_angle).  If I change that calculation to the move_angle, then I run into problems when the ship is starting out, and not moving.</p>
  
  <p>The second version has an issue if I happen to stop thrusting before it gets the speed back below the threshold; I about-face and thrust, and instead, my thrust is negative, which causes me to accelerate farther in the direction I'm travelling, exactly opposite to the direction I'm facing.</p>
  
  <p>What I would ideally like is to be able to cap the thrust at some value (5 for example) in any direction, and when I change directions by a few degrees, for the ship to wind up going in that direction after a few seconds of thrust, and when I about-face and thrust, I would like the ship to decelerate as one would expect.</p>
  
  <p>If anyone's ever played the game Escape Velocity, that's the kind of movement and control system I'm working towards -- sort of my "Gold Standard," if you will.</p>
  
  <p>Check out my current somewhat-working code at <a href="http://adrian.thomas-prestemon.com/game/v3/" rel="nofollow noreferrer">http://adrian.thomas-prestemon.com/game/v3/</a></p>
  
  <p>The specific .js file dealing with this can be found at <a href="http://adrian.thomas-prestemon.com/game/v3/js/ships.js" rel="nofollow noreferrer">http://adrian.thomas-prestemon.com/game/v3/js/ships.js</a>.  Please excuse the mess of commented-out code and stuff.  I've been working on this particular bit of the overall problem for a week now.</p>
  
  <p>I have already examined multiple other solutions available online, and the ones most similar to my design use the first "if" example I provided above, which doesn't work for me.  I have looked at the following StackOverflow Q&amp;As as well:</p>
  
  <p><a href="https://stackoverflow.com/questions/2890520/2d-spaceship-movement-math">2D Spaceship movement math</a></p>
  
  <p><a href="https://stackoverflow.com/questions/2560817/2d-trajectory-planning-of-a-spaceship-with-physics">2d trajectory planning of a spaceship with physics</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1365415/programming-a-smooth-change-of-thrust-from-current-velocity-vector-to-a-target-ve">Programming a smooth change of thrust from current velocity vector to a target vector</a></p>
  
  <p>All of these solutions seem quite fantastic, but they all seem a little complicated for my purposes.  I do not plan on implementing any kind of gravity system, as in the first example, and the other two, while related, don't seem to provide anything of immediate use to my specific problem.</p>
  
  <p>Any basic pseudocode and/or other kind of help anyone can provide -- such as pointing me directly to something else that answers my question, which I may have missed -- would be greatly appreciated.  I'm looking for solutions that work with my current setup, but I am absolutely willing to change a little bit of how my ship's movement code functions if it will make this max_speed checking easier.</p>
  
  <p>Thank you for taking the time to read all of this. :)</p>
  
  <p>UPDATE:</p>
  
  <p>I have made some modifications to my code and I got it working much better than it once did.  It's not perfect, but some more experimentation, tweaking, and debugging is all it should take. :)</p>