Created
March 25, 2016 20:23
-
-
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,...])
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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