Multiple Collision Detection in SpriteKit - swift

I have a dynamic yellow SpriteNode in motion with a categoryBitMask and a contactTestBitMask and it's intended to collide with the static red rectangle SpriteNodes with their own categoryBitMasks and contactTestBitMasks.
I would like to call a function only when both the rectangles have been hit by the yellow sprite? Does anyone know how to do that? I currently have the following code below that I use to check for the collusion with one rectangle.
func didBeginContact(contact: SKPhysicsContact) {
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.categoryBitMask == kYellowCatergory && secondBody.categoryBitMask == kRectangleTarget {
//self.functionToCall()
}
}

Related

game over is not working when player and enemy collide

This is my code:
func didBegin(_ contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if ((firstBody.categoryBitMask & CollisionTypes.enemy.rawValue != 0) && (secondBody.categoryBitMask & CollisionTypes.missile.rawValue != 0)) {
if let enemy = firstBody.node as? SKSpriteNode,
let missile = secondBody.node as? SKSpriteNode {
missileCollidedWithEnemy(missileNode: missile, enemyNode: enemy)
let gameOverScene = GamerOverScene(size: self.size)
self.view?.presentScene(gameOverScene)
}
}
if ((firstBody.categoryBitMask & CollisionTypes.enemy.rawValue != 0) && (secondBody.categoryBitMask & CollisionTypes.player.rawValue != 0)) {
if let enemy = firstBody.node as? SKSpriteNode,
let player = secondBody.node as? SKSpriteNode {
playerCollidedWithEnemy(enemyNode: enemy, playerNode: player)
}
}
if (contact.bodyA.categoryBitMask == playerCategory) {
contact.bodyA.node?.physicsBody?.collisionBitMask = 0
contact.bodyA.node?.physicsBody?.categoryBitMask = 0
} else if (contact.bodyB.categoryBitMask == playerCategory) {
contact.bodyB.node?.physicsBody?.collisionBitMask = 0
contact.bodyB.node?.physicsBody?.categoryBitMask = 0
}
if contact.bodyA.categoryBitMask == enemyCategory {
contact.bodyB.node?.removeFromParent()
contact.bodyB.node?.removeAllActions()
} else if contact.bodyB.categoryBitMask == enemyCategory {
contact.bodyA.node?.removeFromParent()
contact.bodyA.node?.removeAllActions()
}
So my game over does work but only when enemy and missile collide. When I take these lines of code:
let gameOverScene = GamerOverScene(size: self.size)
self.view?.presentScene(gameOverScene)
and place it into the second if statement so game over can show when player and enemy collide, it doesn't work. What's even more confusing is that when I delete the whole first if statement and place the line of code into the second one, the same thing happens, the enemy collides with the missile, and game over pops up.
You are getting expected behavior. You placed the code to show the Game Over scene when the missile and enemy collide.
Move the code to show the Game Over scene to the if statement where the player and enemy collide. You have to do that to get the scene to show when the player and enemy collide.
If the Game Over scene still only appears when the missile and enemy collide, you most likely have a problem with your category bit masks or the raw values of your collision types.

Sprite not disappearing

Here is my code. I am trying to create a space shooter game and when the alien hits the bottom of the screen, I want the player to disappear. I'm not exactly sure what is wrong with this code.
func gameOver() {
let borderBody = SKPhysicsBody(edgeLoopFrom: self.frame)
borderBody.categoryBitMask = borderCategory
func didBegin(_ contact: SKPhysicsContact) {
var firstBody:SKPhysicsBody
var secondBody:SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
}else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if (firstBody.categoryBitMask & alienCategory) != 0 && (secondBody.categoryBitMask & borderCategory) != 0 {
player.removeFromParent()
}

Swift Spritekit Collision Handling

This is my first attempt with SpriteKit and I'm having trouble getting my collision right with bitmasks.
I have three categories, If player hits lit, I want to increase the score and move the lit node off screen, else, I want to call my gameover() function. I've tried a lot of variations and can't see to get anything but general collision to be recognized. I've defined the category and contact bitmasks for each node as well.
let playerCategory: UInt32 = 1
let razzCategory: UInt32 = 2
let litCategory: UInt32 = 4
func didBeginContact(contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
{
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else
{
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if ((firstBody.categoryBitMask & playerCategory) == 0 && (secondBody.categoryBitMask & litCategory) == 1)
{
lit.position.x = 400
score += 1
}
else {
gameOver()
}
}
if ((firstBody.categoryBitMask & playerCategory) == 0 && (secondBody.categoryBitMask & litCategory) == 1) Translates to the following in english.
If Firstbody AND playerCategory = 0 AND SecondBody And litCategory = 1
If Firstbody AND 1 = 0 AND SecondBody And 4 = 1
Now let's define Firstbody as playerCategory and SecondBody as litCategory
If playerCategory AND playerCategory = 0 AND litCategory and litCategory = 1
If 1 AND 1 = 0 AND 4 AND 4 = 1
If 1 = 0 AND 4 = 1
As you can see, this fails, and this method is always going to fail because if the second half of your test (SecondBody AND litCategory) can only have a value of 0 or 4, those 2 values will never be 1.
To correct the issue, you want to make sure that whatever body you are checking is equal to the category you are looking for
if ((firstBody.categoryBitMask & playerCategory) == playerCategory && (secondBody.categoryBitMask & litCategory) == litCategory)
What this says is if the firstBody is a member of the category playerCategory and the secondBody is a member of litCategory, then perform the following operatiion.
Below is the complete fix for your function:
func didBeginContact(contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
{
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else
{
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if ((firstBody.categoryBitMask & playerCategory) == playerCategory && (secondBody.categoryBitMask & litCategory) == litCategory)
{
lit.position.x = 400
score += 1
}
else {
gameOver()
}
}

how to change a physics body shape on contact/collision

I have created a sprite with circle shape physics body. I want to change the circle shape into a rectangle shaped physics body on contact/collision with another body. I believe this should be done in the didBeginContact. Here's what I've done so far
ball.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2)
self.addChild(ball)
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.size.width/2)
ball.physicsBody?.friction = 0
ball.physicsBody?.restitution = 1.2
ball.physicsBody?.linearDamping = 0
ball.physicsBody?.angularDamping = 0
ball.physicsBody?.allowsRotation = false
ball.physicsBody?.applyImpulse(CGVectorMake(2, -4))
func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody
var secondBody : SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.categoryBitMask == ballCategory && secondBody.categoryBitMask == enemyCategory {
//change ball physics shape here
}
}
Remember that a physics body is something that belongs to a node. It's simply a property, and its dependent on that node to exist. All you have to do is swap one body for another in your node's physicsBody property:
//inside of didBeginContact, say you want to change firstBody's body to rectangle
firstBody.node.physicsBody = SKPhysicsBody(rectangleOfSize:...)

Overriding collisions in SpriteKit

I want to override collisions in SpriteKit.
The idea is that I have a ball which bounces around the scene. When didBeginContact detects contact between an edge and the ball, I want the ball to rebound in a random direction and speed.
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let kEdgeCollisionCategory:UInt32 = 0x1 << 1
let kSquareCollisionCategory:UInt32 = 0x1 << 2
override func didMoveToView(view: SKView) {
/* Setup your scene here */
// Physics world
self.physicsWorld.gravity = CGVectorMake(0.0, 0.0)
self.physicsWorld.contactDelegate = self
// Edge
let frameEdges = SKNode()
frameEdges.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
frameEdges.physicsBody?.categoryBitMask = kEdgeCollisionCategory
self.addChild(frameEdges)
// Sprite
var sprite = SKSpriteNode(imageNamed: "blue")
sprite.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(sprite.size.width, sprite.size.height))
sprite.physicsBody?.restitution = 1.0
sprite.physicsBody?.linearDamping = 0.0
sprite.physicsBody?.angularDamping = 0.0
sprite.physicsBody?.friction = 0.0
sprite.physicsBody?.dynamic = true
sprite.physicsBody?.categoryBitMask = kSquareCollisionCategory
sprite.physicsBody?.collisionBitMask = kEdgeCollisionCategory
sprite.physicsBody?.contactTestBitMask = kEdgeCollisionCategory
self.addChild(sprite)
}
func didBeginContact(contact: SKPhysicsContact) {
var firstBody:SKPhysicsBody?
var second:SKPhysicsBody?
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
var randomX = CGFloat(arc4random_uniform(UInt32(4))))
var randomY = CGFloat(arc4random_uniform(UInt32(4))))
if firstBody!.categoryBitMask == kSquareCollisionCategory && secondBody!.categoryBitMask == kEdgeCollisionCategory {
firstBody!.velocity = (CGVectorMake(firstBody!.velocity.dx * randomX, firstBody!.velocity.dy * randomY))
}
}
}
Upon contact you can apply a force or impulse to the ball depending on the ball's current vector. For example, upon contact the ball's vector is (30,-20). This example vector translates to the ball moving right and and down. You can then apply a new vector making the ball move left and up (-20, 25). You can use arc4random to set new values or set static ones.
If you need, read up on using vectors with physics bodies.
Your problem is here ,
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
This means first body has categoryBitMask of kEdgeCollisionCategory and second body has categoryBitMask of kSquareCollisionCategory. So change this to,
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
else {
firstBody = contact.bodyA
secondBody = contact.bodyB
}
To get random number use this,
int minSpeed = 1;
int maxSpeed = 20;
int randNum = rand() % (max-min) + min;
Then apply impulse over the required body.