0
votes

I have a simple SpriteKit game that uses physics. Written in Swift, and works great in iOS8 Simulator. The node stops at the physicsworld edge.

But when running on iOS7 it falls right trough. Think it has something to do with the category, contact and collision bitmask.

any clue?

Defining the categories here

struct PhysicsCategory {
    static let None: UInt32 = 0
    static let Edge: UInt32 = 0b1 // 1
    static let Player: UInt32 = 0b10 // 2
    static let Enemy: UInt32 = 0b100 // 4
}

Setup World

physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
physicsWorld.contactDelegate = self
physicsBody!.categoryBitMask = PhysicsCategory.Edge
physicsWorld.gravity = CGVectorMake(0, -9.81)

Setup Player/Ball/Node

playerNode.physicsBody = SKPhysicsBody(polygonFromPath: path)
playerNode.physicsBody!.contactTestBitMask = PhysicsCategory.Player
playerNode.physicsBody!.dynamic = true
playerNode.physicsBody!.mass = 0.50
playerNode.physicsBody!.categoryBitMask = PhysicsCategory.Player
playerNode.physicsBody!.collisionBitMask = PhysicsCategory.Enemy | PhysicsCategory.Edge
2

2 Answers

0
votes

hmmm I'm not having that problem. I almost wrote your code verbatim. I assumed playerNode is a SKShapeNode and used it's path in polygonFromPath Can you try running this in iOS7 and see if you still have a problem?

struct PhysicsCategory {
    static let None: UInt32 = 0
    static let Edge: UInt32 = 0b1 // 1
    static let Player: UInt32 = 0b10 // 2
    static let Enemy: UInt32 = 0b100 // 4
}

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

    let playerNode = SKShapeNode(ellipseInRect: CGRect(origin: CGPointZero, size: CGSize(width: 10, height: 10)))

    override func didMoveToView(view: SKView) {

        physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        physicsWorld.contactDelegate = self
        physicsBody!.categoryBitMask = PhysicsCategory.Edge
        physicsWorld.gravity = CGVectorMake(0, -9.81)

        self.addChild(playerNode)


        playerNode.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
        playerNode.physicsBody = SKPhysicsBody(polygonFromPath: playerNode.path)
        playerNode.physicsBody!.dynamic = true
        playerNode.physicsBody!.mass = 0.50
        playerNode.physicsBody!.categoryBitMask = PhysicsCategory.Player
        playerNode.physicsBody!.collisionBitMask = PhysicsCategory.Enemy | PhysicsCategory.Edge
    }

}
0
votes

Finally got it working!

Updated to Yosemite 10.10.1 and Xcode 6.1.1, created a new project. Strange, but works great now.