Search code examples
swiftipadsprite-kitskphysicsbodyskphysicsworld

Setup Screen Boundary on iPad Pro 12.9 with SKPhysics


I'm trying to create a boundary of physics for the iPad Pro 12.9

This is how I'm doing it:

override func didMove(to view: SKView) {

        physicsWorld.contactDelegate = self

        let sceneBody = SKPhysicsBody(edgeLoopFrom: self.frame)
        sceneBody.friction = 0
        self.physicsBody = sceneBody

....
}

But the Y is way off in Landscape (much lower and higher than the actual screen), and a little ways off in Portrait. But the X is right in both.

I don't know what I'm doing wrong.

Update

I've added a print to the above, and its showing the maxX and maxY of self.frame to be 375 and 667 respectively. In landscape mode. Neither of those numbers are what they should be, as far as I can tell, yet the X value works correctly whilst Y is way off the top and bottom of the screen.

This iPad model's screen resolution is 2732x2048 (half that in points) so I don't see a correlation between these numbers and the reported frame size.


Solution

  • This has something to do with the way you're scaling the scene. When presenting a scene, you may be setting the scaleMode property of the scene, which is of type SKSceneScaleMode. There are four different modes:

    • fill: Each axis is scaled independently in order to fit the whole screen
    • aspectFill: The scene is scaled to fill the screen, but keeping the aspect ratio fixed. This is the one your scene is probably set to.
    • aspectFit: The scene is scaled to fit inside the screen, but keeps the aspect ratio. If the scene has a different aspect ratio from the device screen, there will be letter boxing.
    • resizeFill: The scene is resized to fit the view.