Skip to content

Commit 74c4ce1

Browse files
vamshi567bhvamshidevara
authored andcommitted
fix mult() inconsistency: make 3x3 and NxN use left-multiplication like 4x4
1 parent 71d062a commit 74c4ce1

3 files changed

Lines changed: 22 additions & 30 deletions

File tree

‎src/math/Matrices/Matrix.js‎

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ export class Matrix extends MatrixInterface {
521521
* const matrix1 = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]);
522522
* const matrix2 = new p5.Matrix([9, 8, 7, 6, 5, 4, 3, 2, 1]);
523523
* matrix1.mult(matrix2);
524-
* console.log(matrix1.matrix); // Output: [30, 24, 18, 84, 69, 54, 138, 114, 90]
524+
* console.log(matrix1.matrix); // Output: [90, 114, 138, 54, 69, 84, 18, 24, 30]
525525
*
526526
* // Multiplying a 4x4 matrix with another 4x4 matrix
527527
* const matrix4x4_1 = new p5.Matrix(4); // Identity matrix
@@ -536,7 +536,7 @@ export class Matrix extends MatrixInterface {
536536
* const matrix2 = new p5.Matrix([9, 8, 7, 6, 5, 4, 3, 2, 1]);
537537
* console.log("Before multiplication:", matrix1.matrix);
538538
* matrix1.mult(matrix2);
539-
* console.log("After multiplication:", matrix1.matrix); // Output: [30, 24, 18, 84, 69, 54, 138, 114, 90]
539+
* console.log("After multiplication:", matrix1.matrix); // Output: [90, 114, 138, 54, 69, 84, 18, 24, 30]
540540
* }
541541
*/
542542
mult(multMatrix) {
@@ -1718,8 +1718,8 @@ export class Matrix extends MatrixInterface {
17181718
for (let j = 0; j < this.#sqDimention; j++) {
17191719
for (let k = 0; k < this.#sqDimention; k++) {
17201720
result[i * this.#sqDimention + j] +=
1721-
this.matrix[i * this.#sqDimention + k] *
1722-
multMatrix[k * this.#sqDimention + j];
1721+
multMatrix[i * this.#sqDimention + k] *
1722+
this.matrix[k * this.#sqDimention + j];
17231723
}
17241724
}
17251725
}
@@ -1730,7 +1730,7 @@ export class Matrix extends MatrixInterface {
17301730
/**
17311731
* This function is only for 3x3 matrices.
17321732
* multiply two mat3s. It is an operation to multiply the 3x3 matrix of
1733-
* the argument from the right. Arguments can be a 3x3 p5.Matrix,
1733+
* the argument from the left. Arguments can be a 3x3 p5.Matrix,
17341734
* a Float32Array of length 9, or a javascript array of length 9.
17351735
* In addition, it can also be done by enumerating 9 numbers.
17361736
*
@@ -1740,27 +1740,19 @@ export class Matrix extends MatrixInterface {
17401740
* @chainable
17411741
*/
17421742
#mult3x3(_src) {
1743-
// each row is used for the multiplier
1744-
let b0 = this.mat3[0];
1745-
let b1 = this.mat3[1];
1746-
let b2 = this.mat3[2];
1747-
this.mat3[0] = b0 * _src[0] + b1 * _src[3] + b2 * _src[6];
1748-
this.mat3[1] = b0 * _src[1] + b1 * _src[4] + b2 * _src[7];
1749-
this.mat3[2] = b0 * _src[2] + b1 * _src[5] + b2 * _src[8];
1750-
1751-
b0 = this.mat3[3];
1752-
b1 = this.mat3[4];
1753-
b2 = this.mat3[5];
1754-
this.mat3[3] = b0 * _src[0] + b1 * _src[3] + b2 * _src[6];
1755-
this.mat3[4] = b0 * _src[1] + b1 * _src[4] + b2 * _src[7];
1756-
this.mat3[5] = b0 * _src[2] + b1 * _src[5] + b2 * _src[8];
1757-
1758-
b0 = this.mat3[6];
1759-
b1 = this.mat3[7];
1760-
b2 = this.mat3[8];
1761-
this.mat3[6] = b0 * _src[0] + b1 * _src[3] + b2 * _src[6];
1762-
this.mat3[7] = b0 * _src[1] + b1 * _src[4] + b2 * _src[7];
1763-
this.mat3[8] = b0 * _src[2] + b1 * _src[5] + b2 * _src[8];
1743+
const a0 = this.mat3[0], a1 = this.mat3[1], a2 = this.mat3[2];
1744+
const a3 = this.mat3[3], a4 = this.mat3[4], a5 = this.mat3[5];
1745+
const a6 = this.mat3[6], a7 = this.mat3[7], a8 = this.mat3[8];
1746+
1747+
this.mat3[0] = _src[0] * a0 + _src[1] * a3 + _src[2] * a6;
1748+
this.mat3[1] = _src[0] * a1 + _src[1] * a4 + _src[2] * a7;
1749+
this.mat3[2] = _src[0] * a2 + _src[1] * a5 + _src[2] * a8;
1750+
this.mat3[3] = _src[3] * a0 + _src[4] * a3 + _src[5] * a6;
1751+
this.mat3[4] = _src[3] * a1 + _src[4] * a4 + _src[5] * a7;
1752+
this.mat3[5] = _src[3] * a2 + _src[4] * a5 + _src[5] * a8;
1753+
this.mat3[6] = _src[6] * a0 + _src[7] * a3 + _src[8] * a6;
1754+
this.mat3[7] = _src[6] * a1 + _src[7] * a4 + _src[8] * a7;
1755+
this.mat3[8] = _src[6] * a2 + _src[7] * a5 + _src[8] * a8;
17641756

17651757
return this;
17661758
}

‎src/webgl/p5.Camera.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1609,7 +1609,7 @@ class Camera {
16091609
// and multiply it to mat1 from the right.
16101610
// This matrix represents the difference between the two.
16111611
// 'deltaRot' means 'difference of rotation matrices'.
1612-
const deltaRot = rotMat1.mult(rotMat0.copy().transpose()); // mat1 is 3x3
1612+
const deltaRot = rotMat0.copy().transpose().mult(rotMat1); // mat1 is 3x3
16131613

16141614
// Calculate the trace and from it the cos value of the angle.
16151615
// An orthogonal matrix is just an orthonormal basis. If this is not the identity

‎test/unit/math/p5.Matrix.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,19 +329,19 @@ suite('p5.Matrix', function () {
329329
const multMatrix = new p5.Matrix([1, 1, 1, 0, 1, 1, 1, 0, 1]);
330330
// When taking a matrix as an argument
331331
m.mult(multMatrix);
332-
expect(toArray(m.mat3)).toEqual([ 4, 3, 6, 10, 9, 15, 16, 15, 24 ]);
332+
expect(toArray(m.mat3)).toEqual([ 12, 15, 18, 11, 13, 15, 8, 10, 12 ]);
333333
});
334334

335335
test('mult a 3x3 matrix with array as argument', function () {
336336
const m = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]);
337337
m.mult([1, 1, 1, 0, 1, 1, 1, 0, 1]);
338-
expect(toArray(m.mat3)).toEqual([ 4, 3, 6, 10, 9, 15, 16, 15, 24 ]);
338+
expect(toArray(m.mat3)).toEqual([ 12, 15, 18, 11, 13, 15, 8, 10, 12 ]);
339339
});
340340

341341
test('mult a 3x3 matrix with arguments non array', function () {
342342
const m = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]);
343343
m.mult(1, 1, 1, 0, 1, 1, 1, 0, 1);
344-
expect(toArray(m.mat3)).toEqual([ 4, 3, 6, 10, 9, 15, 16, 15, 24 ]);
344+
expect(toArray(m.mat3)).toEqual([ 12, 15, 18, 11, 13, 15, 8, 10, 12 ]);
345345
});
346346

347347
test('column() and row()', function () {

0 commit comments

Comments
 (0)