SKPhysicsContact Not Detecting categoryBitMask Collision - swift

So I have my "Floor.swift" class below which is basically a bunch of walls. I have objects coming from the top of the screen and once the Floor and SKSpriteNodes collide, I'd like the SKSpriteNode to be removed. Below is my Floor class.
import Foundation
import SpriteKit
class Floor: SKNode {
override init() {
super.init()
let leftWall = SKSpriteNode(color: UIColor.brown, size: CGSize(width: 5, height: 50))
leftWall.position = CGPoint(x: 0, y: 50)
leftWall.physicsBody = SKPhysicsBody(rectangleOf: leftWall.size)
leftWall.physicsBody!.isDynamic = false
self.addChild(leftWall)
let rightWall = SKSpriteNode(color: UIColor.brown, size: CGSize(width: 5, height: 50))
rightWall.position = CGPoint(x: 375, y: 50)
rightWall.physicsBody = SKPhysicsBody(rectangleOf: rightWall.size)
rightWall.physicsBody!.isDynamic = false
self.addChild(rightWall)
let bottomWall = SKSpriteNode(color: UIColor.brown, size: CGSize(width: 500, height: 10))
bottomWall.position = CGPoint(x: 150, y: -5)
bottomWall.physicsBody = SKPhysicsBody(rectangleOf: bottomWall.size)
bottomWall.physicsBody!.isDynamic = false
self.addChild(bottomWall)
// Set the bit mask properties
self.physicsBody?.categoryBitMask = floorCategory
self.physicsBody?.contactTestBitMask = objectCategory | pointCategory | lifeCategory
self.physicsBody?.collisionBitMask = dodgeCategory
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemted")
}
}
Then in my GameScene class under "func didBegin(_ contact: SKPhysicsContact)" I wrote:
if (contact.bodyA.categoryBitMask == floorCategory) && (contact.bodyB.contactTestBitMask == objectCategory | pointCategory | lifeCategory) {
contact.bodyB.node!.removeFromParent()
print("COLLISION")
}
But for some reason I'm not getting any detection what's so ever. I made my "Floor" class be a "contactTestBitMask" on each class of my objectCategory, pointCategory, lifeCategory. What am I doing wrong!? My other collisions are detected but not this one.

Your leftWall, rightWall and bottomWall have the default contactTest and collision bit masks, so will collide with everything and generate contacts with nothing. Except they all have their isDynamic property set to false, so they can’t collide with or contact anything (although other things can collide and contact them if those other things have isDynamic set to true).
But this is probably what you want (Floors and walls are usually not dynamic). I suspect the real problem is that objects of Floor class won’t have a physics body, unless you initialise it somewhere else. You have these lines:
self.physicsBody?.categoryBitMask = ...
self.physicsBody?.contactTestBitMask = ...
self.physicsBody?.collisionBitMask = ...
but you haven't actually created self.physicsBody. Your code would crash except you’ve used optional chaining (self,phyicsBody?) and so Swift gets to the ‘?’ and says “Oh - it’s optional and doesn’t exist. I’ll just stop here then”
At the very least, try creating a physics body for your Floor object using the physics bodies of the 3 wall objects:
Self.physicsBody = SKPhysicsBody(Bodies: [leftWall.physicsBody, rightWall.PhysicsBoidy, bottomWall.physicsBody])
(SKPhysicsBody has an initialiser that takes an array of physics bodies to create a new physics body).

Related

How to change the fillColor of a SKShapeNode within the didBegin contact function?

My goal is to change the fillColor of a SKShapeNode as soon as that node collides with another Node. I do know how to edit the physics body at the point of collision but I couldn't manage to figure out how to change properties like fill- or strokeColor of a Node.
The SKShapeNode:
func addBrick() -> SKShapeNode {
let brick = SKShapeNode(rect: CGRect(x: -100, y: -20, width: 200, height: 40), cornerRadius: 20)
brick.fillColor = .blue
brick.strokeColor = .blue
brick.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 200, height: 40))
brick.position = CGPoint(x: 0, y: -50)
brick.zPosition = 2
brick.physicsBody?.categoryBitMask = BrickCategory
brick.physicsBody?.collisionBitMask = PlayerCategory
brick.physicsBody?.contactTestBitMask = PlayerCategory
return brick
}
Then I test the contact between the player and the brick:
func didBegin(_ contact: SKPhysicsContact) {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contactMask {
case PlayerCategory | BrickCategory:
print("")
default:
print("Unknown collision")
}
}
I do know that I can make changes to the physics body itself by using
contact.bodyB.node?.//make changes here
, but I don't know how to change the fillColor of bodyB for example to red.
I appreciate your help!
If you have an SKNode node that you know should be an SKShapeNode, then you can cast it like:
if let shapeNode = node as? SKShapeNode {
shapeNode.fillColor = .red
}

SpriteKit node going through wall

I'm working on an Xcode Playground and I'm having some problems with SpriteKit collisions.
The first box has a mass of 1 and the second box has a mass of 100000 (when the latter is 100, for example, everything works fine). The collisions are elastic (restitution was set to 1) and there is no friction or damping at all.
Here is the code for my scene (ColliderType is just an enum for the categories):
class GameScene: SKScene {
private var wall: SKSpriteNode!
private var floor: SKSpriteNode!
private var box1: SKSpriteNode!
private var box2: SKSpriteNode!
override func didMove(to view: SKView) {
self.backgroundColor = .white
wall = SKSpriteNode(color: SKColor.black, size: CGSize(width: 10, height: self.frame.height))
wall.position = CGPoint(x: 100, y: self.frame.height/2)
wall.physicsBody = SKPhysicsBody(rectangleOf: wall.frame.size)
wall.physicsBody?.isDynamic = false
floor = SKSpriteNode(color: SKColor.black, size: CGSize(width: self.frame.width, height: 10))
floor.position = CGPoint(x: self.frame.width/2, y: 100)
floor.physicsBody = SKPhysicsBody(rectangleOf: floor.frame.size)
floor.physicsBody?.isDynamic = false
box1 = SKSpriteNode(color: SKColor.black, size: CGSize(width: 100, height: 100))
box1.position = CGPoint(x: 300, y: floor.position.y+box1.size.height/2)
box1.physicsBody = SKPhysicsBody(circleOfRadius: box1.frame.size.width/2)
box2 = SKSpriteNode(color: SKColor.black, size: CGSize(width: 100, height: 100))
box2.position = CGPoint(x: 750, y: floor.position.y+box2.size.height/2)
box2.physicsBody = SKPhysicsBody(circleOfRadius: box2.frame.size.width/2)
self.addChild(wall)
self.addChild(floor)
self.addChild(box1)
self.addChild(box2)
box1.physicsBody?.allowsRotation = false
box2.physicsBody?.allowsRotation = false
box1.physicsBody?.restitution = 1
box2.physicsBody?.restitution = 1
box1.physicsBody?.mass = 1
box2.physicsBody?.mass = 100000
box1.physicsBody?.friction = 0
box2.physicsBody?.friction = 0
box1.physicsBody?.linearDamping = 0
box2.physicsBody?.linearDamping = 0
wall.physicsBody?.categoryBitMask = ColliderType.Wall.rawValue
box1.physicsBody?.categoryBitMask = ColliderType.Box1.rawValue
box2.physicsBody?.categoryBitMask = ColliderType.Box2.rawValue
box1.physicsBody?.collisionBitMask = ColliderType.Wall.rawValue | ColliderType.Floor.rawValue | ColliderType.Box2.rawValue
box2.physicsBody?.collisionBitMask = ColliderType.Floor.rawValue | ColliderType.Box1.rawValue
box1.physicsBody?.contactTestBitMask = ColliderType.Box2.rawValue | ColliderType.Wall.rawValue
box1.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
box2.physicsBody?.velocity = CGVector(dx: -50, dy: 0)
}
}
Things I've tried:
Set usesPreciseCollisionDetection = true on the first box
Update the positions and check for collisions by myself by overriding the update method (it wasn't as optimised as SpriteKit's engine so it was very slow)
Make the second box slower and set physicsWorld.speed to a number higher than 1
I think in general you're probably expecting too much. SpriteKit is a game engine, designed to give qualitatively reasonable behavior for scenarios involving normal-ish sorts of physics. It's not a high-precision physics simulation, and when you push it into a region where it's got to do something extreme, it's probably going to fail.
In this case, you're expecting it to simulate a huge number of perfectly elastic collisions within a small amount of time and distance (many collisions per animation frame of the simulation as the heavy block mashes the light one into a tiny gap). It's going to find the first collision in the frame, figure the impulses to apply to the blocks, and then advance to the next frame, with the effect that the big block happily mashes into the space where the small one resides. All the subsequent collisions that would prevent that are being missed in search of 60 fps.

Create Wall / Barrier Using SKSpriteNode physicsBody

I have a separate "Floor" class in my SKSpriteKit application. When I first created this class, I had a barrier around the whole frame using
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
but that was actually stopping SKSpriteNodes from entering from the top of the screen so now I'm trying to figure out how I can create a left, right and bottom of the frame wall but not the top. This is so I can avoid one of my SKSpriteNodes at the bottom of the screen (still viewable by users) that moves left and right from leaving the display. I'm trying below which is building as if it's going to work but then when I start up the game I don't see the the walls? I'm not too sure what I'm doing wrong, I have
skView.showsPhysics = true
import Foundation
import SpriteKit
class Floor: SKNode {
override init() {
super.init()
let leftWall = SKSpriteNode(color: UIColor.brown, size: CGSize(width: 100, height: frame.height))
leftWall.position = CGPoint(x: 100, y: 100)
leftWall.physicsBody = SKPhysicsBody(rectangleOf: leftWall.size)
leftWall.physicsBody!.isDynamic = false
self.addChild(leftWall)
let rightWall = SKSpriteNode(color: UIColor.brown, size: CGSize(width: 100, height: frame.height))
rightWall.position = CGPoint(x: 200, y: 200)
rightWall.physicsBody = SKPhysicsBody(rectangleOf: rightWall.size)
rightWall.physicsBody!.isDynamic = false
self.addChild(rightWall)
// Set the bit mask properties
self.physicsBody?.categoryBitMask = balloonCategory
self.physicsBody?.contactTestBitMask = nailDropCategory
//self.physicsBody?.collisionBitMask = balloonCategory
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemted")
}
}
Gravity is affecting your walls, so they are forever falling into oblivion.
When you created your edge loop, you did not need to worry about this because edge loops are always static because it has no volumne
Now you are using volume based bodies, so you need to account for things like gravity and other forces affecting your bodies.
To have your body ignore these forces, simply set isDynamic = false
let leftWall = SKSpriteNode(color: UIColor.brown, size: CGSize(width: 100, height: frame.height))
leftWall.position = CGPoint(x: 100, y: 100)
leftWall.physicsBody = SKPhysicsBody(rectangleOf: leftWall.size)
leftWall.physicsBody!.isDynamic = false
self.addChild(leftWall)
let rightWall = SKSpriteNode(color: UIColor.brown, size: CGSize(width: 100, height: frame.height))
rightWall.position = CGPoint(x: 200, y: 200)
rightWall.physicsBody = SKPhysicsBody(rectangleOf: rightWall.size)
rightWall.physicsBody!.isDynamic = false
self.addChild(rightWall)

Swift 3 Physicsbody Collisions doesn't work

This is my class for the Player
My Player doesn't collide with the Gamefield they just go through each other.
I dont want them to be on top of each other.
I tried to google the solution but for me it seems that I did mostly all right.
Please Help:
class Player: SKShapeNode {
static public let length: CGFloat = 50
static public let rect = CGRect(x: -length/2, y: -length/2, width: length, height: length)
var life = 100
override init() {
super.init()
self.fillColor = SKColor.blue
self.strokeColor = SKColor.black
self.position = CGPoint(x: 50, y: 50)
self.physicsBody = SKPhysicsBody(edgeLoopFrom: Player.rect)
self.physicsBody?.isDynamic = true
self.physicsBody?.allowsRotation = false
self.physicsBody?.categoryBitMask = PhysicsCategory.Robot
self.physicsBody?.contactTestBitMask = PhysicsCategory.Projectile
self.physicsBody?.collisionBitMask = PhysicsCategory.Gamefield
self.physicsBody?.usesPreciseCollisionDetection = true
}; required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}
func moveBy(vect: CGVector) {
self.position.x += vect.dx
self.position.y += vect.dy
//print(vect.dx, vect.dy)
}
}
This is my Gamescene Class
struct PhysicsCategory {
static let None : UInt32 = UInt32.min
static let All : UInt32 = UInt32.max
static let Robot : UInt32 = 0b0001 // 1
static let Gamefield : UInt32 = 0b0010 // 2
static let Monster : UInt32 = 0b0011 // 3
static let Projectile : UInt32 = 0b0100 // 4
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var player = Player(rect: Player.rect)
var controlL: Control
var controlR: Control
var cam = SKCameraNode()
override init(size: CGSize) {
controlL = Control(posX: 0, size: size, direction: 1)
controlR = Control(posX: size.width, size: size, direction: -1)
super.init(size: size)
}; required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}
override func didMove(to view: SKView) {
self.backgroundColor = SKColor.white
physicsWorld.gravity = CGVector.zero
physicsWorld.contactDelegate = self
cam.xScale = 1
cam.yScale = 1
self.camera = cam
player.addChild(cam)
let gameFieldRect = CGRect(x: 0, y: 0, width: 1000, height: 600)
let gameField = SKShapeNode(rect: gameFieldRect)
gameField.fillColor = SKColor.clear
gameField.strokeColor = SKColor.black
gameField.position = CGPoint(x: 0, y: 0)
gameField.physicsBody = SKPhysicsBody(edgeLoopFrom: gameFieldRect)
gameField.physicsBody?.isDynamic = true
gameField.physicsBody?.allowsRotation = false
gameField.physicsBody?.categoryBitMask = PhysicsCategory.Gamefield
gameField.physicsBody?.contactTestBitMask = PhysicsCategory.None
gameField.physicsBody?.collisionBitMask = PhysicsCategory.Robot
self.addChild(gameField)
self.addChild(player)
cam.addChild(controlL.add())
cam.addChild(controlR.add())
I hope someone sees the mistake. It took me quite long allready.
You need to change your robot to a different physics body type (rectangleOf)?:
SpriteKit supports two kinds of physics bodies, volume-based bodies and edge-based bodies. When you create a physics body, its kind, size, and shape are determined by the constructor method you call. An edge-based body does not have mass or volume and is unaffected by forces or impulses in the system. Edge-based bodies are used to represent volume-less boundaries or hollow spaces in your physics simulation. In contrast, volume-based bodies are used to represent objects with mass and volume.
Also, are you using impulses or forces to move your robot? Not .move(to:) or .position =? move and position will break your physics world (it will go through it in some scenarios)
Also, it looks like you need to change your category masks to a proper series (for didBegin(contact:))
it should be, 1, 2 , 4, 8, 16, so on... that is how you get unique contact hits.. Right now you could get a hit of "4" from 0+4 or 1+3... thus, not unique collisions.
Your collisionBitMask looks good though.. They should be bumping into each other.

Child node not keeping same physicsBody when added to parent

Using the Xcode SpriteKit Scene editor I have a few objects added to the scene as "obstacles." I like to animate them across the screen. They are not dynamic. To make things easier I thought to add them to an obstacleParent node and just animate that one object. The problem is that adding the obstacles to a parent totally screws up their physics bodies. Meaning the obstacle physics bodies do not match the outline of the sprite.
I'm not sure if it is a bug in the Scene editor, but adding the physics bodies programmatically yields results as expected.
Some interesting things worth noting are:
the physicsBodies moved with the parent regardless of if they were not Dynamic or not.
I could not get them to overlap each other until I made them all have the same contactTestBitMask
It is difficult to tell in the screen shot but all 3 children kept their physics shapes
If the parent has a physics body and an physics impulse is applied to it, only the parent moves. However setting the position of the parent programmatically moves the parent and child nodes
testObject = SKSpriteNode(texture: nil, color: .greenColor(), size: CGSize(width: 100, height: 100))
testObject.zPosition = Layer.Controls.rawValue
testObject.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
// testObject.physicsBody = SKPhysicsBody(circleOfRadius: testObject.width / 2)
// testObject.physicsBody!.dynamic = true
// testObject.physicsBody!.affectedByGravity = false
// testObject.physicsBody!.allowsRotation = true
addChild(testObject)
let testerObject = SKSpriteNode(texture: nil, color: .clearColor(), size: CGSize(width: 100, height: 100))
testerObject.zPosition = Layer.Controls.rawValue
testerObject.position = CGPoint(x:50, y: 50)
testerObject.physicsBody = SKPhysicsBody(circleOfRadius: testerObject.width / 2)
testerObject.physicsBody!.affectedByGravity = false
testerObject.physicsBody!.dynamic = true
testerObject.physicsBody!.allowsRotation = true
testerObject.physicsBody!.collisionBitMask = 0
testerObject.physicsBody!.contactTestBitMask = 0
testObject.addChild(testerObject)
let testerObject2 = SKSpriteNode(texture: nil, color: .clearColor(), size: CGSize(width: 100, height: 100))
testerObject2.zPosition = Layer.Controls.rawValue
testerObject2.position = CGPoint(x:-50, y: -50)
testerObject2.physicsBody = SKPhysicsBody(circleOfRadius: testerObject.width / 2)
testerObject2.physicsBody!.affectedByGravity = false
testerObject2.physicsBody!.dynamic = true
testerObject2.physicsBody!.allowsRotation = true
testerObject2.physicsBody!.collisionBitMask = 0
testerObject2.physicsBody!.contactTestBitMask = 0
testObject.addChild(testerObject2)
let testerObject3 = SKSpriteNode(texture: nil, color: .clearColor(), size: CGSize(width: 100, height: 100))
testerObject3.zPosition = Layer.Controls.rawValue
testerObject3.physicsBody = SKPhysicsBody(circleOfRadius: testerObject.width / 2)
testerObject3.physicsBody!.affectedByGravity = false
testerObject3.physicsBody!.dynamic = true
testerObject3.physicsBody!.allowsRotation = true
testerObject3.physicsBody!.collisionBitMask = 0
testerObject3.physicsBody!.contactTestBitMask = 0
testObject.addChild(testerObject3)