# phaser matrix rotate

## Phaser Matrix Rotate

If you are working with Phaser, you may need to rotate a matrix. The process of rotating a matrix is very useful when it comes to game development, especially in 2D games. A matrix in Phaser is a 2D array of values that represents the location of objects in the game world. Rotating a matrix can help you change the position of objects in the game world.

### Method 1: Using Phaser.Matrix.rotate

The first method to rotate a matrix in Phaser is by using the built-in function Phaser.Matrix.rotate(). This function takes two arguments, the matrix to rotate and the angle in radians to rotate by.

```
var matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
var angle = Math.PI / 2;
Phaser.Matrix.rotate(matrix, angle);
console.log(matrix);
```

In the above code, we first define a matrix with some values. We then define an angle of rotation in radians. Finally, we call the Phaser.Matrix.rotate() function and pass in the matrix and angle as arguments. The console.log() statement will output the rotated matrix.

### Method 2: Using Math.cos and Math.sin functions

The second method to rotate a matrix in Phaser is by using the Math.cos() and Math.sin() functions along with some basic math calculations. The following code shows how to implement this method:

```
var matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
var angle = Math.PI / 2;
var cos = Math.cos(angle);
var sin = Math.sin(angle);
for (var i = 0; i < matrix.length; i++) {
var row = matrix[i];
var x = row[0];
var y = row[1];
row[0] = x * cos + y * sin;
row[1] = -x * sin + y * cos;
}
console.log(matrix);
```

In the above code, we first define a matrix with some values. We then define an angle of rotation in radians. We then calculate the cosine and sine of the angle. Finally, we loop through each row of the matrix and perform the rotation calculation using the cosine and sine values. The console.log() statement will output the rotated matrix.