How to move a rotated SCNNode in SceneKit?

前端 未结 2 1981
野性不改
野性不改 2021-01-12 14:32

The image below shows a rotated box that should be moved horizontally on the X and Z axes. Y should stay unaffected to simplify the scenario. The box could also be the SCNNo

2条回答
  •  礼貌的吻别
    2021-01-12 14:38

    Building on @Sulevus's correct answer, here's an extension to SCNNode that simplifies things by using the convertVector rather than the convertPosition transformation, in Swift.

    I've done it as a var returning a unit vector, and supplied an SCNVector3 overload of multiply so you can say things like

    let action = SCNAction.move(by: 2 * cameraNode.leftUnitVectorInParent, duration: 1)
    
    
    public extension SCNNode {
        var leftUnitVectorInParent: SCNVector3 {
            let vectorInSelf = SCNVector3(x: 1, y: 0, z: 0)
            guard let parent = self.parent else { return vectorInSelf }
            // Convert to parent's coord system
            return parent.convertVector(vectorInSelf, from: self)
        }
        var forwardUnitVectorInParent: SCNVector3 {
            let vectorInSelf = SCNVector3(x: 0, y: 0, z: 1)
            guard let parent = self.parent else { return vectorInSelf }
            // Convert to parent's coord system
            return parent.convertVector(vectorInSelf, from: self)
        }
    
        func *(lhs: SCNVector3, rhs: CGFloat) -> SCNVector3 {
            return SCNVector3(x: lhs.x * rhs, y: lhs.y * rhs, z: lhs.z * rhs)
        }
        func *(lhs: CGFloat, rhs: SCNVector3) -> SCNVector3 {
            return SCNVector3(x: lhs * rhs.x, y: lhs * rhs.y, z: lhs * rhs.z)
        }
    }
    

提交回复
热议问题