Tim Kolar
Tim Kolar

Reputation: 47

reproducible results for SKPhysicsBody collisions?

I'm using Swift 5 with the iPhone 12 simulator. When I create a number of simple shapes and let gravity pull them down the result differs from run to run.

My eventual goal is to create a two player game where I can rely on the physics simulation to have the exact same results on two different devices. I was hoping I could find a way to seed the RNG somewhere, or at least get deterministic results.

Any pointers would be gratefully received.

Snippets of the test code:

(in SKScene)

        // Setup the boundaries -- whole screen for now
        let boundary = SKNode()
        boundary.position = .zero
        boundary.physicsBody = SKPhysicsBody(edgeLoopFrom: CGRect(origin: .zero, size: self.size))
        addChild(boundary)
        
        for i in 1...20 {
            let groundTile = GroundTileClass(diameter: 2, location: CGPoint(x: self.frame.midX, y: Double(i)*20.0))
            self.addChild(groundTile)
        }
        let biggerTile = GroundTileClass(diameter: 20, location: CGPoint(x: 200, y: 1000))
        self.addChild(biggerTile)
        let biggerTile2 = GroundTileClass(diameter: 20, location: CGPoint(x: 400, y: 1000))
        self.addChild(biggerTile2)

(GroundTileClass)

class GroundTileClass : SKShapeNode {
    init(diameter : UInt32, location : CGPoint) {
        
        super.init()
        
        // Create a hexagon ground tile
    
        self.path = createHexagonPath(diameter: diameter, location: location)
        self.strokeColor = SKColor.green
        self.fillColor = SKColor.systemPink
        self.physicsBody = SKPhysicsBody(polygonFrom: self.path!)
        self.physicsBody?.affectedByGravity = true
        self.physicsBody?.categoryBitMask = ObjectCategoryEnum.groundCategory.rawValue
        self.physicsBody?.collisionBitMask = ObjectCategoryEnum.groundCategory.rawValue
    }
} 

Sample 1

Sample 2

Upvotes: 0

Views: 41

Answers (0)

Related Questions