I'm trying to do some matrix transformations & projections in Java for a applet that is going to animate some lines in 3D.
Does this seem ok? I would also like to get some tips about how to do the perspective projection.
public class matrix3D{ private double[][] matrix; public matrix3D(){ matrix = new double[4][4]; matrix = setIdentity(); } private double[][] setIdentity(){ double[][] temp = {{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}}; return temp; } public void resetMatrix(){ matrix = setIdentity(); } public void setXRotation(double x){ double cos = Math.cos(x); double sin = Math.sin(x); double[][] temp = {{1.0, 0.0, 0.0, 0.0}, {0.0, cos, -sin, 0.0}, {0.0, sin, cos, 0.0}, {0.0, 0.0, 0.0, 1.0}}; matrix = multiply(matrix, temp); } public void setYRotation(double y){ double cos = Math.cos(y); double sin = Math.sin(y); double[][] temp = {{cos, 0.0, sin, 0.0}, {0.0, 1.0, 0.0, 0.0}, {-sin, 0.0, cos, 0.0}, {0.0, 0.0, 0.0, 1.0}}; matrix = multiply(matrix, temp); } public void setZRotation(double z){ double cos = Math.cos(z); double sin = Math.sin(z); double[][] temp = {{cos, -sin, 0.0, 0.0}, {sin, cos, 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0}, {0.0, 0.0, 0.0, 1.0}}; matrix = multiply(matrix, temp); } public void setTranslation(double x, double y, double z){ double[][] temp = {{1.0, 0.0, 0.0, x }, {0.0, 1.0, 0.0, y }, {0.0, 0.0, 1.0, z }, {0.0, 0.0, 0.0, 1.0}}; matrix = multiply(matrix, temp); } public void setScaling(double x, double y, double z){ double[][] temp = {{x, 0.0, 0.0, 0.0}, {0.0, y, 0.0, 0.0}, {0.0, 0.0, z, 0.0}, {0.0, 0.0, 0.0, 1.0}}; matrix = multiply(matrix, temp); } private double[][] multiply(double[][] a, double[][] b){ double[][] result = new double[4][4]; result = setIdentity(); for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) for(int k = 0; k < 4; k++) result[j] += a[k] * b[k][j]; return result; } public matrix3D multiply(matrix3D a, matrix3D b){ matrix3D result = new matrix3D(); for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) for(int k = 0; k < 4; k++) result.matrix[j] += a.matrix[k] * b.matrix[k][j]; return result; } public vertex transformVertex(vertex v){ vertex result = new vertex(); result.setX(v.getX() * matrix[0][0] + v.getY() * matrix[0][1] + v.getZ() * matrix[0][2] + v.getW() * matrix[0][3]); result.setY(v.getX() * matrix[1][0] + v.getY() * matrix[1][1] + v.getZ() * matrix[1][2] + v.getW() * matrix[1][3]); result.setZ(v.getX() * matrix[2][0] + v.getY() * matrix[2][1] + v.getZ() * matrix[2][2] + v.getW() * matrix[2][3]); result.setW(v.getX() * matrix[3][0] + v.getY() * matrix[3][1] + v.getZ() * matrix[3][2] + v.getW() * matrix[3][3]); return result; }}