Skip to content

Commit aa92f67

Browse files
committed
Update
1 parent aae734f commit aa92f67

File tree

1 file changed

+129
-10
lines changed

1 file changed

+129
-10
lines changed

src/main/java/math/Matrix4.java

Lines changed: 129 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,58 @@ public Vector3f multiply(Vector3f point) {
8989
}
9090
return new Vector3f(x, y, z);
9191
}
92+
93+
public Matrix4 rotateX(float angle) {
94+
float cos = (float) Math.cos(angle);
95+
float sin = (float) Math.sin(angle);
96+
Matrix4 rotation = new Matrix4(
97+
1, 0, 0, 0,
98+
0, cos, -sin, 0,
99+
0, sin, cos, 0,
100+
0, 0, 0, 1
101+
);
102+
return this.multiply(rotation);
103+
}
104+
105+
public Matrix4 rotateY(float angle) {
106+
float cos = (float) Math.cos(angle);
107+
float sin = (float) Math.sin(angle);
108+
Matrix4 rotation = new Matrix4(
109+
cos, 0, sin, 0,
110+
0, 1, 0, 0,
111+
-sin, 0, cos, 0,
112+
0, 0, 0, 1
113+
);
114+
return this.multiply(rotation);
115+
}
116+
117+
public Matrix4 rotateZ(float angle) {
118+
float cos = (float) Math.cos(angle);
119+
float sin = (float) Math.sin(angle);
120+
Matrix4 rotation = new Matrix4(
121+
cos, -sin, 0, 0,
122+
sin, cos, 0, 0,
123+
0, 0, 1, 0,
124+
0, 0, 0, 1
125+
);
126+
return this.multiply(rotation);
127+
}
128+
129+
public Matrix4 translate(float x, float y, float z) {
130+
Matrix4 translation = new Matrix4(
131+
1, 0, 0, x,
132+
0, 1, 0, y,
133+
0, 0, 1, z,
134+
0, 0, 0, 1
135+
);
136+
return this.multiply(translation);
137+
}
138+
139+
public Matrix4 identity() {
140+
Arrays.fill(values, 0);
141+
values[0] = values[5] = values[10] = values[15] = 1;
142+
return this;
143+
}
92144

93145
public Matrix4 transpose() {
94146
return new Matrix4(
@@ -131,21 +183,66 @@ public Matrix4 transposeLocal() {
131183
return this;
132184
}
133185

134-
public Matrix4 setToIdentity() {
135-
Arrays.fill(values, 0);
136-
values[0] = values[5] = values[10] = values[15] = 1;
137-
return this;
186+
public Matrix4 invert() {
187+
float[] result = new float[16];
188+
float det = determinant();
189+
190+
if (Math.abs(det) < 1E-6f) { // Check for near-zero determinant
191+
throw new ArithmeticException("Matrix is singular or nearly singular. Inversion failed.");
192+
}
193+
194+
// Calculate cofactors
195+
float[] cofactors = new float[16];
196+
for (int i = 0; i < 4; i++) {
197+
for (int j = 0; j < 4; j++) {
198+
cofactors[j * 4 + i] = cofactor(i, j); // Transpose while calculating cofactors
199+
}
200+
}
201+
202+
// Calculate inverse
203+
for (int i = 0; i < 16; i++) {
204+
result[i] = normalize(cofactors[i] / det);
205+
}
206+
207+
return new Matrix4(result);
138208
}
139209

140-
public float[] getValues() {
141-
return Arrays.copyOf(values, values.length);
210+
public float determinant() {
211+
float det = 0.0f;
212+
213+
for (int i = 0; i < 4; i++) {
214+
det += values[i] * cofactor(0, i); // Use row 0 for determinant calculation
215+
}
216+
217+
return det;
142218
}
143219

144-
public void setValues(float... values) {
145-
if (values.length != 16) {
146-
throw new IllegalArgumentException("Matrix4 must have 16 elements.");
220+
private float cofactor(int row, int col) {
221+
float[] minor = new float[9]; // 3x3 minor matrix
222+
int minorIndex = 0;
223+
224+
for (int i = 0; i < 4; i++) {
225+
if (i != row) {
226+
for (int j = 0; j < 4; j++) {
227+
if (j != col) {
228+
minor[minorIndex++] = values[i * 4 + j]; // Correct indexing for 1D array
229+
}
230+
}
231+
}
147232
}
148-
this.values = values;
233+
234+
return (float) Math.pow(-1, row + col) * determinant3x3(minor);
235+
}
236+
237+
private float determinant3x3(float[] matrix) {
238+
return matrix[0] * (matrix[4] * matrix[8] - matrix[5] * matrix[7])
239+
- matrix[1] * (matrix[3] * matrix[8] - matrix[5] * matrix[6])
240+
+ matrix[2] * (matrix[3] * matrix[7] - matrix[4] * matrix[6]);
241+
}
242+
243+
// Helper method to normalize values
244+
private float normalize(float value) {
245+
return Math.abs(value) < 1e-6 ? 0.0f : value; // Convert -0.0 to 0.0
149246
}
150247

151248
public boolean isIdentity() {
@@ -213,6 +310,28 @@ private static void validateNearFarPlanes(float zNear, float zFar) {
213310
}
214311
}
215312

313+
public Matrix4 setToIdentity() {
314+
Arrays.fill(values, 0);
315+
values[0] = values[5] = values[10] = values[15] = 1;
316+
return this;
317+
}
318+
319+
public float[] getValues() {
320+
return Arrays.copyOf(values, values.length);
321+
}
322+
323+
public void setValues(float... values) {
324+
if (values.length != 16) {
325+
throw new IllegalArgumentException("Matrix4 must have 16 elements.");
326+
}
327+
this.values = values;
328+
}
329+
330+
public float get(int row, int col) {
331+
int index = Mathf.toOneDimensionalIndex(row, col, 4);
332+
return (values[index]);
333+
}
334+
216335
@Override
217336
public String toString() {
218337
StringBuilder sb = new StringBuilder();

0 commit comments

Comments
 (0)