I often see a similar problem in code that deals with coordinate system transformations. But there is a simple solution! It boils down to properly naming your variables.
When using transformation matrices to transform from one coordinate system "In" into another coordinate system "Out" you have two options to name the matrix:
M_{In,Out} or M_{Out,In}, which can be read as "In to Out" and "Out from In".
Unfortunately what I often see is the first notation. It looks simpler at first, but it is actually backwards, similar to how the german numbers are backwards.
When you chain the transformations you get this weird forth and back:
M_{A to C} = M_{B to C} * M_{A to B}
M_{A,C} = M_{B,C} * M_{A,B}
Compare that to the (not backward) alternative:
M_{C from A} = M_{C from B} * M_{B from A}
M_{C,A} = M_{C,B} * M_{B,A}
Note how the Bs line up, C is the most left in both sides of the equation and A is on the right. When you transform a vector from coordinate system A to, lets say C, it looks like this: "vec_C = M_{C,A} * vec_A". Everything lines up and is in the natural order the transformations are taking place in.
Compared to this, the backward notation is really confusing.
I see people make mistakes when dealing with transformation chains all the time because they use this weird M_{From,To} notation, or worse, no notation involving the coordinate systems at all.
Like take this example: "M = M1 * inv(M3) * M2", what does that even mean.
Find names for the involved coordinate systems and name the transformations accordingly. That could be:
M1 transforms to A from B, M3 is C_B, M2 is C_D. Then M is A_D:
When using transformation matrices to transform from one coordinate system "In" into another coordinate system "Out" you have two options to name the matrix: M_{In,Out} or M_{Out,In}, which can be read as "In to Out" and "Out from In". Unfortunately what I often see is the first notation. It looks simpler at first, but it is actually backwards, similar to how the german numbers are backwards.
When you chain the transformations you get this weird forth and back:
M_{A to C} = M_{B to C} * M_{A to B}
M_{A,C} = M_{B,C} * M_{A,B}
Compare that to the (not backward) alternative:
M_{C from A} = M_{C from B} * M_{B from A}
M_{C,A} = M_{C,B} * M_{B,A}
Note how the Bs line up, C is the most left in both sides of the equation and A is on the right. When you transform a vector from coordinate system A to, lets say C, it looks like this: "vec_C = M_{C,A} * vec_A". Everything lines up and is in the natural order the transformations are taking place in. Compared to this, the backward notation is really confusing.
I see people make mistakes when dealing with transformation chains all the time because they use this weird M_{From,To} notation, or worse, no notation involving the coordinate systems at all.
Like take this example: "M = M1 * inv(M3) * M2", what does that even mean. Find names for the involved coordinate systems and name the transformations accordingly. That could be: M1 transforms to A from B, M3 is C_B, M2 is C_D. Then M is A_D:
B_C = inv(C_B)
A_D = A_B * B_C * C_D