The order of matrix multiplication

Hi I hope you are having a good day.

How does one determine the correct order of matrix multiplication in order to get the desired result, for example when creating an fps cam one must fist multiply along the up vector then the right vector or else the camera behaves weirdly.
I know that because I read about it in a book but I want to know how to determine the order on my own without having to go through trial and error.

2.Does gimbal lock affect matrices.

Thanks any help will be greatly appreciated.

what are you trying to do exactly?

Nothing for now. I just want to understand the math.

It is a processing order. Imagine treasure hunt:

I) to the entrance of the garden head north

  1. go forward 10 steps
  2. head left
  3. go forward 5 steps
  4. head right
  5. go forward 7 steps
  6. dig a whole to get the treasure chest

Try to do that in a different order. It will not work. Imagine there are walls, holes, bridges doors. Just using different turn angels will produce different results.

There are other solutions that can be solved with the same steps (in other order) but most likely it does not work.

The trick with matrices is that you can express all these steps as matrix. The final result is a matrix again. This allows to combine the steps into a single matrix. Lets express the above example as matrices:

I) M0 = I [Identity matrix = no transformation]

  1. T10 = T(10,0,0) [Translation matrix go 10 steps along X]
  2. Rl = R(0,0,-90°) [Rotation along Z left)]
  3. T5 = T(5,0,0) [Translation matrix go 10 steps along X]
  4. Rr = R(0,0,90°) [Rotation along Z right)]
  5. T7 = T(7,0,0) [Translation matrix go 10 steps along X]
  6. done

The path is:

M0 * T10 * Rl * T5 * Rr * T7 = Mcombined

Now imagine you want to draw the character there. The character has 10.000 vertices. You would need calculate the above formula 10.000 times. As all vertices inherit the same transformation you can multiply the vertex position with the combined matrix Mcombined.

Vfinal = Vorignal * MFinal

now imagine your character turn left again (Rl). You could do

M0 * T10 * Rl * T5 * Rr * T7 * Rl = Mnext
or
you use the current transformation

Mcombined * Rl = Mnext

You do not need to remember all past steps as they are already in the combined matrix.
You place the character mesh at the new location with:

Vfinal = Vorignal * Mnext

A different example - Orbiting object:

  1. move the object at the orbit center (move left by radius r)
  2. turn the object by the desired angle a around Z
  3. move the object back to orbit (move right by radius r)

Matrix:

  1. Tc = Translation(0,r,0)
  2. Ra = Rotation(0,0,a)
  3. To = Translation(0,-r,0)

Tc * Ra * To = Mstep

You get an orbit animation by:

currentTransformation = currentTransformation * Mstep
draw Mesh
currentTransformation = currentTransformation * Mstep
draw Mesh
currentTransformation = currentTransformation * Mstep
draw Mesh
currentTransformation = currentTransformation * Mstep
draw Mesh

I hope this helps a bit