title: C++ gamedev: truncating float to int
body: <p>I'm making a game in C++ where is a tank that can moves on a stage. The tank has an angle (float, in degrees, and i'm supposing the tank is at 0º when his cannon points to the right), a speed (float), and there is a time constant called &quot;deltaT&quot; (float).</p>
  <p>When the player moves the tank forward, I use trigonometry and the physic equation of position in function of time (I mean X(t), I don't know how it says in English) in order to calculate the new tank's coordinates in the stage.</p>
  <p>This is my problem: due to the passage from float to int, the values closest to zero are not taken into account. So, at certain angles, the tank appears rotated, but moves in a different direction.</p>
  <p>This is what my code does:</p>
  <p>1 - first, I separate the speed in its components X and Y, by using the angle in which the tank is moving:</p>
  <pre><code>    float speedX = this-&gt;speed * cos(this-&gt;angle);
      float speedY = this-&gt;speed * sin(this-&gt;angle);
  </code></pre>
  <p>2 - then use the equation I mentioned above to get the new coordinates:</p>
  <pre><code>    this-&gt;x = (int) ceil(this-&gt;x + (speedX * deltaT));
      this-&gt;y = (int) ceil(this-&gt;y - (speedY * deltaT));
  </code></pre>
  <p>The problem begins at the first step: at certain angles, the value of the cos or the sin is very close to zero.
  So, when I multiply it for speed to obtain, say, speedX, I still got a very low number, and then when I multiply it for deltaT it is still too low, and finally when apply the ceil, that amount is totally lost.</p>
  <p>For example, at 94º, with deltaT = 1.5, and speed = 2, and assuming initial value of X is 400, we have:</p>
  <pre><code>speedX = -0.1395...
  this-&gt;x = 400 //it must be 399.86..., but stays in 400 due to the ceiling
  </code></pre>
  <p>So, in my game the tank appears rotated, but moves straight forward. Also, sometimes it moves well backwards but wrong forward, and viceversa.</p>
  <p>How can I do to make the direction of the tank more accurate? To raise the speed or the value of deltaT are not options since it's about a tank, not a formula 1.</p>