如何使用平移手势旋转场景中的对象 - SceneKit

时间:2016-02-04 07:25:25

标签: swift rotation scenekit

我想用Scene套件创建一个应用来解决Rubix Cube。我自己的多维数据集的dae文件。 这是我在viewDidLoad中的设置代码

    let myscene = SCNScene(named: "Rubik1.scnassets/Rubiks_Cube.dae")
    scene.rootNode.addChildNode((myscene?.rootNode)!)

    // retrieve the SCNView
    let scnView = self.view as! SCNView

    // set the scene to the view
    scnView.scene = scene

    geometryNode = (scnView.scene?.rootNode.childNodeWithName("Cube",recursively: true))!

    let panRecognizer = UIPanGestureRecognizer(target: self, action: "panGesture:")
    scnView.addGestureRecognizer(panRecognizer)

识别平移手势以旋转立方体

func panGesture(gestureRecognize: UIPanGestureRecognizer){

    let translation = gestureRecognize.translationInView(gestureRecognize.view!)

    let x = Float(translation.x)
    let y = Float(-translation.y)

    let anglePan = sqrt(pow(x,2)+pow(y,2))*(Float)(M_PI)/180.0

    var rotationVector = SCNVector4()
    rotationVector.x = -y
    rotationVector.y = x
    rotationVector.z = 0
    rotationVector.w = anglePan

    geometryNode.rotation = rotationVector

    //geometryNode.transform = SCNMatrix4MakeRotation(anglePan, -y, x, 0)

    if(gestureRecognize.state == UIGestureRecognizerState.Ended) {
        //
    }
}

上面的代码并没有保留以前的平移手势。我如何使用" rotationvector"或

SCNMatrix4MakeRotation(anglePan, -y, x, 0)

旋转立方体

2 个答案:

答案 0 :(得分:7)

如果您的对象原始位置不是(0,0,0),则此解决方案有效。

SELECT *, SUBSTRING(POS_TITLE,0,CHARINDEX(',',POS_TITLE,0)) as POS1_Title, SUBSTRING(POS_TITLE,CHARINDEX(',',POS)+1,LEN(POS_TITLE)) as POS2_Title
FROM
(
SELECT 
    P.ID
    , N.FIRST
    , N.MIDDLE
    , N.LAST
    , A.AD1
    , A.AD2
    , A.AD3
    , A.CITY
    , A.STATE
    , A.ZIP
    , A.COUNTRY
    , PH.NUMBER
    , PH.TYPE
    , E.TYPE
    , E.ADDRESS
    ,STUFF(
    (SELECT ','+title FROM POSITIONS as P WHERE P.PositionId = N.PositionId FOR XML PATH('')),1,1,'') as POS_TITLE
FROM
    PERSON AS P
    INNER JOIN NAME AS N ON P.ID=N.ID,
    INNER JOIN ADDRESS AS A ON P.ID=A.ID,
    INNER JOIN PHONE AS PH ON P.ID=PH.ID,
    INNER JOIN EMAIL AS E ON P.ID=E.ID
    INNER JOIN POSITIONS AS N ON N.ID = A.ID
GROUP BY P.ID
    , N.FIRST
    , N.MIDDLE
    , N.LAST
    , A.AD1
    , A.AD2
    , A.AD3
    , A.CITY
    , A.STATE
    , A.ZIP
    , A.COUNTRY
    , PH.NUMBER
    , PH.TYPE
    , E.TYPE
    , E.ADDRESS
) as A

但我不明白为什么我们将变换或旋转的反转值赋给changePivot。它是否正确,我们正在尝试将枢轴旋转到我们当前的旋转值,并将.transform属性设置为单位矩阵,它将节点重置为枢轴值。我们没有看到任何变化,因为枢轴已经具有与我们的节点相同的值。 但是为什么我们使用反转值,通常你用它来将某些东西旋转回原点。有人可以告诉我,我在哪里想错了吗?

答案 1 :(得分:6)

问题解决了

if(gestureRecognize.state == UIGestureRecognizerState.Ended) {

    let currentPivot = geometryNode.pivot
    let changePivot = SCNMatrix4Invert( geometryNode.transform)

    geometryNode.pivot = SCNMatrix4Mult(changePivot, currentPivot)

    geometryNode.transform = SCNMatrix4Identity
}