Camera is not following the airplane in Scenekit

Viewed 1594

I have a flying aircraft which I am following and I am also showing the path the aircraft has followed. I am drawing cylinders as a line for drawing the path. Its kind of drawing a line between 2 points. I have a cameraNode which is set to (0,200,200) initially. At that point I can see the aircraft. But when I start my flight. It goes out of the screen. I want 2 things :

  1. Follow just the aircraft (Path won't matter).
  2. Show whole path and also the aircraft.

I tried finding the min ad max x,y and z and taking average but it din't work. If you see below gif its too zoomed and aircraft has moved out of the screen

Here is how I set my camera:

- (void)setUpCamera {
SCNScene *workingScene = [self getWorkingScene];
_cameraNode = [[SCNNode alloc] init];
_cameraNode.camera = [SCNCamera camera];
_cameraNode.camera.zFar = 500;
_cameraNode.position = SCNVector3Make(0, 60, 50);
[workingScene.rootNode addChildNode:_cameraNode];

SCNNode *frontCameraNode = [SCNNode node];
frontCameraNode.position = SCNVector3Make(0, 100, 50);
frontCameraNode.camera = [SCNCamera camera];
frontCameraNode.camera.xFov = 75;
frontCameraNode.camera.zFar = 500;
[_assetActivity addChildNode:frontCameraNode]; //_assetActivity is the aircraft node.

}

Here is how I am changing camera position which is not working:

- (void)showRealTimeFlightPath {


DAL3DPoint *point = [self.aircraftLocation convertCooridnateTo3DPoint];
DAL3DPoint *previousPoint = [self.previousAircraftLocation convertCooridnateTo3DPoint];
self.minCoordinate = [self.minCoordinate findMinPoint:self.minCoordinate currentPoint:point];
self.maxCoordinate = [self.minCoordinate findMaxPoint:self.maxCoordinate currentPoint:point];
DAL3DPoint *averagePoint = [[DAL3DPoint alloc] init];
averagePoint = [averagePoint averageBetweenCoordiantes:self.minCoordinate maxPoint:self.maxCoordinate];

SCNVector3 positions[] = {
    SCNVector3Make(point.x,point.y,point.z) ,
    SCNVector3Make(previousPoint.x,previousPoint.y,previousPoint.z)
};
SCNScene *workingScene = [self getWorkingScene];
DALLineNode *lineNodeA = [[DALLineNode alloc] init];
[lineNodeA init:workingScene.rootNode v1:positions[0] v2:positions[1] radius:0.1 radSegementCount:6 lineColor:[UIColor greenColor]] ;
[workingScene.rootNode addChildNode:lineNodeA];
self.previousAircraftLocation = [self.aircraftLocation mutableCopy];
self.cameraNode.position = SCNVector3Make(averagePoint.x, averagePoint.y, z);
self.pointOfView = self.cameraNode;

}

Code in swift or objective c are welcomed.

Thanks!!

2 Answers

You need to calculate the angle of the velocity so that the camera points in the direction of the moving SCNNode.

This code will point you in the right direction.

    func renderer(_ aRenderer: SCNSceneRenderer, didSimulatePhysicsAtTime time: TimeInterval) {

        // get velocity angle using velocity of vehicle

        var degrees = convertVectorToAngle(vector: vehicle.chassisBody.velocity)

        // get rotation of current camera on X and Z axis

        let eX = cameraNode.eulerAngles.x
        let eZ = cameraNode.eulerAngles.z

        // offset rotation on y axis by 90 degrees

        // this needs work, buggy

        let ninety = deg2rad(90)

        // default camera Y Euler angle facing north at 0 degrees

        var eY : Float = 0.0

        if degrees != 0 {

            eY = Float(-degrees) - Float(ninety)

        }

        // rotate camera direction using cameraNode.eulerAngles and direction of velocity as eY

        cameraNode.eulerAngles = SCNVector3Make(eX, eY, eZ)

        // put camera 25 points behind vehicle facing direction of velocity

        let dir = calculateCameraDirection(cameraNode: vehicleNode)
        let pos = pointInFrontOfPoint(point: vehicleNode.position, direction:dir, distance: 25)

        // camera follows driver view from 25 points behind, and 10 points above vehicle

        cameraNode.position = SCNVector3Make(pos.x, vehicleNode.position.y + 10, pos.z)

}

func convertVectorToAngle(vector: SCNVector3) -> CGFloat {

    let degrees = atan2(vector.z, vector.x)

    return CGFloat(degrees)

}


func pointInFrontOfPoint(point: SCNVector3, direction: SCNVector3, distance: Float) -> SCNVector3 {

    var x = Float()
    var y = Float()
    var z = Float()

    x = point.x + distance * direction.x
    y = point.y + distance * direction.y
    z = point.z + distance * direction.z

    let result = SCNVector3Make(x, y, z)
    return result

}

func calculateCameraDirection(cameraNode: SCNNode) -> SCNVector3 {

            let x = -cameraNode.rotation.x
            let y = -cameraNode.rotation.y
            let z = -cameraNode.rotation.z
            let w = cameraNode.rotation.w
            let cameraRotationMatrix = GLKMatrix3Make(cos(w) + pow(x, 2) * (1 - cos(w)),
                                                      x * y * (1 - cos(w)) - z * sin(w),
                                                      x * z * (1 - cos(w)) + y*sin(w),

                                                      y*x*(1-cos(w)) + z*sin(w),
                                                      cos(w) + pow(y, 2) * (1 - cos(w)),
                                                      y*z*(1-cos(w)) - x*sin(w),

                                                      z*x*(1 - cos(w)) - y*sin(w),
                                                      z*y*(1 - cos(w)) + x*sin(w),
                                                      cos(w) + pow(z, 2) * ( 1 - cos(w)))

            let cameraDirection = GLKMatrix3MultiplyVector3(cameraRotationMatrix, GLKVector3Make(0.0, 0.0, -1.0))
            return SCNVector3FromGLKVector3(cameraDirection)
}

func deg2rad(_ number: Double) -> Double {

        return number * .pi / 180

}
Related