0
votes

I have a SCNCylinder in a SCNView in SceneKit. My goal is to rotate the cylinder by 90° and 180° each on different axes of rotation. I am taking the input on which angle to rotate by using SwiftUI. I have made this playground that achieves the same. But I am having error as :“There was a problem encountered while running this playground. Please check your code for error.” Not sure why I get this error. I believe I have done all the things programmatically right.

Here is my code:

import SwiftUI
import SceneKit
import PlaygroundSupport

struct ContentView: View {
    @State var rotationAngle: Angle = .zero
    @State var rotationAngle2: Angle = .zero
    var body: some View {
        
        VStack{
            
            Text("180°").onTapGesture {
                self.rotationAngle = .degrees(180)
                self.rotationAngle2 = .degrees(0)
            }
            
            Divider()
            
            Text("90°").onTapGesture {
                self.rotationAngle = .degrees(0)
                self.rotationAngle2 = .degrees(90)
            }
            
            
            
            SceneKitView(radius: 0.02, height: 2, angle: $rotationAngle, angle2: $rotationAngle2)
                .position(x: 225.0, y: 175)
                .frame(width: 300, height: 300, alignment: .center)
            
        }
    }
}

struct SceneKitView: UIViewRepresentable {
    @Binding var angle: Angle
    @Binding var angle2 : Angle
    
    
    let cylindernode: SCNNode
    
    init(radius: CGFloat, height: CGFloat, angle: Binding<Angle>, angle2: Binding<Angle>) {
        
        let cylinder = SCNCylinder(radius: radius, height: height)
        cylinder.firstMaterial?.diffuse.contents = UIColor.green
        self.cylindernode = SCNNode(geometry: cylinder)
        self.cylindernode.position = SCNVector3(0, 0, 0)
        cylindernode.pivot = SCNMatrix4MakeTranslation(0, -1, 0)
        self._angle = angle
        self._angle2 = angle2
        
    }
    
    func makeUIView(context: UIViewRepresentableContext<SceneKitView>) -> SCNView {
        
        let sceneView = SCNView()
        sceneView.scene = SCNScene()
        sceneView.autoenablesDefaultLighting = true
        sceneView.allowsCameraControl = true
        sceneView.scene?.rootNode.addChildNode(cylindernode)
        return sceneView
    }
    
    func updateUIView(_ sceneView: SCNView, context: UIViewRepresentableContext<SceneKitView>) {
        
        let rotation = SCNAction.rotate(by: CGFloat(angle.radians), around: SCNVector3(1, 0, 0), duration: 3)
        
        let rotation2 = SCNAction.rotate(by: CGFloat(angle2.radians), around: SCNVector3(0, 0, 1), duration: 3)
        
        cylindernode.runAction(rotation)
        cylindernode.runAction(rotation2)
    }
}

PlaygroundPage.current.setLiveView(ContentView())


     
1

1 Answers

0
votes

testing the code after i added 4 lines (just import + setLiveView) there was no error when i started it.