4.1 C
Canberra
Monday, July 14, 2025

ios – Find out how to convert between rotation and euler angles in SceneKit


You should use the Spatial API.

import Spatial
import simd

func rotationToEuler(rotation: SCNVector4) -> SCNVector3 {
    SCNVector3(
        Rotation3D(angle: .radians(Double(rotation.w)), axis: .init(x: rotation.x, y: rotation.y, z: rotation.z))
            .eulerAngles(order: .xyz)
            .angles
    )
}

func eulerToRotation(eulerAngles: SCNVector3) -> SCNVector4 {
    let rotation = Rotation3D(eulerAngles: .init(angles: [eulerAngles.x, eulerAngles.y, eulerAngles.z], order: .xyz))
    return SCNVector4(
        simd_double4(rotation.axis.vector, rotation.angle.radians)
    )
}

Word that simd is not strictly vital right here. I simply used it in order that I haven’t got to transform between floats and doubles 4 instances in eulerToRotation.


For iOS 16:

func rotationToEuler(rotation: SCNVector4) -> SCNVector3 {
    SCNVector3(
        Rotation3D(angle: .init(radians: Double(rotation.w)), axis: .init(x: rotation.x, y: rotation.y, z: rotation.z))
            .eulerAngles(order: .pitchYawRoll)
            .angles
    )
}

func eulerToRotation(eulerAngles: SCNVector3) -> SCNVector4 {
    let rotation = Rotation3D(eulerAngles: .init(angles: [eulerAngles.x, eulerAngles.y, eulerAngles.z], order: .pitchYawRoll))
    return SCNVector4(
        simd_double4(rotation.axis.vector, rotation.angle.radians)
    )
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles