Chapter 23 (Animation) - A small coding question (probably more of a style question)

Hi,

In Chapter 23 (Animation), once all the transform stuff was implemented, in ./Game/Render
ing.swift, the your sample code contains:

for mesh in meshes {
  let currentLocalTransform =
    mesh.transform?.currentTransform ?? .identity
  uniforms.modelMatrix =
    transform.modelMatrix * currentLocalTransform
  uniforms.normalMatrix = uniforms.modelMatrix.upperLeft
  encoder.setVertexBytes(
    &uniforms,
    length: MemoryLayout<Uniforms>.stride,
    index: UniformsBuffer.index)

So, as I was going through the tutorial, I fat fingered the assignment of uniforms.modelMatrix and, in my code, I had:

 uniforms.modelMatrix =
    currentLocalTransform

Initially, I fixed this by adding the code (to the previous line)

  let currentLocalTransform =
    mesh.transform?.currentTransform ?? transform.modelMatrix

And this “fixed” my typo.

I did go back to your code and found my mis-type.

My question: I’m “pretty sure” (for varying degrees of “pretty sure”) that the code is
equivalent, but can you see any situation where it wouldn’t be? I’m trying to make sure I didn’t “miss” any potential problem.

Thanks for your patience

Just a casual observer here, but it all depends on the values. There could be moments which the results will match, and some that won’t.

1 Like

If you run the final sample code, you will immediately see the difference.

The ground disappears.

The ground is scaled up by 40, so transform.modelMatrix contains

[  0, 40,  0, 0]
[-40,  0,  0, 0]
[  0,  0, 40, 0] 
[  0,  0,  0, 1]

The base transform is scaled by 40, so the local transform shouldn’t be scaled again.