Search code examples
swiftswift3sprite-kitskphysicsbody

Swift 3 Physicsbody Collisions doesn't work


This is my class for the Player

My Player doesn't collide with the Gamefield they just go through each other. I dont want them to be on top of each other. I tried to google the solution but for me it seems that I did mostly all right.

Please Help:

class Player: SKShapeNode {
    static public let length: CGFloat = 50
    static public let rect = CGRect(x: -length/2, y: -length/2, width: length, height: length)

    var life = 100

    override init() {
        super.init()
        self.fillColor = SKColor.blue
        self.strokeColor = SKColor.black
        self.position = CGPoint(x: 50, y: 50)

        self.physicsBody = SKPhysicsBody(edgeLoopFrom: Player.rect)
        self.physicsBody?.isDynamic = true
        self.physicsBody?.allowsRotation = false
        self.physicsBody?.categoryBitMask = PhysicsCategory.Robot
        self.physicsBody?.contactTestBitMask = PhysicsCategory.Projectile
        self.physicsBody?.collisionBitMask = PhysicsCategory.Gamefield
        self.physicsBody?.usesPreciseCollisionDetection = true
    }; required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}

    func moveBy(vect: CGVector) {
        self.position.x += vect.dx
        self.position.y += vect.dy
        //print(vect.dx, vect.dy)
    }
}

This is my Gamescene Class

struct PhysicsCategory {
    static let None         : UInt32 = UInt32.min
    static let All          : UInt32 = UInt32.max
    static let Robot        : UInt32 = 0b0001      // 1
    static let Gamefield    : UInt32 = 0b0010      // 2
    static let Monster      : UInt32 = 0b0011      // 3
    static let Projectile   : UInt32 = 0b0100      // 4
}

class GameScene: SKScene, SKPhysicsContactDelegate {
    var player = Player(rect: Player.rect)
    var controlL: Control
    var controlR: Control

    var cam = SKCameraNode()

    override init(size: CGSize) {
        controlL = Control(posX: 0, size: size, direction: 1)
        controlR = Control(posX: size.width, size: size, direction: -1)

        super.init(size: size)

    }; required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}

    override func didMove(to view: SKView) {
        self.backgroundColor = SKColor.white

        physicsWorld.gravity = CGVector.zero
        physicsWorld.contactDelegate = self

        cam.xScale = 1
        cam.yScale = 1
        self.camera = cam
        player.addChild(cam)

        let gameFieldRect = CGRect(x: 0, y: 0, width: 1000, height: 600)
        let gameField = SKShapeNode(rect: gameFieldRect)
            gameField.fillColor = SKColor.clear
            gameField.strokeColor = SKColor.black
            gameField.position = CGPoint(x: 0, y: 0)

            gameField.physicsBody = SKPhysicsBody(edgeLoopFrom: gameFieldRect)
            gameField.physicsBody?.isDynamic = true
            gameField.physicsBody?.allowsRotation = false
            gameField.physicsBody?.categoryBitMask = PhysicsCategory.Gamefield
            gameField.physicsBody?.contactTestBitMask = PhysicsCategory.None
            gameField.physicsBody?.collisionBitMask = PhysicsCategory.Robot
        self.addChild(gameField)

        self.addChild(player)

        cam.addChild(controlL.add())
        cam.addChild(controlR.add())

I hope someone sees the mistake. It took me quite long allready.


Solution

  • You need to change your robot to a different physics body type (rectangleOf)?:

    SpriteKit supports two kinds of physics bodies, volume-based bodies and edge-based bodies. When you create a physics body, its kind, size, and shape are determined by the constructor method you call. An edge-based body does not have mass or volume and is unaffected by forces or impulses in the system. Edge-based bodies are used to represent volume-less boundaries or hollow spaces in your physics simulation. In contrast, volume-based bodies are used to represent objects with mass and volume.

    Also, are you using impulses or forces to move your robot? Not .move(to:) or .position =? move and position will break your physics world (it will go through it in some scenarios)

    Also, it looks like you need to change your category masks to a proper series (for didBegin(contact:))

    it should be, 1, 2 , 4, 8, 16, so on... that is how you get unique contact hits.. Right now you could get a hit of "4" from 0+4 or 1+3... thus, not unique collisions.

    Your collisionBitMask looks good though.. They should be bumping into each other.