I use these methods to get camera matrix:
public static float[] getViewMatrix(float[] position, float[] rotation) {
float[] matTrans = Matrix3D.copy(position);
float[] matRot = Matrix3D.copy(rotation);
// Transpose rotation matrix.
float tmp;
tmp = matRot[0 * 4 + 1]; matRot[0 * 4 + 1] = matRot[1 * 4 + 0]; matRot[1 * 4 + 0] = tmp;
tmp = matRot[0 * 4 + 2]; matRot[0 * 4 + 2] = matRot[2 * 4 + 0]; matRot[2 * 4 + 0] = tmp;
tmp = matRot[1 * 4 + 2]; matRot[1 * 4 + 2] = matRot[2 * 4 + 1]; matRot[2 * 4 + 1] = tmp;
// Invert translation.
matTrans[3 * 4 + 0] *= -1.0f;
matTrans[3 * 4 + 1] *= -1.0f;
matTrans[3 * 4 + 2] *= -1.0f;
return Matrix3D.multiply(matRot, matTrans);
}public static final float[] multiply(float[] b, float[] a) {
// Return identity if matrices aren't 4x4.
if (a.length != b.length && a.length != 16)
return identity();
// Initialize empty matrix.
float[] result = new float[16];
// Multiply.
int k = 15;
for (int i = 3; i >= 0; i--) {
for (int j = 3; j >= 0; j--) {
result[k] += a[i * 4] * b[j];
result[k] += a[i * 4 + 1] * b[4 + j];
result[k] += a[i * 4 + 2] * b[8 + j];
result[k] += a[i * 4 + 3] * b[12 + j];
k--;
}
}
return result;
}position and rotation float arrays are matrices.The problem is, that viewing arround doesn't quite work on some directions unless I comment these lines:
tmp = matRot[0 * 4 + 1]; matRot[0 * 4 + 1] = matRot[1 * 4 + 0]; matRot[1 * 4 + 0] = tmp; tmp = matRot[0 * 4 + 2]; matRot[0 * 4 + 2] = matRot[2 * 4 + 0]; matRot[2 * 4 + 0] = tmp; tmp = matRot[1 * 4 + 2]; matRot[1 * 4 + 2] = matRot[2 * 4 + 1]; matRot[2 * 4 + 1] = tmp;
Any ideas what I'm doing wrong? This is on android.
Multiply method is tested and is working.
Thank you, Martin.
EDIT:
if I understand correctly, I need to inverse my camera position and rotation matrix to be able to get view from where camera is positioned with original matrix. Is there something wrong with my inverse matrix implementation?






