Clamping camera around the background of a scene in SpriteKit
<p>so I have a base game setup that can be found at the bitbucket link below:</p>

<p><a href="https://bitbucket.org/2016globalgamejam/myinsanityv2" rel="noreferrer">Game link</a></p>

<p>I'm currently having a hard time understanding how to translate the camera node in relation to the scene's layout. </p>

<p>The goal is to have the camera follow the player up until hitting a corner boundary defined by the size of the scene. In this particular test scenario setup, the scene is 1000x1000 in size with a camera scale of 1. </p>

<p>The code below is used to modify the position of the camera when a new position is set to follow the character:</p>

<pre><code>    var cameraPosition: CGPoint {

        get {
            return CGPoint(x: camera!.position.x, y: camera!.position.y)
        }

        set {

            let cameraScale = CGFloat(1)
            let sceneScale = CGFloat(1)//CGFloat(1 - 0.44  + 0.05 /*possible rounding error adjustment*/)
//            let viewAspectRatio = CGRectGetWidth(view!.frame)/CGRectGetHeight(view!.frame)
            let newPositionValue = double2(x: Double(newValue.x * sceneScale), y: Double(newValue.y * sceneScale))

            let scaledSceneSize = CGSize(width: size.width * sceneScale , height: size.height * sceneScale)
////            scaledSceneSize.height = scaledSceneSize.height / viewAspectRatio

            let cameraSize = view!.bounds.size
            let scaledCameraSize = CGSize(width: cameraSize.width * cameraScale, height: cameraSize.height * cameraScale)


            let minX = 0//-scaledSceneSize.width * anchorPoint.x + scaledCameraSize.width / 2
            let minY = -219//-scaledSceneSize.height * anchorPoint.y + scaledCameraSize.height / 2

            let minValues = double2(x: Double(minX), y: Double(minY))

            let maxX = 0//(scaledSceneSize.width * anchorPoint.x - scaledCameraSize.width / 2) //size.width - cameraSize.width / 2
            let maxY = 219//(scaledSceneSize.height * anchorPoint.y - scaledCameraSize.height / 2) //- cameraSize.height / 2

            let maxValues = double2(x: Double(maxX), y: Double(maxY))

            let clampedPosition = clamp(newPositionValue, min: minValues, max: maxValues)


            camera!.position = CGPoint(x: (clampedPosition.x / Double(sceneScale)), y: (clampedPosition.y / Double(sceneScale)))
        }

    }
</code></pre>

<p>There are hardcore value that currently fit the required scene size, and I'm unsure how to get those results via the scale. The scale by default is:</p>

<pre><code>/* Set the scale mode to scale to fit the window */
            scene.scaleMode = .AspectFill
</code></pre>

<p>Without the knowledge of knowing there's a translation in scale, by default, I would expect the boundaries to be 
largestSceneDimensionXValue - cameraSize.width/2
largestSceneDimensionYValue - cameraSize.height/2</p>

<p>As a high level example. Would anyone be able to assist me in getting this translation?</p>

<p>Overall the scene should look like the following in all corners: 
<a href="https://i.stack.imgur.com/5Svzi.png" rel="noreferrer"><img src="https://i.stack.imgur.com/5Svzi.png" alt="goal"></a></p>

<p>VS having the black background overflow in the camera as such:</p>

<p><a href="https://i.stack.imgur.com/jaobN.png" rel="noreferrer"><img src="https://i.stack.imgur.com/jaobN.png" alt="anti goal"></a></p>