ios – Scenekit Pan 2D翻译到正交3D仅水平

前端之家收集整理的这篇文章主要介绍了ios – Scenekit Pan 2D翻译到正交3D仅水平前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在3D编程方面遇到了更多的数学问题,我希望你能帮助我!

我正在尝试使用具有等角度角度的Scenekit创建3D游戏.

这段代码创建了我的正交相机:

var cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.name = "Camera"
cameraNode.position = SCNVector3Make(-5.0,-5.0,10.0)
cameraNode.eulerAngles = SCNVector3Make(PI / 3.0,0.0,-PI / 4.0)
cameraNode.camera?.usesOrthographicProjection = true
cameraNode.camera?.orthographicScale = 7.0
scene.rootNode.addChildNode(cameraNode)

现在我想使用平移手势移动相机,产生滚动感.为了实现这一点,相机不应垂直移动,只能水平移动.移动时,屏幕上的触摸位置和3D世界中未投影的位置应保持不变.

我考虑过计算2D平移到3D差异并忽略垂直分量.这段代码实际上可以工作,几乎可以产生所需的结果,但速度不正确.如果我平移,相机似乎加速并且没有正确反应:

var prevIoUsTranslation = CGPointMake(0.0,0.0)

func pan(gesture: UIPanGestureRecognizer)
{
    let view = self.view as SCNView
    let translation = gesture.translationInView(view)
    let location = gesture.locationInView(view)

    let diffTrans = translation - prevIoUsTranslation
    prevIoUsTranslation = translation

    let cameraNode = scene.rootNode.childNodeWithName("Camera",recursively: false)

    let worldPointTrans = view.unprojectPoint(SCNVector3Make(-Float(diffTrans.x),-Float(diffTrans.y),0.0))
    let worldPoint0 = view.unprojectPoint(SCNVector3Make(0.0,0.0))

    var diff = worldPointTrans - worldPoint0
    diff.x = diff.x / Float(cameraNode!.camera!.orthographicScale)
    diff.y = diff.y / Float(cameraNode!.camera!.orthographicScale)
    diff.z = 0
    cameraNode?.position += diff
}

有人知道一种复杂的方法来计算屏幕平移到水平3D平移,忽略垂直轴吗?

先感谢您 :)

编辑:
平底锅现在用于水平翻译.但不是垂直的,因为我将z轴上的差异设置为零.

解决方法

我找到了自己的解决方案.

我正在计算手势开始位置(P1-P2)的光线和翻译位置(Q1-Q2)的光线.现在我有两条光线,让两条光线都与XY平面相交,以接收点P0和Q0

P0和Q0的差异是未投影的翻译.

这种技术也适用于非正交相机,但我还没有测试过.

在我看来它是有效的,但如果有人能在数学上证实这个假设,我会很高兴看到:)

这是代码

var prevIoUsLocation = SCNVector3(x: 0,y: 0,z: 0)

func pan(gesture: UIPanGestureRecognizer)
{
    let view = self.view as SCNView
    let translation = gesture.translationInView(view)

    let location = gesture.locationInView(view)
    let secLocation = location + translation

    let P1 = view.unprojectPoint(SCNVector3(x: Float(location.x),y: Float(location.y),z: 0.0))
    let P2 = view.unprojectPoint(SCNVector3(x: Float(location.x),z: 1.0))

    let Q1 = view.unprojectPoint(SCNVector3(x: Float(secLocation.x),y: Float(secLocation.y),z: 0.0))
    let Q2 = view.unprojectPoint(SCNVector3(x: Float(secLocation.x),z: 1.0))

    let t1 = -P1.z / (P2.z - P1.z)
    let t2 = -Q1.z / (Q2.z - Q1.z)

    let x1 = P1.x + t1 * (P2.x - P1.x)
    let y1 = P1.y + t1 * (P2.y - P1.y)

    let P0 = SCNVector3Make(x1,y1,0)

    let x2 = Q1.x + t1 * (Q2.x - Q1.x)
    let y2 = Q1.y + t1 * (Q2.y - Q1.y)

    let Q0 = SCNVector3Make(x2,y2,0)

    var diffR = Q0 - P0
    diffR *= -1

    let cameraNode = view.scene!.rootNode.childNodeWithName("Camera",recursively: false)

    switch gesture.state {
    case .Began:
        prevIoUsLocation = cameraNode!.position
        break;
    case .Changed:
        cameraNode?.position = prevIoUsLocation + diffR
        break;
    default:
        break;
    }
}
原文链接:https://www.f2er.com/iOS/334739.html

猜你在找的iOS相关文章