Chapter 9 - parent relation with camera

Hello ! sorry for my English, but I am French…
Thank you for your wonderful book !

I have a little problem with the parent relation with the camera :
I develop a space navigation simulation with metal and I use the final project of the chapter 9 as starting point. When I make my space ship a child of the camera (as in the book with camera and car), all works very well. But I need to place the camera as a child of the space ship (embarqued camera), and that doesn’t work at all, and I really don’t understand what is the reason… I have spent hours on this topics, without result… The camera doesn’t want at all to be a child of a Prop !.. (it compiles well but the result is the camera is not the child…)

I also use quaternions to navigate with the ship to not have gimbal-lock. Quaternions work very well with the space ship (a Prop in the code). But it doesn’t work at all with the camera. I am obliged to use camera.rotation with camera.quaternion.imag, and of course I obtain gimbal lock… and it is for this reason I want to put the camera as a child of the space ship.

Thank you to every body could help me to understand my errors…

@caroline Can you please help with this when you get a chance? Thank you - much appreciated! :]

@jplozach - Merci beaucoup :slight_smile:

I’ll deal with the camera problem first.

The camera is not set up properly to be a child of nodes. (Program flaw :unamused:)

However, if you want to experiment, you can try this. This code works for position but does not work for rotation, but hopefully you will see from this code why placing a camera as child of a Prop doesn’t work currently.

Using the final sample code, create a new Swift file and add this code:

import Foundation

class NewScene: Scene {
  let ground = Prop(name: "large-plane")
  let car = Prop(name: "racing-car")

  override func setupScene() {
    ground.tiling = 32
    add(node: ground)
    car.rotation = [0, radians(fromDegrees: 90), 0]
    car.position = [-0.8, 0, 0]
    add(node: car)
    camera.position = [0, 1.2, -4]
    
    remove(node: camera)
    add(node: camera, parent: car, render: false)
    
    inputController.player = car
  }
}

Change ViewController to use this scene instead of CarScene.

In Camera class, add a new property:

  var worldViewMatrix: float4x4 {
    if let parent = parent {
      return parent.worldTransform.inverse * self.viewMatrix
    }
    return viewMatrix
  }

In Scene’s update(deltaTime:), change the viewMatrix assignment to:

 uniforms.viewMatrix = camera.worldViewMatrix

And build and run. Rotate the camera using the right arrow key until you see the car. Now use the wasd keys to move the car, and the camera will follow along as a child.

However, rotation with arrow keys doesn’t currently work, as the center of rotation needs to be the same as the car. (Needs a bit of math work on it)

From this example I hope you can see why the original code doesn’t work. Scene was using viewMatrix directly, whereas the parent transforms need to be taken into account as well when constructing the viewMatrix.

1 Like

As for the quaternion problem - currently Camera’s viewMatrix is using rotation. You could try changing the viewMatrix to

  var viewMatrix: float4x4 {
    let translateMatrix = float4x4(translation: position)
    let rotateMatrix = float4x4(quaternion)
    let scaleMatrix = float4x4(scaling: scale)
    return (translateMatrix * scaleMatrix * rotateMatrix).inverse
  }

However, it sounds as if you might be wanting a full range of camera movement. There are other types of camera that you could look at - for example the ArcBall camera, which focuses on a target and rotates around that point

1 Like

Thank you very much for your response. The modification for the parent problem works very fine ! :smiley:
The other modification to use quaternions with camera works also very well ! :smiley:

I can now fly around the earth or the moon with the space ship without gimbal lock. Your explanations help me to understand the problems, I can now have good nights ! :sweat_smile:

Your book on metal is the first I read on metal. It is a very good book to learn 3D programming with metal !

I am going to look at others cameras as the ArcBall one. Thank you for your advice.

Thank you very much for your work !

2 Likes

I’m so glad! 3D programming is very addictive once you get the basics, and there’s an infinite amount to learn

2 Likes