简体   繁体   English

相机不在Scenekit中跟随飞机

[英]Camera is not following the airplane in Scenekit

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. 它在2点之间画一条线。 I have a cameraNode which is set to (0,200,200) initially. 我有一个cameraNode,最初设置为(0,200,200)。 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. 我尝试找到min ad max x,y和z并取平均值,但它不起作用。 If you see below gif its too zoomed and aircraft has moved out of the screen 如果你看到下面的gif,它太快了,飞机已经移出屏幕了

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!! 谢谢!!

The first behavior you describe would most easily be achieved by chaining a look-at constraint and a distance constraint, both targeting the aircraft. 您描述的第一个行为最容易通过链接观察约束和距离约束来实现,两者都是针对飞机的。

let lookAtConstraint = SCNLookAtConstraint(target: aircraft)

let distanceConstraint = SCNDistanceConstraint(target: aircraft)
distanceConstraint.minimumDistance = 10 // set to whatever minimum distance between the camera and aircraft you'd like
distanceConstraint.maximumDistance = 10 // set to whatever maximum distance between the camera and aircraft you'd like

camera.constraints = [lookAtConstraint, distanceConstraint]

For iOS 10 and earlier, you can implement a distance constraint using SCNTransformConstraint . 对于iOS 10及更早版本,您可以使用SCNTransformConstraint实现距离约束。 Here's a basic (though slightly ugly 😛) implementation that uses linear interpolation to update the node's position. 这是一个基本的(虽然有点丑😛)实现,它使用线性插值来更新节点的位置。

func normalize(_ value: Float, in range: ClosedRange<Float>) -> Float {
    return (value - range.lowerBound) / (range.upperBound - range.lowerBound)
}

func interpolate(from start: Float, to end: Float, alpha: Float) -> Float {
    return (1 - alpha) * start + alpha * end
}

let target = airplane

let minimumDistance: Float = 10
let maximumDistance: Float = 15

let distanceConstraint = SCNTransformConstraint(inWorldSpace: false) { (node, transform) -> SCNMatrix4 in
    let distance = abs(sqrt(pow(target.position.x - node.position.x, 2) + pow(target.position.y - node.position.y, 2) + pow(target.position.z - node.position.z, 2)))

    let normalizedDistance: Float

    switch distance {
    case ...minimumDistance:
        normalizedDistance = self.normalize(minimumDistance, in: 0 ... distance)
    case maximumDistance...:
        normalizedDistance = self.normalize(maximumDistance, in: 0 ... distance)
    default:
        return transform
    }

    node.position.x = self.interpolate(from: target.position.x, to: node.position.x, alpha: normalizedDistance)
    node.position.y = self.interpolate(from: target.position.y, to: node.position.y, alpha: normalizedDistance)
    node.position.z = self.interpolate(from: target.position.z, to: node.position.z, alpha: normalizedDistance)

    return transform
}

The second behavior could be implemented by determining the bounding box of your aircraft and all of its path segments in the camera's local coordinate space, then updating the camera's distance from the center of that bounding box to frame all of those nodes in the viewport. 第二种行为可以通过确定飞机的边界框及其在摄像机局部坐标空间中的所有路径段来实现,然后更新摄像机距该边界框中心的距离,以构建视口中的所有节点。 frameNodes(_:) , a convenience method that implements this functionality, was introduced in iOS 11 and is defined on SCNCameraController . frameNodes(_:)是一种实现此功能的便捷方法,在iOS 11中引入并在SCNCameraController上定义。 I'd recommend using it if possible, unless you want to dive into the trigonometry yourself. 我建议尽可能使用它,除非你想自己深入研究三角学。 You could use your scene view's default camera controller or create a temporary instance, whichever suits the needs of your app. 您可以使用场景视图的默认摄像头控制器或创建一个临时实例,以满足您的应用程序的需要。

You need to calculate the angle of the velocity so that the camera points in the direction of the moving SCNNode. 您需要计算速度的角度,以便摄像机指向移动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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM