blue
blue

Reputation: 7375

Swift: sprite collision detection without making physics bodies?

Alright, so I'm fairly new to sprite kit and I'm working in Swift. I have 2 sprite nodes, savior and chicken1. savior has a physics body and this is how I set it up:

var saviorTexture = SKTexture(imageNamed: "1.png")
saviorTexture.filteringMode = SKTextureFilteringMode.Nearest

savior = SKSpriteNode(texture: saviorTexture)
savior.setScale(0.2)
savior.position = CGPoint(x: self.frame.size.width * 0.5, y: self.frame.size.height * 0.2)
savior.physicsBody?.dynamic = true
savior.physicsBody?.allowsRotation = false
savior.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(savior.size.width, savior.size.height))

        self.addChild(savior)

chicken1 right now does not have a physics body:

var chickenTexture = SKTexture(imageNamed: "block1")

        chicken1 = SKSpriteNode(texture: chickenTexture)
        chicken1.setScale(0.75)
        chicken1.position = CGPoint(x: self.size.width * 0.38, y: self.size.height * 1.2)
        self.addChild(chicken1)

I need to know how I can have collision detection (i.e. if savior and chicken1 touch, execute this) between these objects, preferably without making chicken1 a physics body.

When I make chicken1 a physics body, suddenly it becomes erratic and moves other objects around.

If there is not a way to do collision detection without physics, how can I make chicken1 a physics body that is NOT affected by anything and just goes right through the other physics bodies?

EDIT**************

So I followed beginner swift sprite kit - node collision detection help (SKPhysicsContact) and have:

  1. changed my class to be a SKPhysicsContactDelegate:

    class GameScene: SKScene, SKPhysicsContactDelegate {

  2. Written the following:

        self.physicsWorld.contactDelegate = self
    
        savior.physicsBody?.categoryBitMask = saviorCategory
        savior.physicsBody?.contactTestBitMask = animalCategory
        savior.physicsBody?.collisionBitMask = 0
    
        chicken1.physicsBody?.categoryBitMask = animalCategory
        chicken1.physicsBody?.contactTestBitMask = saviorCategory
        chicken1.physicsBody?.collisionBitMask = 0
    
        func didBeginContact(contact: SKPhysicsContact) {
    
        var saviorPhysics: SKPhysicsBody
        var chicken1Physics: SKPhysicsBody
    
        if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask
        {
            saviorPhysics = contact.bodyA
            chicken1Physics = contact.bodyB
        }
        else {
            saviorPhysics = contact.bodyB
            chicken1Physics = contact.bodyA
        }
    
        if saviorPhysics.categoryBitMask == 0 && chicken1Physics.categoryBitMask == 1 {
            self.chicken1.removeFromParent()
        }
    
    }
    

I ran it and there were no errors, but it doesn't work.

Upvotes: 2

Views: 2919

Answers (1)

chrissukhram
chrissukhram

Reputation: 2967

You can do this using categoryBitMask and handling the collision yourself by responding to the SKSceneContactDelegate.

This way you can set different categories for different nodes and handle the collision between the different categories as you please.

Tutorial:

http://seodoa.com/2014/04/01/skphysicsbody-react-to-contact-but-not-to-collide/

Upvotes: 4

Related Questions