Translate matrix 3x3 vs 4x4

Why, if the dimension of matrices is 3x3 for using a translate matrix, the correct result is obtained when we multiply a vector by a matrix, and when using 4x4 matrices, the correct result is obtained when we multiply a matrix by a vector?

func check() {
    
    let delta: SIMD2<Float> = [100, 100]
    
    var matrix3x3 = matrix_identity_float3x3
    matrix3x3[0, 2] = delta.x
    matrix3x3[1, 2] = delta.y
    
    var matrix4x4 = matrix_identity_float4x4
    matrix4x4[3, 0] = delta.x
    matrix4x4[3, 1] = delta.y
    
    let dote3: SIMD3<Float> = [10, 10, 1]
    let dote4: SIMD4<Float> = [10, 10, 0, 1]
    
    let r1 = matrix3x3 * dote3 // SIMD3<Float>(10.0, 10.0, 2001.0)      // bad
    let r2 = dote3 * matrix3x3 // SIMD3<Float>(110.0, 110.0, 1.0)       // good
    let r3 = matrix4x4 * dote4 // SIMD4<Float>(110.0, 110.0, 0.0, 1.0)  // good
    let r4 = dote4 * matrix4x4 // SIMD4<Float>(10.0, 10.0, 0.0, 2001.0) // bad

    
}

Your 3x3 matrix is:

 1   0   0
 0   1   0
100 100  1

Your 4x4 matrix is:

 1   0   0  100
 0   1   0  100
 0   0   1   0
 0   0   0   1

Does this help explain what you're seeing?

Correct is quite a needlessly strong word and doesn't tell us much since we don't know what exactly you're confusing about. Your confusion might be from one of these:

  • You're treating the vector as a row vector when doing vector * matrix, and treating it as a column vector when doing matrix * vector (the multiplication wouldn't work otherwise).
  • The subscript is in a column-row pair, so matrix[column, row].
1 Like

Thanks a lot. I got the matrices from two different websites.

1 Like
Terms of Service

Privacy Policy

Cookie Policy