How to rotate to another face of 3d cube when swiped right

心不动则不痛 提交于 2019-12-06 10:23:54

You need to use a SCNAction.rotate getting the current w value of the node rotation and adding the amount of angle you want, using this 2 extensions for converting to Radians and from Radians, replace my self.shipNode by your cube node, and change the axis if you need to, taking in account that the first 0 in the SCNVector4Make sentence is X axis

This is an example

extension Int {
    var degreesToRadians: Double { return Double(self) * .pi / 180 }
    var radiansToDegrees: Double { return Double(self) * 180 / .pi }
}

extension FloatingPoint {
    var degreesToRadians: Self { return self * .pi / 180 }
    var radiansToDegrees: Self { return self * 180 / .pi }
}

@objc func handleTap(_ gestureRecognize: UIGestureRecognizer) {
    //here we rotate the ship node 30 grades in the Y axis each time
    self.shipNode?.runAction(SCNAction.rotate(toAxisAngle: SCNVector4Make(0, 1, 0, (self.shipNode?.rotation.w)! + Float(30.degreesToRadians)), duration: 1))
}

To rotate down you only have to change the axis of the rotation you can use this

- (void) handleTap:(UIGestureRecognizer*)gestureRecognize
{

     [self.ship runAction:[SCNAction rotateToAxisAngle:SCNVector4Make(1, 0, 0, [self getRadiansFromAngle:[self getAngleFromRadian:self.ship.rotation.w] + 30]) duration:1]];
}

UPDATED Using your code You need to use SCNAction.rotate(by: #Float, around: #SCNVector3, duration: #duration) method instead, below is the full code for your requeriments

@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {


        if let swipeGesture = gesture as? UISwipeGestureRecognizer {


            switch swipeGesture.direction {

                case UISwipeGestureRecognizerDirection.right:

                    print("Swiped Right")

                geometryNode.runAction(SCNAction.rotate(by: CGFloat(90.degreesToRadians), around: SCNVector3Make(0, 1, 0), duration: 0.2))


                case UISwipeGestureRecognizerDirection.left:

                    print("Swiped Left")
                    geometryNode.runAction(SCNAction.rotate(by: CGFloat(-90.degreesToRadians), around: SCNVector3Make(0, 1, 0), duration: 0.2))

                case UISwipeGestureRecognizerDirection.down:

                    print("Swiped Down")
                   geometryNode.runAction(SCNAction.rotate(by: CGFloat(90.degreesToRadians), around: SCNVector3Make(1, 0, 0), duration: 0.2))
                default:

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