Search code examples
swiftaugmented-realityscenekitarkit

Rotating SCNTorus


I have a project which is showing planets using ARKit on iOS. I'm creating Saturn using SCNSphere and it worked. Now i'm trying to create a ring of it using SCNTorus but the result are showing up like this : Saturn Result

I need to rotate the ring 90 degrees so it would really look like Saturn in space. Is that possible to rotate this SCNTorus? Below is my Code

// Declaring Saturn Ring Using Torus
let saturnusRingTorus = SCNTorus(ringRadius: 0.1, pipeRadius: 0.2)
saturnusRingTorus.ringSegmentCount = 2

// Declaring Materials & Textures
let saturnusRingMaterial = SCNMaterial()
saturnusRingMaterial.diffuse.contents = UIImage(named: "textures.scnassets/saturnring.png")
saturnusRingTorus.materials = [saturnusRingMaterial]

// Show Ring Through Node
let nodeSaturnusRing = SCNNode()
nodeSaturnusRing.position = SCNVector3(x: -0.3, y: 0.1, z: -0.7)
nodeSaturnusRing.geometry = saturnusRingTorus
sceneSaturnus.scene.rootNode.addChildNode(nodeSaturnusRing)
sceneSaturnus.autoenablesDefaultLighting = true

Solution

  • If you need to rotate SCNTorus node around Z axis, counter-clockwise (CCW), use this code:

    Euler rotation (x, y, z)

    // eulerAngles's type is SCNVector3
    nodeSaturnusRing.eulerAngles.z = .pi / 2 
    

    Quaternion rotation (x, y, z, w)

    // x,y,z are vectors here, and w is a rotation angle
    nodeSaturnusRing.rotation = SCNVector4(0, 0, 1, Float.pi / 2)
    

    Take into consideration that rotation in ARKit, SceneKit and RealityKit is expressed in radians. When you want to rotate your model CW use negative values, and if you rotate your model CCW use positive values.

    If you want to know what is Quaternions vs Euler's angles, read this post.