SceneKit - 向通过平移手势旋转的球体添加漂移

SceneKit - Adding drift to a sphere rotated via a pan gesture

我正在使用 SceneKit 并允许用户使用平移手势旋转他们在球体内看到的内容。这工作正常 - 除了,我想让球体在平移手势的方向上保持轻微旋转,以感觉更有反应。

这是我的场景设置:

    // Create scene
    let scene = SCNScene()

    sceneView.scene = scene

    let panRecognizer = UIPanGestureRecognizer(target: self,
                                               action: #selector(ViewController.handlePanGesture(_:)))

    sceneView.addGestureRecognizer(panRecognizer)

    //Create sphere
    let sphere = SCNSphere(radius: 50.0)

    // sphere setup

    sphereNode = SCNNode(geometry: sphere)

    sphereNode!.position = SCNVector3Make(0,0,0)
    scene.rootNode.addChildNode(sphereNode!)

    // Create camera
    let camera = SCNCamera()

    // camera setup

    cameraNode = SCNNode()

    cameraNode!.camera = camera
    cameraNode!.position = SCNVector3Make(0, 0, 0)

    cameraOrbit = SCNNode()

    cameraOrbit!.addChildNode(cameraNode!)
    scene.rootNode.addChildNode(cameraOrbit!)

    let lookAtConstraint = SCNLookAtConstraint(target: sphereNode!)
    lookAtConstraint.gimbalLockEnabled = true
    cameraNode!.constraints = [lookAtConstraint]

    sceneView.pointOfView = cameraNode

这是当前处理平移手势的方式(由 提供):

    let translation = sender.translationInView(sender.view!)
    let widthRatio = Float(translation.x) / Float(sender.view!.frame.size.width) + lastWidthRatio
    let heightRatio = Float(translation.y) / Float(sender.view!.frame.size.height) + lastHeightRatio

    self.cameraOrbit?.eulerAngles.y = Float(-2 * M_PI) * widthRatio
    self.cameraOrbit?.eulerAngles.x = Float(-M_PI) * heightRatio

    if (sender.state == .Ended) {
        lastWidthRatio = widthRatio
        lastHeightRatio = heightRatio
    }

我不确定从哪里开始添加轻微的持续旋转运动。也许添加 physicsBody 并施加力?也许动画变化 eulerAngles?

我之前处理此类事情的方式是将手势处理程序拆分为手势状态开始、更改和结束的部分。如果旋转对您有效,您需要做的就是将代码添加到 if (sender.state == .Ended) 语句的底部。

lastHeightRatio = heightRatio 之后,您可以使用 sender.velocityInView(sender.view!) 在视图中找到平移速度,然后像以前一样找到水平分量,然后添加一个 SCNAction 来旋转节点EaseOut 计时模式。

您可能需要一个乘数来将视图中的速度(以点为单位)转换为您希望旋转的角度(以弧度为单位)。相对较小的 10 点速度将导致非常快的旋转。

此外,如果您希望触摸停止剩余旋转,则需要在手势再次开始时从节点中删除所有操作。

一些示例代码是:

if (sender.state == .Ended) {
  lastWidthRatio = widthRatio
  lastHeightRatio = heightRatio

  // Find velocity
  let velocity = sender.velocityInView(sender.view!)

  // Create Action for whichever axis is the correct one to rotate about.
  // The 0.00001 is just a factor to provide the rotation speed to pan
  // speed ratio you'd like. Play with this number and the duration to get the result you want
  let rotate = SCNAction.rotateByX(velocity.x * 0.00001, y: 0, z: 0, duration: 2.0)

  // Run the action on the camera orbit node (if I understand your setup correctly)
  cameraOrbit.runAction(rotate)
}

这应该足以让您入门。