Skip to content

Instantly share code, notes, and snippets.

@antoineMoPa
Created March 25, 2016 20:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antoineMoPa/7d0ffe34458e815bbb9d to your computer and use it in GitHub Desktop.
Save antoineMoPa/7d0ffe34458e815bbb9d to your computer and use it in GitHub Desktop.
Y axis 3d rotation of matrix in javascript (matrix is of form [x1,y1,z1,x2,y2,z2,...])
/*
Multiply matrices of 3d coords
multiply_matrix_3d(mat1,mat2) =
[a,b,c,a2,b2,c2,a3,b3,c3] * [x,y,z,x2,y2,z2,...]
*/
function multiply_matrix_3d(mat1,mat2){
var num;
for(var i = 0; i < mat2.length / 3; i++){
for(var j = 0; j < 3; j++){
// initialize
num = 0;
for(var k = 0; k < 3; k++){
num += mat1[3*j + k] * mat2[3*i+k];
}
mat2[3 * i + j] = num;
}
}
}
function rotate_y(theta,mat){
// Build a rotation matrix
var r = [
Math.cos(theta),0,Math.sin(theta),
0,1,0,
(-Math.sin(theta)),0,Math.cos(theta)
];
// Rotate
multiply_matrix_3d(r,mat);
}
rotate_y(0.8,vertex);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment