猿问

旋转SCNCamera节点,查看假想球体周围的对象

旋转SCNCamera节点,查看假想球体周围的对象

我在位置(30,30,30)有一个SCNCamera,在位于(0,0,0)位置的对象上有一个SCNLookAtConstraint。我正在尝试使用A UIPanGestureRecognizer让相机围绕虚拟球体上的物体旋转,同时保持相机和物体之间的半径。我假设我应该使用四元数预测,但我在这方面的数学知识很糟糕。我知道的变量是x&y平移+我试图保留的半径。我在Swift中编写了这个项目,但Objective-C中的答案也同样被接受(希望使用标准的Cocoa Touch Framework)。


哪里:


private var cubeView : SCNView!;

private var cubeScene : SCNScene!;

private var cameraNode : SCNNode!;

这是我设置场景的代码:


// setup the SCNView

cubeView = SCNView(frame: CGRectMake(0, 0, self.width(), 175));

cubeView.autoenablesDefaultLighting = YES;

self.addSubview(cubeView);


// setup the scene

cubeScene = SCNScene();

cubeView.scene = cubeScene;


// setup the camera

let camera = SCNCamera();

camera.usesOrthographicProjection = YES;

camera.orthographicScale = 9;

camera.zNear = 0;

camera.zFar = 100;


cameraNode = SCNNode();

cameraNode.camera = camera;

cameraNode.position = SCNVector3Make(30, 30, 30)  

cubeScene.rootNode.addChildNode(cameraNode)


// setup a target object

let box = SCNBox(width: 10, height: 10, length: 10, chamferRadius: 0);

let boxNode = SCNNode(geometry: box)

cubeScene.rootNode.addChildNode(boxNode)


// put a constraint on the camera

let targetNode = SCNLookAtConstraint(target: boxNode);

targetNode.gimbalLockEnabled = YES;

cameraNode.constraints = [targetNode];


// add a gesture recogniser

let gesture = UIPanGestureRecognizer(target: self, action: "panDetected:");

cubeView.addGestureRecognizer(gesture);

以下是手势识别器处理的代码:


private var position: CGPoint!;


internal func panDetected(gesture:UIPanGestureRecognizer) {


    switch(gesture.state) {

    case UIGestureRecognizerState.Began:

        position = CGPointZero;

    case UIGestureRecognizerState.Changed:

        let aPosition = gesture.translationInView(cubeView);

        let delta = CGPointMake(aPosition.x-position.x, aPosition.y-position.y);


        // ??? no idea...


        position = aPosition;

    default:

        break

    }

}

谢谢!


胡说叔叔
浏览 955回答 3
3回答

忽然笑

如果您想使用手势识别器实现rickster的答案,则必须保存状态信息,因为您只会获得相对于手势开头的翻译。我在班上加了两个varsvar lastWidthRatio: Float = 0var lastHeightRatio: Float = 0并实现了他的旋转代码如下:func handlePanGesture(sender: UIPanGestureRecognizer) {     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 % 1         lastHeightRatio = heightRatio % 1     }}
随时随地看视频慕课网APP
我要回答