Question

I"m using the following library:

https://github.com/greggman/tdl/blob/master/tdl/math.js

however, the question is about different results i'm getting when i take the same steps in different ways:

//Example 1
m = tdl.math.matrix4.identity();
tdl.math.matrix4.rotateX( m, 90*(Math.PI/180 ) );
tdl.math.matrix4.translate( m, [10,20,30]);

console.log (m); //output down below 1

//Example2
i = tdl.math.matrix4.identity();
r = tdl.math.matrix4.rotationX( 90*(Math.PI/180 ) );
t = tdl.math.matrix4.translation( [10,20,30] );
m = tdl.math.matrix4.mul( i, r );
m = tdl.math.matrix4.mul( m, t );

console.log( m ); // output down below 2

Both ways have the same order of actions, namely

  1. identity
  2. rotate 90 deg around x
  3. translate by 10x, 20y

However, the output is not the same, here is output 1:

 [1, 0, 0, 0, 0, 6.123233995736766e-17, 1, 0, 0, -1, 6.123233995736766e-17, 0, 10, -30, 20.000000000000004, 1]

here is output 2

 [1, 0, 0, 0, 0, 6.123233995736766e-17, 1, 0, 0, -1, 6.123233995736766e-17, 0, 10, 20, 30, 1]

Why is the outcome different, when i'm seemingly taking the same steps?

Was it helpful?

Solution

Because multiplication of matrices is different depending on which matrix is on the left. In other words

matA * matB != matB * matA

Try

i = tdl.math.matrix4.identity();
r = tdl.math.matrix4.rotationX( 90*(Math.PI/180 ) );
t = tdl.math.matrix4.translation( [10,20,30] );
m = tdl.math.matrix4.mul( r, i );
m = tdl.math.matrix4.mul( t, m );

console.log( m ); // output down below 

Outputs

[1, 0, 0, 0, 0, 6.123031769111886e-17, 1, 0, 0, -1, 6.123031769111886e-17, 0, 10, -30, 20.000000000000004, 1] 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top