SpriteKit sprite collision detection without rebound

I am new to SpriteKit / Swift coding and have the following problem: It is assumed that the character collects coins by jumping into them. There is no problem in detecting a collision and getting rid of the collected coin, but my character bounces off the coin before it disappears.

The character must fly through the coin and collect it along the way.

let playerCategory: UInt32 = 0x1 << 0 let coinCategory: UInt32 = 0x1 << 1 

 player = SKSpriteNode(texture: playerTexture) player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.height / 2) player.physicsBody?.dynamic = true player.physicsBody?.allowsRotation = false player.physicsBody?.categoryBitMask = playerCategory player.physicsBody?.contactTestBitMask = coinCategory var coin:SKSpriteNode = SKSpriteNode(texture: coinTexture) coin.physicsBody = SKPhysicsBody(circleOfRadius: coin.size.height / 2) coin.physicsBody?.dynamic = false coin.physicsBody?.allowsRotation = false coin.physicsBody?.categoryBitMask = coinCategory coin.physicsBody?.contactTestBitMask = playerCategory 

 func playerDidCollideWithCoin(player:SKSpriteNode, thisCoin:SKSpriteNode) { thisCoin.removeFromParent() coinsCollected++ } 

Collision detection works like this, but, as I said, how can I avoid flying and replace it with β€œflying”?

I am using Xcode 6 Beta 7

Thanks in advance!

The solution is in the comments below;)

+6
source share
2 answers

By default, the spritekit behavior is that everything collides with everything if the Collision bit is not changed to 0.

Change this value in your code to 0 in all those objects that you do not want to bounce, but receive notifications from them.

 player = SKSpriteNode(texture: playerTexture) player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.height / 2) player.physicsBody?.dynamic = true player.physicsBody?.allowsRotation = false player.physicsBody?.categoryBitMask = playerCategory player.physicsBody?.contactTestBitMask = coinCategory player.physicsBody?.collisionBitMask = 0 var coin:SKSpriteNode = SKSpriteNode(texture: coinTexture) coin.physicsBody = SKPhysicsBody(circleOfRadius: coin.size.height / 2) coin.physicsBody?.dynamic = false coin.physicsBody?.allowsRotation = false coin.physicsBody?.categoryBitMask = coinCategory coin.physicsBody?.contactTestBitMask = playerCategory coin.physicsBody?.collisionBitMask = 0 

This will cause these objects to not bounce from each other.

+4
source

You are almost there. There is a third mask: collisionBitMask

A mask that determines which categories of physical bodies can collide with this physical body.

Given that we have:

 enum { Ground = 1, Player = 1<<1, Coin = 1<<2, }; player.categoryBitMask = Player; player.collisionBitMask = Ground; player.contactTestBitMask = Ground | Coin; ground.categoryBitMask = Ground; ground.collisionBitMask = Player; ground.contactTestBitMask = Player; coin.categoryBitMask = Coin; coin.collisionBitMask = 0; coin.contactTestBitMask = Player; 

The body of the player in this case detects contact with the ground and the coin, but will only collide with the ground.

0
source

Source: https://habr.com/ru/post/974836/


All Articles