I have this array that I pass into a function that's suppose to rotate the array 90 deg and return the result. I then want to check whether the new array is colliding with other things on the canvas.
let newArray = [
[0,0,1],
[1,1,1],
[0,0,0]
];
let test = rotate(newArray);
if ( collision(test) ){
draw(newArray);
} else {
draw(test);
}
Here's the problem. I think rotate() is changing the newArray-variable so it doesn't matter what the if-statement evaluate to, the draw() function will always do the same thing.
I want to select the array to draw after checking if the rotated matrix collides with other stuff.
function rotate(matrix) {
matrix = matrix.reverse();
for (var i = 0; i < matrix.length; i++) {
for (var j = 0; j < i; j++) {
var temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
return matrix
}
function collision(mat){
let collision = false;
for ( let i = 0; i < mat.length; i++){
for ( let j = 0; j < mat.length; j++){
if (mat[i][j] == 1){
let test = ctx.getImageData(x + 10*j +10, y + 10*i, 1, 1);
if (test.data != "255,255,255,255" && test.data != "0,0,0,0"){
collision = true;
}
}
}
}
return collision;
}