Why Is There Multiple Collision Calls Sprite Kit Swift

Why is there multiple collision calls Sprite Kit Swift

Could it possibly be due to the physicsBody:

var star = SKSpriteNode(imageNamed: "star")
star.size = CGSizeMake(30, 30)
star.zPosition = 10
var starPhysicsRect = CGRectMake(star.position.x - star.frame.size.width / 2, star.position.y - star.frame.size.width / 2, star.frame.size.width, star.frame.size.height)
star.physicsBody = SKPhysicsBody(edgeLoopFromRect: starPhysicsRect)

Have a look at the logic creating the starPhysicsRect:

(star.position.x - star.frame.size.width / 2, star.position.y - star.frame.size.width / 2, star.frame.size.width, star.frame.size.height)

From what I can see this translates to the following values:
(-15, -15, 30, 30) those negative position-coordinates seem odd. Are you sure you don't just want: SKPhysicsBody(rectangleOfSize: star.size) ?

Sprite-Kit registering multiple collisions for single contact

OK - it would appear that a simple:

        if bomb == nil {return}

is all that's required. This should be added as follows:

        let bomb = contact.bodyA.categoryBitMask == category.bomb.rawValue ? contact.bodyA.node : contact.bodyB.node
if bomb == nil {return}

This works to prevent multiple collisions for a node that you removeFromParent in didBeginContact. If you don;t remove the node but are still registering multiple collisions, then use the node's userData property to set some sort of flag indicating that the node i s'anactive' Alternately, subclass SKSPriteNode and add a custom 'isActive' property, which is what I did to solve my problem of bullets passing up the side of an invader and taking out that invader and the one above it. This never happens on a 'direct hit'.

It doesn't answer the underlying question as to why SK is registering multiple collisions, but it does mean that I can remove all the extra code concerning setting contactTestBitMasks to 0 and then back to what they should be later etc.

Edit: So it appears that if you delete a node in didBeginContact, the node is removed but the physics body isn't. So you have to be careful as Sprite-Kit appears to build an array of physicsContacts that have occurred and then calls dBC multiple times, once for each contact. So if you are manipulating nodes and also removing them in dBC, be aware that you might run into an issue if you force unwrap a node's properties.

Swift SpriteKit Collisions Registered Multiple Times

Give your bullet node a name:

let bullet = SKSPriteNode()
bullet.name = "bullet"

Then check your physics contact

if (contact.bodyA.node?.name == "bullet") {
contact.bodyA.node?.removeFromParent()
} else if contact.bodyB.node?.name == "bullet" {
contact.bodyB.node?.removeFromParent()
}

Good luck!

Swift/SpriteKit Multiple Collision Detection?

Several problems here.

  1. You're defining categories in a way that keeps them from being easily tested.
  2. You're testing categories in a way that doesn't get you the unique answers you want.
  3. You've confused your code by trying to track up to four bodies in one contact. Any contact will always have exactly two bodies.

Let's solve them one at a time...

1. Defining Categories

You want to define collision categories so that each kind of body in your game uses its own bit in the mask. (You've got a good idea using Swift's binary literal notation, but you're defining categories that overlap.) Here's an example of non-overlapping categories:

struct PhysicsCategory: OptionSet {
let rawValue: UInt32
init(rawValue: UInt32) { self.rawValue = rawValue }

static let enemy = PhysicsCategory(rawValue: 0b001)
static let bullet = PhysicsCategory(rawValue: 0b010)
static let spiral = PhysicsCategory(rawValue: 0b100)
}

I'm using a Swift OptionSet type for this, because it makes it easy to make and test for combinations of unique values. It does make the syntax for defining my type and its members a bit unwieldy compared to an enum, but it also means I don't have to do a lot of boxing and unboxing raw values later, especially if I also make convenience accessors like this one:

extension SKPhysicsBody {
var category: PhysicsCategory {
get {
return PhysicsCategory(rawValue: self.categoryBitMask)
}
set(newValue) {
self.categoryBitMask = newValue.rawValue
}
}
}

Also, I'm using the binary literal notation and extra whitespace and zeroes in my code so that it's easy to make sure that each category gets its own bit — enemy gets only the least significant bit, bullet the next one, etc.

2 & 3. Testing & Tracking Categories

I like to use a two-tiered approach to contact handlers. First, I check for the kind of collision — is it a bullet/enemy collision or a bullet/spiral collision or a spiral/enemy collision? Then, if necessary I check to see which body in the collision is which. This doesn't cost much in terms of computation, and it makes it very clear at every point in my code what's going on.

func didBegin(_ contact: SKPhysicsContact) {
// Step 1. To find out what kind of contact we have,
// construct a value representing the union of the bodies' categories
// (same as the bitwise OR of the raw values)
let contactCategory: PhysicsCategory = [contact.bodyA.category, contact.bodyB.category]

if contactCategory.contains([.enemy, .bullet]) {
// Step 2: We know it's an enemy/bullet contact, so there are only
// two possible arrangements for which body is which:
if contact.bodyA.category == .enemy {
self.handleContact(enemy: contact.bodyA.node!, bullet: contact.bodyB.node!)
} else {
self.handleContact(enemy: contact.bodyB.node!, bullet: contact.bodyA.node!)
}
} else if contactCategory.contains([.enemy, .spiral]) {
// Here we don't care which body is which, so no need to disambiguate.
self.gameOver()

} else if contactCategory.contains([.bullet, .spiral]) {
print("bullet + spiral contact")
// If we don't care about this, we don't necessarily
// need to handle it gere. Can either omit this case,
// or set up contactTestBitMask so that we
// don't even get called for it.

} else {
// The compiler doesn't know about which possible
// contactCategory values we consider valid, so
// we need a default case to avoid compile error.
// Use this as a debugging aid:
preconditionFailure("Unexpected collision type: \(contactCategory)")
}
}

Extra Credit

Why use if statements and the OptionSet type's contains() method? Why not do something like this switch statement, which makes the syntax for testing values a lot shorter?

switch contactCategory {
case [.enemy, .bullet]:
// ...
case [.enemy, .spiral]:
// ...

// ...

default:
// ...
}

The problem with using switch here is that it tests your OptionSets for equality — that is, case #1 fires if contactCategory == [.enemy, .bullet], and won't fire if it's [.enemy, .bullet, .somethingElse].

With the contact categories we've defined in this example, that's not a problem. But one of the nice features of the category/contact bit mask system is that you can encode multiple categories on a single item. For example:

struct PhysicsCategory: OptionSet {
// (don't forget rawValue and init)
static let ship = PhysicsCategory(rawValue: 0b0001)
static let bullet = PhysicsCategory(rawValue: 0b0010)
static let spiral = PhysicsCategory(rawValue: 0b0100)
static let enemy = PhysicsCategory(rawValue: 0b1000)
}

friendlyShip.physicsBody!.category = [.ship]
enemyShip.physicsBody!.category = [.ship, .enemy]
friendlyBullet.physicsBody!.category = [.bullet]
enemyBullet.physicsBody!.category = [.bullet, .enemy]

In a situation like that, you could have a contact whose category is [.ship, .bullet, .enemy] — and if your contact handling logic is testing specifically for [.ship, .bullet], you'll miss it. If you use contains instead, you can test for the specific flags you care about without needing to care whether other flags are present.

Why are didBeginContact called multiple times?

I had the same problem (score increasing multiple times for a single enemy destroyed and multiple life points being lost for a single instance of damage.) A user on the Apple forums thinks that it's a bug in [SKPhysicsBody bodyWithTexture:size:] but I don't believe that's the case, because it was happening with other constructors too.

First off, the categoryBitMask and contactTestBitMask are very important, obviously. Take a look at Apple's SpriteKit Physics Collisions sample code:

// Contacts are often a double dispatch problem; the effect you want is based on the type of both bodies in the contact. This sample this in a brute force way, by checking the types of each. A more complicated example might use methods on objects to perform the type checking.

// The contacts can appear in either order, and so normally you'd need to check
each against the other. In this example, the category types are well ordered, so
the code swaps the two bodies if they are out of order. This allows the code
to only test collisions once.

What I did to solve it was setting a flag after handling each condition. In my case, I was testing whether bodyA.node.parent was nil in didBeginContact, because I called removeFromParent() on the missile/enemy nodes to destroy them.

I think you should expect the event to fire multiple times and your code in there has to make sure it's processed only once.

Have a collision only detected once

Yep - this happens. The way to handle it (you can't get sprite-kit to NOT call didBegin multiple times in some circumstances) is to make sure that your contact code accommodates this and that handling the contract multiple times does not cause a problem (such as adding to the score multiple times, removing multiple lives, trying to access a node or physicsBody that has been removed etc).

There is a discussion here: Sprite-Kit registering multiple collisions for single contact

Some things you can do include:

  • If you remove a node that is contacted, check for it being nil before
    you remove it (for the duplicate contacts)
  • Add the node to a set and then remove all the nodes in the set in
    didFinishUpdate
  • Add an 'inactive' flag' to the node's userData
  • Make the node a subclass of SKSpriteNode and add an inactive property
  • Etc etc.


Related Topics



Leave a reply



Submit