0

I’m having trouble creating a physics body from a texture that matches the outline of the texture. I’m using SpriteKit’s SKPhysicsBody(texture:,size:) to create the physics body. I’ve found similar posts here and here that involve a large texture and a texture atlas. My app doesn’t use a large texture or an atlas.

Here’s code that illustrates the problem

class GameScene: SKScene {
    override func didMove(to view: SKView) {
        view.showsPhysics = true
        removeAllChildren()
        let rect1 = CGRect(origin: .zero, size: CGSize(width: 150, height: 100))
        if let oval = createOval(at: CGPoint(x: -100, y: 0), with: rect1) {
            addChild(oval)
        }
        
        let rect2 = CGRect(origin: .zero, size: CGSize(width: 151, height: 100))
        if let oval = createOval(at: CGPoint(x: 100, y: 0), with: rect2) {
            addChild(oval)
        }
    }
    
    func createOval(at point:CGPoint, with rect:CGRect) -> SKSpriteNode? {
        if let image = ovalImage(in: rect, color: .gray) {
            let texture = SKTexture(image: image)
            let sprite = SKSpriteNode(texture: texture)
            sprite.position = point
            sprite.physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
            sprite.physicsBody?.isDynamic = false
            return sprite
        }
        return nil
    }
    
    func ovalImage(in rect:CGRect, color:SKColor) -> UIImage? {
        let path = UIBezierPath(ovalIn: rect)
        
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
        defer {
            UIGraphicsEndImageContext()
        }
        guard let context = UIGraphicsGetCurrentContext() else {
            return nil
        }
        context.addPath(path.cgPath)
        color.setFill()
        context.drawPath(using: .fill)
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}

Here's a snapshot showing the issue. The physics body on the left does not match the texture but the body on the right does. The only difference is the one on the left used an image with a width that is even (150) and the one on the right used an image with a width that is odd (151).

enter image description here

This issue seems to affect only iOS/iPadOS versions 13.4 and 13.5. I’ve only been able to reproduce this bug on Xcode’s Simulator, since I don’t have a device running iOS 13.4 or 13.5. Can anyone confirm that this problem only affects the Simulator or does it affect devices running iOS 13.4 and 13.5?

Here's my history with iOS 13 and physics bodies from textures

13.0 - 13.1 broken (simulator + device)

13.2 fixed (sim + dev)

13.3 working (sim + dev)

13.4 broken (sim + ???)

13.5 broken (sim + ???)

13.5.1 working (dev)

I'm trying to fill in the ???s but lack a device running those iOS versions. If the answer to any of the ???s is that it's broken, I can't release my game in its current form.

I understand that creating a physics body from a texture from an atlas doesn't work and making a copy is a solution.

3
  • as reported in the comments in 1 of the questions you linked to..."Physics bodies from textures have been broken in various ways in iOS 13.*" I could probably point to a dozen posts on here in the past few months with all the same comment. IT IS BROKEN and unreliable in in iOS 13 Jul 18, 2020 at 15:11
  • @RonMyschuk I understand it's broken. I'm trying to understand under what circumstances. See my updated question.
    – Epsilon
    Jul 18, 2020 at 19:14
  • there isn't a question here. You are reporting your findings that this is broken, but this has already been reported many times. If you are looking for a alternate solution (which you haven't asked about) you could create the shapes via paths (I use a program called PaintCode) which outputs the points and beziers into swift code for you) and then create the physics body from path . Actually looking at your code you already have a a path for your oval just create the physics from it instead of the outputted texture Jul 20, 2020 at 15:40

1 Answer 1

0

SKPhysicsBody attempts to create a polygon body by looking for transparent pixels surrounding your shape. In this example the oval shape stretches to the edges of the image, so there are no transparent pixels surrounding it in all directions. I have seen this giving unexpected results in some versions. I recommend you try making your oval a little smaller, as to leave transparent pixels around it.

1
  • My game has dozens of images where the physics body doesn't match the texture. I always leave at least 1 transparent pixel surrounding the character when I create an image manually. This is not the issue.
    – Epsilon
    Jul 18, 2020 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.