场景中的相机没有跟随飞机运动。

5
我有一架正在飞行的飞机,我正在跟随它,并显示飞机飞行的路径。我使用圆柱体作为路径的线条。这有点像在两个点之间画一条直线。我有一个cameraNode,最初设置为(0,200,200)。在那个点上我可以看到飞机。但是当我开始飞行时,它就飞出了屏幕。我想要做两件事:
  1. 只跟随飞机(路径无关)。
  2. 显示整个路径和飞机。
我尝试找到最小和最大的x、y和z值,并取平均值,但没有成功。如果您看下面的gif,它太放大了,而且飞机已经飞出了屏幕。 这是我如何设置我的相机:
- (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.

这里是我尝试改变相机位置但未成功的代码:

以下是修改相机位置的代码:

- (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;

欢迎使用Swift或Objective-C编写代码。

谢谢!


如果我理解正确的话,您是在询问如何实现两种不同的相机行为?此外,您能否澄清一下“仅跟随飞机”是什么意思?相机是否应该保持与飞机的最小距离,还是只需更新以朝向其方向? - user3739999
@ErikFoss 是的,我需要两种行为:1. 跟随飞机(相机只会聚焦在飞机上而不是路径)。2. 在一个屏幕上展示整个路径和飞机。相机必须保持与飞机的一定距离。 - Dhara
2个回答

7
您可以通过链接一个看向约束和一个距离约束来实现您所描述的第一种行为,两个约束都针对飞机。
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]

对于iOS 10及更早版本,您可以使用SCNTransformConstraint实现距离约束。以下是一种基本的(虽然略显丑陋)实现,使用线性插值来更新节点的位置。

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
}

第二种行为可以通过在相机的本地坐标空间中确定飞机和其所有路径段的边界框,然后更新相机距离该边界框中心的距离以在视口中框定所有这些节点来实现。在iOS 11中引入了SCNCameraController上定义的frameNodes(_:)方法,它是实现此功能的便捷方法。如果可能的话,我建议使用它,除非您想自己深入研究三角学。您可以使用场景视图的默认相机控制器或创建临时实例,以满足应用程序的需要。

边界框意味着找到最大和最小的x、y、z,并找到平均值?另外,您能否给出一个使用frameNodes的示例代码。 - Dhara
嗯,我不知道您是如何存储要框架的节点的,因此我无法为您编写该部分。您需要做的第一件事是将它们放入一个数组中。然后检索或创建一个相机控制器;如果您没有使用场景视图的默认相机控制器,只需创建一个新实例(let cameraController = SCNCameraController())并分配当前视角(cameraController.pointOfView = sceneView.pointOfView)。然后调用 frameNodes(_:),传递您的数组(cameraController.frameNodes(nodes))。这应该就是您需要做的全部! - user3739999
“Bounding box”指的是包围所有节点的矩形体积。该边界框的中心点是移动到达或远离以达到定位节点所需的目标距离的点。 - user3739999
所有这些方法都在iOS 11 beta中。它们不适用于Xcode 8。 - Dhara
抱歉,我不应该假设你正在使用测试版。使用SCNTransformConstraint可以很容易地实现距离约束,并且SCNLookAtConstraint在iOS 8及更高版本中可用。如果你愿意,我可以编辑我的帖子并提供这些示例。 - user3739999
如果您能编辑答案,那将非常棒。 - Dhara

0
你需要计算速度的角度,以便相机指向移动的SCNNode方向。
这段代码将指引你朝着正确的方向前进。
    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

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接