-1

I'm having some issues figuring out how to detect when a object collides with any objects does anyone know how you would go about doing this?

For reference the project I'm trying to incorporate this into is here - https://github.com/SuperbiaR/Physics-Playground

My goal is to have something happen when the Player and Puff object collides

Superbia
  • 13
  • 6
  • Does this answer your question? [How to Detect collision in Swift, Sprite kit](https://stackoverflow.com/questions/51033558/how-to-detect-collision-in-swift-sprite-kit) – Steve Ives Dec 07 '21 at 08:21

1 Answers1

0

You can use categoryBitMask defined as ContactCategoryPlayer = 0x1 << 0.

you can anther node subclass Puff, and that has physics body with a categoryMask defined as ContactCategoryPuff = 0x1 << 1.

Step 1

Define unique Categories.

let ContactCategoryPlayer: UInt32 = 0x1 << 0   // bitmask is ...00000001
let ContactCategoryPuff: UInt32 = 0x1 << 1   // bitmask is ...00000010

Step 2

Assign the categories.

player.physicsBody?.categoryBitMask = ContactCategoryPlayer
puff.physicsBody?.categoryBitMask = ContactCategoryPuff

Step 3

Assign the categories.

enemy.physicsBody?.collisionBitMask = 0 
puff.physicsBody?.collisionBitMask = 0 

You can invoke these collision handlers via the delegate callback :

// MARK: SKPhysicsContactDelegate
extension GameScene: SKPhysicsContactDelegate {
    func didBegin(_ contact: SKPhysicsContact) {
        print("contact!!!")
    }
}

Here is Reference. https://medium.com/@JohnWatson/simplified-collision-handling-in-spritekit-71de9bea6302

bdeviOS
  • 449
  • 1
  • 6