Rotation Matrix Java -
i've been following tutorial creating game engine , when got calculating 3d rotation matrix ran problem believe matrix isn't being calculated properly. when try rotate matrix 0 degrees on x, y, , z axis, should getting identity matrix rotation matrix should this:
rotation matrix: 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0
but end matrix looks this:
rotation matrix: 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
here code calculating rotation matrix directly tutorial:
public class matrix4f { private float[][] m; //generate 4x4 array inital matrix represent homogeneous //coordinates: x, y, z, w public matrix4f() { m = new float[4][4]; } public matrix4f initrotation(float x, float y, float z) { //generate rotation matrices x, y, , z matrix4f rx = new matrix4f(); matrix4f ry = new matrix4f(); matrix4f rz = new matrix4f(); //convert x,y, , z radians angle calculations x = (float)math.toradians(x); y = (float)math.toradians(y); z = (float)math.toradians(z); //calculate rotation matrices x, y, z rz.m[0][0] = (float)math.cos(z);rz.m[0][1] = (float)math.sin(z);rz.m[0][2] = 0; rz.m[0][3] = 0; rz.m[1][0] = -(float)math.sin(z);rz.m[1][1] = (float)math.cos(z);rz.m[1][2] = 0; rz.m[1][3] = 0; rz.m[2][0] = 0; rz.m[2][1] = 0; rz.m[2][2] = 1; rz.m[2][3] = 0; rz.m[3][0] = 0; rz.m[3][1] = 0; rz.m[3][2] = 0; rz.m[3][3] = 1; rx.m[0][0] = 1; rx.m[0][1] = 0; rx.m[0][2] = 0; rx.m[0][3] = 0; rx.m[1][0] = 0; rx.m[1][1] = (float)math.cos(x);rx.m[1][2] = -(float)math.sin(x);rx.m[1][3] = 0; rx.m[2][0] = 0; rx.m[2][1] = -(float)math.sin(x);rx.m[2][2] = (float)math.cos(x);rx.m[2][3] = 0; rx.m[3][0] = 0; rx.m[3][1] = 0; rx.m[3][2] = 0; rx.m[3][3] = 1; ry.m[0][0] = (float)math.cos(y);ry.m[0][1] = 0; ry.m[0][2] = -(float)math.sin(y);ry.m[0][3] = 0; ry.m[1][0] = 0; ry.m[1][1] = 1; ry.m[1][2] = 0; ry.m[1][3] = 0; ry.m[2][0] = (float)math.sin(y);ry.m[2][1] = 0; ry.m[2][2] = (float)math.cos(y);ry.m[2][3] = 0; ry.m[3][0] = 0; ry.m[3][1] = 0; ry.m[3][2] = 0; ry.m[3][3] = 1; //calculate final rotation matrix multiplying 3 rotation matrices m = rz.mul(ry.mul(rx)).getm(); //a simple way print out full matrix system.out.println("rotation matrix:"); for(int = 0; < 4; i++) { system.out.println(""); for(int j = 0; j < 4; j++) { system.out.print(m[i][j] + " "); } } system.out.println(""); return this; } //defining multiplication operation matrices public matrix4f mul(matrix4f r) { matrix4f res = new matrix4f(); for(int = 0; < 4; i++) { for(int j = 0; j < 4; j++) { res.set(i, j, m[i][0]*r.get(0, i) + m[i][1]*r.get(1, i) + m[i][2]*r.get(2, i) + m[i][3]*r.get(3, i)); } } return res; } //get matrix in array form public float[][] getm() { return m; } //get individual element of matrix public float get(int x, int y) { return m[x][y]; } //set whole matrix equal matrix m public void setm(float[][] m) { this.m = m; } //set individual element of matrix value public void set(int x, int y, float value) { m[x][y] = value; } }
then when try run in main method so:
(new matrix4f()).initrotation(0, 0, 0);
i
rotation matrix: 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
even though according rotation matrix operations should getting:
rotation matrix: 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0
edit: after more debugging, i've resolved problem after more debugging problem in matrix mul method. apologize laziness. i've been programming while , don't sort of thing often, got frustrated program.
the correction matrix multiplication algorithm is:
public matrix4f mul(matrix4f r) { matrix4f res = new matrix4f(); for(int = 0; < 4; i++) { for(int j = 0; j < 4; j++) { res.set(i, j, m[i][0]*r.get(0, j) + m[i][1]*r.get(1, j) + m[i][2]*r.get(2, j) + m[i][3]*r.get(3, j)); } } return res; }
instead of:
public matrix4f mul(matrix4f r) { matrix4f res = new matrix4f();
for(int = 0; < 4; i++) { for(int j = 0; j < 4; j++) { res.set(i, j, m[i][0]*r.get(0, i) + m[i][1]*r.get(1, i) + m[i][2]*r.get(2, i) + m[i][3]*r.get(3, i)); } } return res; }
i think multiplication function incorrect logically. in multiplication function end multiplying first raw , column every cell. changing j shown below calculate multiplication correctly.
public matrix4f mul(matrix4f r) { matrix4f res = new matrix4f(); for(int = 0; < 4; i++) { for(int j = 0; j < 4; j++) { res.set(i, j, m[i][0]*r.get(0, j) + m[i][1]*r.get(1, j) + m[i][2]*r.get(2, j) + m[i][3]*r.get(3, j)); } } return res; }
secondly, can try debug code using breakpoints in multiplication function. can debug using standard ide (eclipse) debug feature. correct idea of execution step step , able correct code yourself.
hope helps.
Comments
Post a Comment