didBeginContact not working properly - swift

I created a struct PhysicsCatagory for each of the different objects I want interacting with each other
struct PhysicsCatagory {
static let Blade : UInt32 = 1
static let Laser : UInt32 = 2
}
above my class GameScene
class GameScene: SKScene, SKPhysicsContactDelegate {
and I initialized an SKSpriteNode blade with its physicsBody in the didMoveToView method
override func didMoveToView(view: SKView) {
physicsWorld.contactDelegate = self
Blade.position = CGPointMake(self.size.width / 2, (self.size.height / 14))
Blade.anchorPoint = CGPointMake(0.5, -0.13)
Blade.physicsBody = SKPhysicsBody(rectangleOfSize: Blade.size)
Blade.physicsBody?.categoryBitMask = PhysicsCatagory.Blade
Blade.physicsBody?.contactTestBitMask = PhysicsCatagory.Laser
Blade.physicsBody?.dynamic = false
self.addChild(Blade)
}
As well as a SKSpriteNode laser and its physicsbody in the method shootLaser
func shootLaser(){
var Laser = SKSpriteNode(imageNamed: "Laser.png")
Laser.position = Enemy.position
Laser.zPosition = -5
Laser.physicsBody = SKPhysicsBody(rectangleOfSize: Laser.size)
Laser.physicsBody?.categoryBitMask = PhysicsCatagory.Laser
Laser.physicsBody?.contactTestBitMask = PhysicsCatagory.Blade
Laser.physicsBody?.dynamic = false
let action = SKAction.moveBy(laserVector, duration: 0.7)
let actionDone = SKAction.removeFromParent()
Laser.runAction(SKAction.sequence([action,actionDone]))
self.addChild(Laser)
}
But when they collide in the simulation, the didBeginContact method is not called and "Hello" is not printed
func didBeginContact(contact: SKPhysicsContact) {
NSLog("Hello")
}
Why isn't the didBeginContact method being called when they collide? Thanks in advance (:

Sprite Kit does not check for contacts between non-dynamic physics bodies because they aren't expected to move. If you don't want your sprites to fall off the screen due to gravity, set the physics body's affectedByGravity property to false and set dynamic = true.

Related

SpriteKit collision detection not working as expected

So I have this code inside my GameScene class (child of SKScene):
physicsWorld.contactDelegate = self
physicsWorld.gravity = CGVector(dx: 0, dy: -9.8)
addChild(balloon)
addChild(monkey)
monkey.physicsBody = SKPhysicsBody(rectangleOf: monkey.frame.size)
monkey.physicsBody?.isDynamic = false
monkey.physicsBody?.categoryBitMask = 0
monkey.physicsBody?.contactTestBitMask = 1//Balloon.categoryBitMask
balloon.physicsBody = SKPhysicsBody(rectangleOf: balloon.frame.size)
balloon.physicsBody?.isDynamic = false
balloon.physicsBody?.categoryBitMask = 1
balloon.physicsBody?.contactTestBitMask = 0
balloon.start()
I can see that the balloon and the monkey node have touched each other in the simulator, however, nothing happens. I have also conformed to the SKPhysicsContactDelegate protocol, like so:
extension GameScene: SKPhysicsContactDelegate {
func didBegin(_ contact: SKPhysicsContact) {
print("CONTACT!!!")
}
}
Edit: I set isDynamic = true and it printed "CONTACT!!" as expected, however I dont want the objects to be affecting each other's position

Gameover on any collision - Swift Spritekit

In my game I have planes moving around. In my game, I want so that if any plane collides with another, it prints "game over". I have added SKPhysicsContactDelegate to my gamescene. I added a physics bodies to my planes. Then, I added this function to my didMoveToView function:
func didBegin(_ contact: SKPhysicsContact){
print("Game over")
}
Now, when I run my game, and the planes collide, nothing prints to the console. How can I change my code so if any plane collides with another (there are more than 2) it prints game over to the console?
Edit: I have set the physics world contact delegate to self. I have not called this function - do I have to - I thought that this function runs when there is a collision in the scene.
Here is my code:
//
// GameScene.swift
// PlaneGame
//
// Created by Lucas Farleigh on 09/04/2017.
// Copyright © 2017 Farleigh Tech. All rights reserved.
//
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
private let Background = SKSpriteNode(imageNamed: "Background")
private let PlaneRed = SKSpriteNode(imageNamed: "PlaneRed")
private let PlaneBlue = SKSpriteNode(imageNamed: "PlaneBlue")
private let PlaneRed2 = SKSpriteNode(imageNamed: "PlaneRed2")
private let PlaneBlue2 = SKSpriteNode(imageNamed: "PlaneBlue2")
private let StartButton = SKLabelNode(fontNamed: "Chalkduster")
private let Trail = SKSpriteNode(imageNamed: "BlueTrail")
private let Center = SKNode()
private let Center2 = SKNode()
var GameHasStarted = false
override func didMove(to view: SKView) {
//Defining the position,size and ZPosition for the background
Background.position = CGPoint(x:self.frame.midX / 2,y: self.frame.midY)
Background.xScale = 10.0
Background.yScale = 10.0
Background.zPosition = -10
addChild(Background)
//Defining a start button - will be used later as a button
StartButton.position = CGPoint(x:self.frame.midX,y:self.frame.minY + 100)
StartButton.text = "Tap Anywhere To Start"
StartButton.fontSize = 50.0
StartButton.name = "StartButton"
addChild(StartButton)
//Setting the planered position and size up for the plane, ready to start the game
PlaneRed.position = CGPoint(x:self.frame.midX - 250,y: self.frame.midY)
PlaneRed.xScale = 0.13
PlaneRed.yScale = 0.13
PlaneRed.zPosition = 10
//Setting the planeblue position and size up for the plane, ready to start the game
PlaneBlue.position = CGPoint(x:self.frame.midX + 250,y: self.frame.midY)
PlaneBlue.xScale = 0.13
PlaneBlue.yScale = -0.13
PlaneBlue.zPosition = 10
//Setting the planered position and size up for the plane, ready to start the game
PlaneRed2.position = CGPoint(x:self.frame.midX,y: self.frame.midY + 250)
PlaneRed2.xScale = -0.13
PlaneRed2.yScale = 0.13
PlaneRed2.zPosition = 10
//Setting the planeblue position and size up for the plane, ready to start the game
PlaneBlue2.position = CGPoint(x:self.frame.midX,y: self.frame.midY - 250)
PlaneBlue2.xScale = 0.13
PlaneBlue2.yScale = 0.13
PlaneBlue2.zPosition = 10
//Making the trail
Trail.position = CGPoint(x:self.frame.midX,y:self.frame.midY)
Trail.xScale = 1.08
Trail.yScale = 1.08
addChild(Trail)
//In order to rotate the planes around a point, we must create the point as an SKNode, and make the planes a child of the point
//The point then rotates bringing the planes with it
//Setting up the point where the plane will rotate around
Center.position = CGPoint(x:self.frame.midX,y:self.frame.midY)
addChild(Center)
Center2.position = CGPoint(x:self.frame.midX,y:self.frame.midY)
addChild(Center2)
Center.addChild(PlaneRed)
Center.addChild(PlaneBlue)
Center2.addChild(PlaneRed2)
Center2.addChild(PlaneBlue2)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//ADDING PHYSICS TO PLANES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Defining the red planes physics body
PlaneRed.physicsBody = SKPhysicsBody(rectangleOf: PlaneRed.size)
PlaneRed2.physicsBody = SKPhysicsBody(rectangleOf: PlaneRed2.size)
physicsWorld.contactDelegate = self
physicsWorld.gravity = CGVector.zero
func didBegin(contact: SKPhysicsContact){
print("Game over")
}
}
func Start(){
//Defining an SKAction for the plane to orbit the center
let OrbitCenter = SKAction.rotate(byAngle: CGFloat(-2), duration: 3.8)
let Orbit = SKAction.repeatForever(OrbitCenter)
//Creating the action for the center 2 to rotate anti clockwise
let AntiOrbitCenter = SKAction.rotate(byAngle: CGFloat(2), duration: 3.8)
let AntiOrbit = SKAction.repeatForever(AntiOrbitCenter)
//Running the SKAction on the plane
Center.run(Orbit)
Center2.run(AntiOrbit)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?){
for touch in touches{
//Setting up the touch settings - these two variables store the nodes that the user has touched by first defining the location and then checking for nodes at this location
let location = touch.location(in: self)
let node = self.atPoint(location)
if GameHasStarted == false{
Start()
GameHasStarted = true
}
}
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
First
Your didBegin func needs to be outside of didMove func
override func didMove(to view: SKView) {
}
func didBegin(_ contact: SKPhysicsContact) {
print("Game over")
}
second
you need to setup some physics categories for your objects so they
know what they can collide with, what they can pass through and what
collisions don't matter. You can put this outside your class
declaration
//Game Physics
struct PhysicsCategory {
static let plane: UInt32 = 0x1 << 0
static let plane2: UInt32 = 0x1 << 1
static let obstacle: UInt32 = 0x1 << 2
}
third
You need to add those physics decorations to your objects
//Defining the red planes physics body
PlaneRed.physicsBody = SKPhysicsBody(rectangleOf: PlaneRed.size)
PlaneRed.physicsBody?.categoryBitMask = PhysicsCategory.plane
PlaneRed.physicsBody?.collisionBitMask = PhysicsCategory.plane
PlaneRed.physicsBody?.contactTestBitMask = PhysicsCategory.plane
PlaneRed.physicsBody?.isDynamic = true
PlaneRed2.physicsBody = SKPhysicsBody(rectangleOf: PlaneRed2.size)
PlaneRed2.physicsBody?.categoryBitMask = PhysicsCategory.plane
PlaneRed2.physicsBody?.collisionBitMask = PhysicsCategory.plane
PlaneRed2.physicsBody?.contactTestBitMask = PhysicsCategory.plane
PlaneRed2.physicsBody?.isDynamic = true
PlaneBlue.physicsBody = SKPhysicsBody(rectangleOf: PlaneBlue.size)
PlaneBlue.physicsBody?.categoryBitMask = PhysicsCategory.plane
PlaneBlue.physicsBody?.collisionBitMask = PhysicsCategory.plane
PlaneBlue.physicsBody?.contactTestBitMask = PhysicsCategory.plane
PlaneBlue.physicsBody?.isDynamic = true
PlaneBlue2.physicsBody = SKPhysicsBody(rectangleOf: PlaneBlue2.size)
PlaneBlue2.physicsBody?.categoryBitMask = PhysicsCategory.plane
PlaneBlue2.physicsBody?.collisionBitMask = PhysicsCategory.plane
PlaneBlue2.physicsBody?.contactTestBitMask = PhysicsCategory.plane
PlaneBlue2.physicsBody?.isDynamic = true
self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVector.zero

How do I create a collision with SpriteKit with an already created SKSpriteNode?

I want the pacman to restart from its original position when it collides with blinky, that is moving.
How can I make them collide considering I have already declared them?
You move the pacman, but blinky moves alone. I want it to work like the pacman game.
public class PacmanScene: SKScene {
let playerSpeed: CGFloat = 40.0
var pacman: SKSpriteNode?
var playerTextures: [SKTexture] = []
var lastTouch: CGPoint? = nil
var blinky: SKSpriteNode?
var clyde: SKSpriteNode?
var inky: SKSpriteNode?
var pinky: SKSpriteNode?
override public init(size: CGSize) {
let pacmanTexture = SKTexture(imageNamed: "pacman01.png")
pacman = SKSpriteNode(texture: pacmanTexture)
pacman?.name = "pacman"
pacman?.position = CGPoint(x:30, y:30)
pacman?.zPosition = 1.0
pacman?.physicsBody = SKPhysicsBody(texture: pacmanTexture, size: CGSize(width: (pacman?.size.width)!, height: (pacman?.size.height)!))
pacman?.physicsBody?.allowsRotation = true
pacman?.physicsBody?.affectedByGravity = false
pacman?.physicsBody?.mass = 2
let blinkyTexture = SKTexture(imageNamed: "blinky.png")
blinky = SKSpriteNode(texture: blinkyTexture)
blinky?.name = "blinky"
blinky?.position = CGPoint(x: 15, y: 60)
blinky?.zPosition = 1.0
blinky?.physicsBody = SKPhysicsBody(texture: pacmanTexture, size: CGSize(width: (blinky?.size.width)!, height: (blinky?.size.height)!))
blinky?.physicsBody?.allowsRotation = false
blinky?.physicsBody?.affectedByGravity = false
blinky?.physicsBody?.mass = 1000
super.init(size: size)
addChild(pacman!)
addChild(blinky!)
override public func didMove(to view: SKView) {
let bmoveUp = SKAction.moveBy(x: 0, y: 450, duration: 4.0)
let bmoveRight = SKAction.moveBy(x:20, y:0, duration: 1.0)
let bmoveDown = SKAction.moveBy(x:0, y: -450, duration: 4.0)
let bmoveLeft = SKAction.moveBy(x:-20, y:0, duration: 1.0)
let bsequence = SKAction.sequence([bmoveUp, bmoveRight, bmoveDown, bmoveLeft])
let bendlessAction = SKAction.repeatForever(bsequence)
blinky?.run(bendlessAction)
}
If iv got this right you want your "blinky" to follow your "pacman" to do this you would have to work out the position of the pacman then add an SKAction to your blinky to move to that position.
Try something like this
//Speed blinky moves
let blinkySpeed = 100
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
updateBlinky()
}
func updateBlinky() {
//Set the point that blinky moves to
let point = CGPoint(x: pacman.position.x, y: pacman.position.y)
//Get the distance its got to travel
let distance = distanceBetweenPoints(first: pacman.position, second: blinky.position)
//Get the time is got to take from the speed and distance
let time = distance / blinkySpeed
//Create and run the action
let action = SKAction.move(to: point, duration: TimeInterval(time))
blinky.run(action)
}
//work out the distance between the sprites
func distanceBetweenPoints(first: CGPoint, second: CGPoint) -> Int {
return Int(hypot(second.x - first.x, second.y - first.y))
}
The end result would be something like this
Edit:
Okay I think from your question you already have "blinky" moving you just want to detect collisions.
First you need to add the SKPhysicsContactDelegate to your class
public class PacmanScene: SKScene, SKPhysicsContactDelegate {
Then you need to add a category bit mask to both the sprites then handle the collisions in didBegin(_ contact: SKPhysicsContact) method.
What I would do
//Create Physics category struct
struct PhysicsCategory {
static let pacman : UInt32 = 0x1 << 1
static var blinky : UInt32 = 0x1 << 2
}
Then where you are setting up pacman and blinky set the category bitmask
//Set the category bit mask for pacman
pacman?.physicsBody?.categoryBitMask = PhysicsCategory.pacman
//Set what categories you want to test contact
pacman?.physicsBody?.contactTestBitMask = PhysicsCategory.blinky
//Set what categories you want to collide with each other
pacman?.physicsBody?.collisionBitMask = PhysicsCategory.blinky
//Set the category bit mask for blinky
blinky?.physicsBody?.categoryBitMask = PhysicsCategory.blinky
//Set what categories you want to test contact
blinky?.physicsBody?.contactTestBitMask = PhysicsCategory.pacman
//Set what categories you want to collide with each other
blinky?.physicsBody?.collisionBitMask = PhysicsCategory.pacman
Then you would need to implement the didBegin(_ contact: SKPhysicsContact) method to handle the collisions
func didBegin(_ contact: SKPhysicsContact) {
//Work out which contact was pacman
let other = contact.bodyA.categoryBitMask == PhysicsCategory.pacman ? contact.bodyB : contact.bodyA
//Test what it hit
switch other.categoryBitMask {
case PhysicsCategory.blinky:
print("pacman hit blinky")
//Move pacman
pacman?.position = CGPoint(x:30, y:30)
default:
break
}
}
Hope this helps

Sprite Kit never detect my collision

When player body meet a monster body the function func didBeginContact(contact: SKPhysicsContact) is never called.
My class implement SKPhysicsContactDelegate
class GameScene: SKScene, SKPhysicsContactDelegate
the property collisionBitMask i commented it because i read that if we don't put it, node can collise with all node
I add my player like this :
bear.position = CGPoint(x:self.frame.width/20, y:self.frame.height/5)
bear.size = CGSize(width: self.frame.width/7, height: self.frame.height/5)
bear.physicsBody = SKPhysicsBody(rectangleOfSize: bear.frame.size)
bear.physicsBody?.dynamic = false;
bear.physicsBody?.categoryBitMask = PhysicsCategory.Player
bear.physicsBody?.contactTestBitMask = PhysicsCategory.Monster
//bear.physicsBody?.collisionBitMask = PhysicsCategory.Monster
I add monster like this:
let thePosition = CGPoint(x: positionX, y: (self.frame.height/7.4)*positionY)
let monster = SKSpriteNode(imageNamed: pattern as String)
monster.size = CGSize(width: self.frame.width/16, height: self.frame.height/11)
monster.name = name
monster.position = thePosition
monster.physicsBody = SKPhysicsBody(rectangleOfSize: monster.frame.size)
monster.physicsBody?.dynamic = false
monster.physicsBody?.categoryBitMask = PhysicsCategory.Monster // 3
monster.physicsBody?.affectedByGravity = false
monster.physicsBody?.contactTestBitMask = PhysicsCategory.Player // 4
//platformNode.physicsBody?.collisionBitMask = PhysicsCategory.Player // 5
addChild(monster)
my PhysicalCategory
struct PhysicsCategory {
static let None : UInt32 = 0
static let All : UInt32 = UInt32.max
static let Player: UInt32 = 0b1 // 1
static let Monster : UInt32 = 0b10 // 2
static let Projectile: UInt32 = 0b100 // 3
static let Plateforme: UInt32 = 0b1000 // 4
}
I have multiple Textures, for example when player walk i call this :
func walkingBear() {
bear.runAction( SKAction.repeatActionForever(SKAction.animateWithTextures(bearCoursFrames, timePerFrame: 0.1, resize: false, restore: true)), withKey:"cours")
}
I initlaise bearCoursFrames like this
var walkFrames = [SKTexture]()
var numImages : Int = bearAnimatedAtlas.textureNames.count
for var i=1; i<=numImages/2; i++ {
let bearTextureName = "cours\(i)"
walkFrames.append(bearAnimatedAtlas.textureNamed(bearTextureName))
}
bearCoursFrames = walkFrames
Where is my error please, i can't find a solution in other post
any help would be appreciated
---------------------------------EDIT------------------------------
Solution: I just add self.physicsWorld.contactDelegate = self and it works now.
The answer is simple. There is a rule which says that at least one body has to be defined as dynamic in order to register the contact.
Change either player or other body to be dynamic and contact will be detected.
As an addition, if you already haven't, enable physics visual representation ... It can be helpful because sometimes contact is not detected because the actual bodies are not intersecting each other eg. when node's anchor point is changed, so the texture and a body are misplaced (which is fixable, using appropriate initializers of SKPhysicsBody class, but this is a very common error).
I just add
self.physicsWorld.contactDelegate = self
and it works now.

Ending a game when two nodes collide

I have two separate nodes with their own physics bodies, and when they collide, an SKScene with the high score and replay button should present itself. This is how my scene is called:
func didBeginContact(contact: SKPhysicsContact) {
gameOver()
print("gameOver")
}
And this is how my physics bodies for my nodes are set up:
func createDown(position: CGPoint) -> SKNode {
let circleNode = SKSpriteNode()
let circle = SKSpriteNode(imageNamed: "first#2x")
circleNode.position = CGPointMake(position.x, position.y)
circleNode.physicsBody = SKPhysicsBody(circleOfRadius: 30)
circleNode.physicsBody?.dynamic = false
circle.size = CGSize(width: 75, height: 75)
circleNode.addChild(circle)
circleNode.name = "circleNode"
circle.name = "CIRCLE"
let up = SKAction.moveByX(0, y: -9000, duration: 100)
physicsBody?.categoryBitMask = blackCategory
circleNode.runAction(up)
return circleNode
}
func playerPhysics() {
player.physicsBody = SKPhysicsBody(circleOfRadius: 30)
player.physicsBody?.affectedByGravity = false
player.physicsBody?.categoryBitMask = playerCategory
player.physicsBody?.contactTestBitMask = blackCategory
}
And here is my gameOver function:
func gameOver() {
gameEnd = true
let reveal = SKTransition.fadeWithDuration(1)
let scene = GameOver(size: self.scene!.size)
view!.presentScene(scene, transition: reveal)
}
Am I missing something? Will post more code if necessary.
Just use intersectsNode and call gameOver() when the two nodes collide.
if yourNode.intersectsNode(yourSecondNode) {
gameOver()
}
SKNode Class Reference: SKNode
You need to add the physics world as a contact delegate for your methods to work.
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMoveToView(view: SKView) {
physicsWorld.contactDelegate = self
And like Whirlwind said, you need to set your categoryBitMask and contactTestBitMask for the circle node.
Then everything should work. I hope I could help.