I have a SpriteKit scene where I have a background (SKSpriteNode) whose size is 2x the size of the scene. I added a camera that moves when the user clicks his fingers and holds his fingers. I have a pinch to zoom and pan, however I need a camera so that it does not move past the edges of the node background.
I tried using the physical body and the contour of the ribs, but this did not work (unless I configured it incorrectly). Here is the code I have and some images to help convey the message. Should I move the background node instead of the camera?
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
self.physicsBody!.categoryBitMask = SCENE_EDGE_CAT
self.physicsWorld.contactDelegate = self
mCamera = self.childNode(withName: "camera") as! SKCameraNode
mCamera.physicsBody = SKPhysicsBody(edgeLoopFrom: mCamera.frame)
mCamera.physicsBody?.collisionBitMask = self.SCENE_EDGE_CAT
mCamera.physicsBody!.contactTestBitMask = mCamera.physicsBody!.collisionBitMask
func panForTranslation(_ translation: CGPoint) {
let position = mCamera.position
let aNewPosition = CGPoint(x: position.x - translation.x, y: position.y - translation.y)
mCamera?.position = aNewPosition
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let positionInScene = touch?.location(in:self)
let previousPosition = touch?.previousLocation(in:self)
let translation = CGPoint(x: (positionInScene?.x)! - (previousPosition?.x)!, y: (positionInScene?.y)! - (previousPosition?.y)!)
panForTranslation(translation)
print(mCamera.position)
}
Scene (gray - background image, red - camera)

Fits this ...

But I need him to stop at the edge ...
