iOS Development

ios – Swift (SCNCamera) lock Globe rotation round axis

I see that your globe is centered in a SCNScene, the place the digital camera is positioned to have a look at the globe. And you’ve got constraints that aren’t functioning as anticipated.

SCNLookAtConstraint is designed to make a node orient itself in direction of one other node. Whereas it supplies a function like isGimbalLockEnabled to forestall gimbal lock, it doesn’t provide direct management over proscribing rotation angles on particular axes
It’s extra suited to eventualities the place you need the digital camera to all the time face a goal, fairly than controlling its tilt or horizontal rotation limits.

As a substitute of utilizing SCNLookAtConstraint, attempt to use SCNTransformConstraint to regulate the digital camera’s orientation. Outline a variety for allowed rotation across the y-axis (horizontal rotation).

SCNTransformConstraint lets you outline customized constraints on the node’s transformation, together with its orientation. That lets you exactly management the rotation round particular axes, resembling limiting rotation to a sure vary across the x-axis (tilt) whereas permitting free rotation across the y-axis (horizontal rotation). And you may apply customized logic to find out the allowed rotation.

In your case, restrict the lean of the globe to +/- 30 levels across the x-axis.

non-public func setupCamera() {
    self.cameraNode = SCNNode()
    cameraNode.digital camera = SCNCamera()
    cameraNode.place = SCNVector3(x: 0, y: 0, z: 5)

    let constraint = SCNTransformConstraint.orientationConstraint(inWorldSpace: true) { (_, orientation) -> SCNQuaternion in
        let clampedX = max(min(orientation.x, 0.261799), -0.261799) // Clamping to +/- 30 levels in radians
        return SCNQuaternion(x: clampedX, y: orientation.y, z: 0, w: orientation.w)
    }
    
    cameraNode.constraints = [constraint]
    sceneView.scene?.rootNode.addChildNode(cameraNode)
}

The max(min()) perform clamps the rotation to +/- 30 levels (transformed to radians). Rotation across the y-axis (horizontal rotation) stays unrestricted.

Credit: www.ismmailgsm.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button