为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

胡说叔叔 2019-08-26 19:13:06
旋转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 SCNViewcubeView = SCNView(frame: CGRectMake(0, 0, self.width(), 175));cubeView.autoenablesDefaultLighting = YES;self.addSubview(cubeView);// setup the scenecubeScene = SCNScene();cubeView.scene = cubeScene;// setup the cameralet 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 objectlet box = SCNBox(width: 10, height: 10, length: 10, chamferRadius: 0);let boxNode = SCNNode(geometry: box)cubeScene.rootNode.addChildNode(boxNode)// put a constraint on the cameralet targetNode = SCNLookAtConstraint(target: boxNode);targetNode.gimbalLockEnabled = YES;cameraNode.constraints = [targetNode];// add a gesture recogniserlet 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    }}谢谢!
查看完整描述

3 回答

?
忽然笑

TA贡献1806条经验 获得超5个赞

如果您想使用手势识别器实现rickster的答案,则必须保存状态信息,因为您只会获得相对于手势开头的翻译。我在班上加了两个vars

var 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
    }}


查看完整回答
反对 回复 2019-08-26
  • 3 回答
  • 0 关注
  • 954 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信