properly rotate camera to see the side of the scene

130 Views Asked by At

I'm using SceneKit to use a 3d car to use in google map just like Uber. I managed to use it and rotate it but the camera is on top and I would like to rotate it once the location manager heading changes to see the edges of the car.

I tried changing the Euler angles but that didn't work well. I tried using the default camera but I didn't find how to place it on top or on left or on right ... I can only change it from the .scn file only but not in Code.

https://github.com/HilalAH/Uber3dModel

1

There are 1 best solutions below

4
On

Fyi - you have allowsCameraControl = true, probably don't want that.

This is pretty quick way to get you started.

Set a constraint on your camera so that it always looks at your car, then you can adjust the cameraEye using simple vectors. You can do some math on the cars heading and place the cameraEye wherever you want it in relation to the direction it's moving (back, side, etc.), but the car will always be the focal point.

Something like this code with cameraFocus being your car:

class Camera
{
    var cameraEye = SCNNode()
    var cameraFocus = SCNNode()

    init()
    {
            cameraEye.name = "Camera Eye"
            cameraFocus.name = "Camera Focus"

            cameraFocus.isHidden = true
            cameraFocus.position  =  SCNVector3(x: 0, y: 0, z: 0)

            cameraEye.camera = SCNCamera()
            cameraEye.constraints = []
            cameraEye.position = SCNVector3(x: 0, y: 0, z: 20)

            let vConstraint = SCNLookAtConstraint(target: cameraFocus)
            vConstraint.isGimbalLockEnabled = true
            cameraEye.constraints = [vConstraint]
        }
}

Make sure you've added your camera objects:

scene.rootNode.addChildNode(camera.cameraEye)
scene.rootNode.addChildNode(camera.cameraFocus)