I'm using JOGL
and Box2d
to write a simple physics based game.
But I'm struggling to create a View
(LookAt) matrix. I'm trying to avoid adding any math library dependencies to project.
Could anyone share an example of how they create View
matrix? So I could modify to work with my custom Matrix4f
class.
Heres my method to setup orthographic projection matrix ..
public Matrix4f ortho(float left, float right, float top, float bottom, float zfar, float znear) {
return new Matrix4f(new float[][] {
new float[] { 2 / (right - left), 0, 0, -((right + left) / (right - left)) },
new float[] { 0, 2 / (top - bottom), 0, -((top + bottom) / (top - bottom)) },
new float[] { 0, 0, -2 / (zfar - znear), -((zfar + znear) / (zfar - znear)) },
new float[] { 0, 0, 0, 1 },
});
}
Heres my custom Matrix4f
class ..
public class Matrix4f {
public float[] values;
public Matrix4f() {
this.values = new float[16];
}
public Matrix4f(float[] values) {
this.values = values;
}
public Matrix4f(float[][] values) {
load(values);
}
public void load(float[][] values) {
this.values = new float[] {
values[0][0], values[0][1], values[0][2], values[0][3],
values[1][0], values[1][1], values[1][2], values[1][3],
values[2][0], values[2][1], values[2][2], values[2][3],
values[3][0], values[3][1], values[3][2], values[3][3]
};
}
public float get(int x, int y) {
int position = x + (y * 4);
return this.values[position];
}
public float[] getValues() {
return this.values;
}
}