We need to create a couple methods for later uses. All of this will still be inside our code.

var TankGame = (function() {
	// code
}

We will add a function called deg2rad that will pass the variable degrees. The point of this function is to return the degrees into radians.

function deg2rad(degrees) {
    return degrees * (Math.PI/180);
}

To understand what our vector2d class does, we have to understand what a vector is. A Vector2 has a 2D direction, like a xy point in a 2D space, or the position of a joystick stick, or the uv offset of a point on a 2D texture.

Our Map is a vector because it is a flat (2d) area where our tanks move.

Under this we want a class called Vector2d. We also need to add a constructor to the class so that wherever we call it, we can add values to their fields.

class Vector2d {
constructor(x, y) {
      if (typeof x == 'undefined' || typeof  y == 'undefined') {
        throw "Invalid arguments";
      }
      this.x = x;
      this.y = y;
}

// more code
}

We have an if statement inside the constructor that checks to make sure the x and y are not undefined. If either of these variables are undefined we set their respective fields to null.

Next we will add another method inside the vector2d class. The point of this method is to rotate the coordinates clockwise. Simply we will turn the radians into “-radians”. We will pass a variable that is radians

rotate(radians) {
      // Rotates coordinates counterclockwise
      //radians=-radians
      if (radians != 0) {
        var x = this.x;
        var y = this.y;
        this.x = x * Math.cos(radians) - y * Math.sin(radians);
        this.y = x * Math.sin(radians) + y * Math.cos(radians);
      }
      return this; 
    }

Our next method will add a new vector to the instance of the Vector2d class. The point of this is to have the ability to add new vectors.

add(vector) {
      if (vector instanceof Vector2d) {
        this.x += vector.x;
        this.y += vector.y;
      }
      else throw "Invalid argument";
      return this;
    }

We also need the ability to multiply values to our existing x and y values. This multiply method will first have to check that the value that is passed in the multiply argument is a number.

subtract(vector) {
      if (vector instanceof Vector2d) {
        this.x -= vector.x;
        this.y -= vector.y;
      }
      else throw "Invalid argument";
      return this;
    }