ios – Swift modify node rotation based on globe coordinates


I have a 3d globe built with swift and SceneKit and to represent the users current location I use a torus (donut shaped object). The users location can be anywhere in the world and simply adding the node to the globe does not make it flush (aligned with globe). I have to modify the nodes eulerAngles in order to make it flat with the earth. My function doesn’t work to properly align the shape with the earth given any unique location. I want the function to position the torus flush based on any set of latitude and longitude.

    let dotNode = SCNNode(geometry: lowerCircle)
 
    dotNode.eulerAngles = getRotation(lat: viewModel.currentLocation?.lat ?? 0.0, long: viewModel.currentLocation?.long ?? 0.0)

    func getRotation(lat: Double, long: Double) -> SCNVector3 {
        let latRad = lat * .pi / 180.0
        let lonRad = long * .pi / 180.0

        let rotationX = Float(-latRad)
        let rotationY = Float(lonRad)
        let rotationZ = Float(lonRad)

        return SCNVector3Make(rotationX, rotationY, rotationZ)
        
        //example
        //slightly correct rotation for Saudi Arabia
        //SCNVector3Make(.pi / 3.75, 0, -.pi / 4)
    }

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img