title: 2D Vector rotation in Java
body: <p>I try to make a simple Race-Game.
  For that I got a car with a vector for the position and one for the direction it is facing.</p>
  
  <p>I have an update and an input method:</p>
  
  <pre><code>public void update(double delta) {
      float rotation = 0;
      if(movement.normalized().getY() &lt; 0) {
          rotation = (float) (2*Math.PI - Math.acos(movement.normalized().getX()));
      } else {
          rotation = (float) Math.acos(movement.normalized().getX());
      }
  
      pos = pos.add(new Vector3f((float) (Math.cos(rotation) * (movement.length() - 2) * delta), (float) (Math.sin(rotation) * (movement.length() - 2) * delta), 0f));
  
      v1 = new Vertex(new Vector3f((float)(pos.getX() + Math.cos((rotation + basicAngel)) * radius), (float) (pos.getY() + (Math.sin((rotation + basicAngel)) * radius)), 0), new Vector2f(0, 0));
      v2 = new Vertex(new Vector3f((float)(pos.getX() + Math.cos(((2*Math.PI - basicAngel) + rotation)) * radius), (float) (pos.getY() + Math.sin(((2*Math.PI - basicAngel) + rotation)) * radius), 0), new Vector2f(0, 0));
      v3 = new Vertex(new Vector3f((float)(pos.getX() + Math.cos((Math.PI + rotation + basicAngel)) * radius), (float)(pos.getY() + Math.sin((Math.PI + rotation + basicAngel)) * radius), 0), new Vector2f(0, 0));
      v4 = new Vertex(new Vector3f((float)(pos.getX() + Math.cos(((Math.PI - basicAngel) + rotation)) * radius), (float)(pos.getY() + Math.sin(((Math.PI - basicAngel) + rotation)) * radius), 0), new Vector2f(0, 0));
      v5 = new Vertex(new Vector3f((float)(pos.getX() + Math.cos((windowAngel + rotation)) * windowRadius), (float)(pos.getY() + Math.sin((windowAngel + rotation)) * windowRadius), 0), new Vector2f(0, 0));
      v6 = new Vertex(new Vector3f((float)(pos.getX() + Math.cos(((2*Math.PI - windowAngel) + rotation)) * windowRadius), (float)(pos.getY() + Math.sin(((2*Math.PI - windowAngel) + rotation)) * windowRadius), 0), new Vector2f(0, 0));
  }
  </code></pre>
  
  <p>The first if-statement checks whether the y component of the normalized vector below 0 is.<br>
  That makes sure that the car rotates below the horizontal axis.<br>
  The next line applies the rotation and speed to the position.<br>
  The block with v1...v4 calculates the corners of the car to render it.<br>
  v5 and v6 are the corners of the cars window.</p>
  
  <pre><code>public void input() {
  
      float tempX = movement.normalized().getX();
      float tempY = movement.normalized().getY();
      float tempLength = movement.length();
  
      if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
          movement = movement.rotate(3);
      }
  
      if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
          movement = movement.rotate(-3);
      }
  
      if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
          if (tempLength &lt;= 7) {
  
              movement.setX((float) (Math.cos(Math.acos(movement.normalized().getX())) * (tempLength + 0.2)));
              movement.setY((float) (Math.sin(Math.acos(movement.normalized().getX())) * (tempLength + 0.2)));
  
          }
      } else {
          if(tempLength &gt; 2) {
  
              movement.setX((float) (Math.cos(Math.acos(movement.normalized().getX())) * (tempLength - 0.05)));
              movement.setY((float) (Math.sin(Math.acos(movement.normalized().getX())) * (tempLength - 0.05)));
  
          }
      }
  
      if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
          if (tempLength &gt;= 0) {
  
              movement.setX((float) (Math.cos(Math.acos(movement.normalized().getX())) * (tempLength - 0.2)));
              movement.setY((float) (Math.sin(Math.acos(movement.normalized().getX())) * (tempLength - 0.2)));
  
          }
      } else {
          if (tempLength &lt; 2) {
  
              movement.setX((float) (Math.cos(Math.acos(movement.normalized().getX())) * (tempLength + 0.05)));
              movement.setY((float) (Math.sin(Math.acos(movement.normalized().getX())) * (tempLength + 0.05)));
  
          }
      }
  
      if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
          reset();
      }
  }
  </code></pre>
  
  <p>The first two if-statements are checking whether the left or right key were pressed. If that happened the movement vetor gets rotated.
  The next two statements are checking the up and down key and appliing the speed to the movement vector by using this formula:  </p>
  
  <pre><code>x = cos a * (l + speed)
  y = sin a * (l + speed)
  </code></pre>
  
  <p>where a is the angle of the movement vector<br>
  and l is the length of the movement vector</p>
  
  <p>Everything should work fine and it does, except that it miscalculates sometimes so that the car goes in a different direction as expected.</p>
  
  <p>Can someone help me finding the mistake?</p>