How to rotate object in a scene with pan gesture - SceneKit

让人想犯罪 __ 提交于 2019-11-30 21:20:42

This solution works if your object original position isn't (0,0,0).

    if(gestureRecognize.state == UIGestureRecognizerState.Ended) {
       let currentPivot = geometryNode.pivot
       let currentPosition = geometryNode.position
       let changePivot = SCNMatrix4Invert(SCNMatrix4MakeRotation(geometryNode.rotation.w, geometryNode.rotation.x, geometryNode.rotation.y, geometryNode.rotation.z))

       geometryNode.pivot = SCNMatrix4Mult(changePivot, currentPivot)
       geometryNode.transform = SCNMatrix4Identity
       geometryNode.position = currentPosition
    }

But I don't understand why we assign the invert value of transform or rotation to changePivot. Is it correct, we are trying to rotate the pivot to our current rotation values and with setting the .transform property to the identity matrix it resets the node to the pivot value. We don't see any change, because the pivot already has the same values as our node right then. But why we use the invert values, normally you use this to rotate something back to its origin. Can somebody tell me, where i'm thinking wrong?

The problem is solved

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

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

    geometryNode.pivot = SCNMatrix4Mult(changePivot, currentPivot)

    geometryNode.transform = SCNMatrix4Identity
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!