SpriteKit objects not colliding with each other - sprite-kit

I'm working on my first SpriteKit project and I'm having problems getting collisions to work. The objects just pass right through each other. I thought just setting the category, collision and contact bitmasks would be all that was required. I'm defining my two objects like so:
final class Paddle: SKShapeNode {
init(isLeft: Bool) {
super.init()
...
path = CGPath(rect: rect, transform: nil)
let body = SKPhysicsBody(rectangleOf: rect.size)
body.category(.paddle, collidesWith: .ball, isDynamic: false)
physicsBody = body
}
}
final class Ball: SKShapeNode {
override init() {
super.init()
...
let rect = CGRect(x: 0, y: 0, width: radius * 2, height: radius * 2)
path = CGPath(ellipseIn: rect, transform: nil)
let body = SKPhysicsBody(circleOfRadius: radius)
body.category(.ball, collidesWith: .paddle)
body.linearDamping = 0
body.angularDamping = 0
body.restitution = 1.0
physicsBody = body
}
}
and then I added a helper extension to setup the common physics body properties:
extension SKPhysicsBody {
func category(_ category: ShapeType, collidesWith: ShapeType, isDynamic: Bool = true) {
categoryBitMask = category.rawValue
collisionBitMask = collidesWith.rawValue
contactTestBitMask = collidesWith.rawValue
fieldBitMask = 0
allowsRotation = false
affectedByGravity = false
friction = 0
restitution = 0
self.isDynamic = isDynamic
}
}

Related

collision not detected between SKSpitekit nodes

I am building a maze and I have added some SKSpritekit nodes for the walls and a dot for the player. however when the dot and the walls collide, there is no detection of collision. my code is as follows:
import UIKit
import SpriteKit
import GameplayKit
import Foundation
import GameplayKit
class level1: SKScene, SKPhysicsContactDelegate {
var entities = [GKEntity]()
var graphs = [String : GKGraph]()
var dot = SKSpriteNode()
override func sceneDidLoad () {
buildMaze()
addDot()
func addDot() {
let startNum = x * (y - 1)
startCoord = coordArray[startNum]
dot = SKSpriteNode(imageNamed: "redDot")
dot.physicsBody?.isDynamic = true
dot.size = CGSize(width: 20, height: 20)
dot.position = startCoord
dot.physicsBody = SKPhysicsBody(circleOfRadius: 10)
dot.physicsBody?.mass = 0
dot.physicsBody?.usesPreciseCollisionDetection = true
self.addChild(dot)
}
func buildMaze() {
let difference = coordArray[1].x - coordArray[0].x
let wallDistance = difference/2
let thickness = CGFloat(3)
let length = difference - CGFloat(thickness)/2
var count = 0
for point in coordArray {
let northWall = SKSpriteNode(color: SKColor.black, size : CGSize (width: length, height: thickness))
northWall.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: length, height: thickness))
northWall.physicsBody?.mass = 200000
let southWall = SKSpriteNode(color: SKColor.black, size : CGSize (width: length, height: thickness))
southWall.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: length, height: thickness))
southWall.physicsBody?.mass = 200000
let eastWall = SKSpriteNode(color: SKColor.black, size : CGSize (width: thickness, height: length ))
eastWall.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: thickness, height: length))
eastWall.physicsBody?.mass = 200000
let westWall = SKSpriteNode(color: SKColor.black, size : CGSize (width: thickness, height: length ))
westWall.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: thickness, height: length))
westWall.physicsBody?.mass = 200000
if !instructions[count].contains("N") {
//print("added north wall")
northWall.position = CGPoint (x: point.x , y: point.y + wallDistance)
if nodes(at: northWall.position) == [] {
addChild(northWall)}
else {print("north wall already there")}
}
if !instructions[count].contains("S") {
//print("added south wall")
southWall.position = CGPoint (x: point.x , y: point.y - wallDistance)
if nodes(at: southWall.position) == [] {
addChild(southWall)}
else {//print("southwall already there")
}
}
if !instructions[count].contains("E") {
//print("added east wall")
eastWall.position = CGPoint (x: point.x + wallDistance , y: point.y)
if nodes(at: eastWall.position) == [] {
addChild(eastWall)}
else {//print("east already there")
}
}
if !instructions[count].contains("W") {
//print("added west wall")
westWall.position = CGPoint (x: point.x - wallDistance , y: point.y)
if nodes(at: westWall.position) == [] {
addChild(westWall)}
else {//print("west wall already there")
}
}
count = count + 1
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
let location = t.location(in: self)
dot.position.x = location.x
dot.position.y = location.y
}
}
func didBegin(_ contact: SKPhysicsContact) {
print("contact!")
}
The walls appear just as I wanted them, and the dot is also in the right position.
I added a masso of 20000 for each of them so that when I move the dot, the walls stay in place. however, when I move the dot with my finger, it just goest straight through the walls of the maze instead of being stopped by them.
I added a print statement to the didBegin function to see if at least it was detecting any contact between the sprites, but it does not.
Why is this?
cheers!
First in your didMoveTo or sceneDidLoad, you need to set the physicsContactDelegate:
override func sceneDidLoad () {
physicsWorld.contactDelegate = self
buildMaze()
addDot()
}
To set the contact/collision mask, you have to do it this way, because they're based on bitwise operation:
Let's suppose you want collision between dot and walls
struct PhysicsCategory {
static let wall: UInt32 = 0x1 << 1
static let dot: UInt32 = 0x1 << 2
}
You can put the struct above you class if you want
Then, when you assign physics body, you have to set the bitmask:
For dots:
dot.physicsBody?.categoryBitMask = PhysicsCategory.dot
dot.physicsBody?.contactTestBitMask = PhysicsCategory.wall
dot.physicsBody?.collisionBitMask = PhysicsCategory.wall
//Collision is different from contact, so if you want to avoid collision
//dot.physicsBody?.collisionBitMask = PhysicsCategory.dot
Collision is different from contact, check apple documentation about it
For walls:
northWall.physicsBody?.categoryBitMask = PhysicsCategory.wall
northWall.physicsBody?.contactTestBitMask = PhysicsCategory.dot
northWall.physicsBody?.collisionBitMask = PhysicsCategory.dot
//Do the same for all walls
If you want walls to contact or collide with more than one object:
northWall.physicsBody?.contactTestBitMask = PhysicsCategory.dot | PhysicsCategory.other
northWall.physicsBody?.collisionBitMask = PhysicsCategory.dot | PhysicsCategory.other
For walls are valid all consideration as per dots
Collision and contact detection don't work very well when you set the position of your sprites directly.
In your code, the lines:
dot.position.x = location.x
dot.position.y = location.y
are directly setting the position of dot, overriding anything that the physics engine wants to do with the objects.
Also, you don't appear to have set up any of the necessary categories or collision/contactTest bit masks.
You allow manual movement of the dot but with contact detection with the walls, then you'd probably need to see if the touch onthe screen was inside a wall and then not move the dot if that was the case. (which would mean that you are not using physics at all).
Edit: my step-by-step guide for collisions and contacts:
https://stackoverflow.com/a/51041474/1430420
And a guide to collision and contactTest bit masks:
https://stackoverflow.com/a/40596890/1430420

Collision detection inconsistent

My collision detection is working most of the time, but sometimes one physics body will go inside the other, when they should collide. I have a car with physics and road sides with physics. The car will blow through the road sides occasionally. I'm moving the car forward by position, however I have also tried moving forward by force and the same thing happens. Does anyone have any idea what would be causing this?
I've defined my physics categorys like so;
struct PhysicsCategory: OptionSet {
let rawValue: UInt32
init(rawValue: UInt32) { self.rawValue = rawValue }
static let CarCategory = PhysicsCategory(rawValue: 0b00001)
static let RoadSidesCategory = PhysicsCategory(rawValue: 0b00010)
static let NoCollisionsCategory = PhysicsCategory(rawValue: 0b00000)
}
Giving the car physics like so;
func giveCarPhysics(physics: Bool) {
//give physicsBody to the car and set to collide with the background
if physics {
self.size = CGSize(width: self.size.width, height: self.size.height)
self.physicsBody = SKPhysicsBody(rectangleOf: CGSize (width: self.size.width * 0.6, height: self.size.height * 0.9), center: CGPoint(x: self.anchorPoint.x, y: self.anchorPoint.y + self.size.height / 2))
self.physicsBody?.mass = vehicleMass
self.physicsBody?.usesPreciseCollisionDetection = true
self.physicsBody?.friction = 0.5
self.physicsBody?.restitution = 0.2
self.physicsBody?.affectedByGravity = false
self.physicsBody?.isDynamic = true
self.physicsBody?.linearDamping = airResistance
self.physicsBody?.categoryBitMask = PhysicsCategory.CarCategory.rawValue
self.physicsBody?.collisionBitMask = PhysicsCategory.RoadSidesCategory.rawValue
self.physicsBody?.contactTestBitMask = PhysicsCategory.RoadSidesCategory.rawValue
}
}
And then giving the road sides physics;
func createPhysicsRight() {
for (index, backgroundImageRight) in self.physicsRight.enumerated() {
let roadSpriteR = SKSpriteNode()
roadSpriteR.anchorPoint = CGPoint(x: 0.5, y: 0.5)
roadSpriteR.position = CGPoint(x: 0, y: CGFloat(self.physicsCounterR) * self.frame.size.height)
roadSpriteR.zPosition = 0
roadSpriteR.name = "RoadR \(index)"
print(roadSpriteR.name as Any)
roadSpriteR.physicsBody = SKPhysicsBody(polygonFrom: backgroundImageRight)
roadSpriteR.physicsBody?.mass = backgroundMass
roadSpriteR.physicsBody?.affectedByGravity = false
roadSpriteR.physicsBody?.isDynamic = false
roadSpriteR.physicsBody?.categoryBitMask = PhysicsCategory.RoadSidesCategory.rawValue
roadSpriteR.physicsBody?.collisionBitMask = PhysicsCategory.CarCategory.rawValue
roadSpriteR.physicsBody?.contactTestBitMask = PhysicsCategory.CarCategory.rawValue
roadSpriteRArray.append(roadSpriteR)
}
}
func createPhysicsLeft() {
for (index, backgroundImageLeft) in self.physicsLeft.enumerated() {
let roadSpriteL = SKSpriteNode()
roadSpriteL.anchorPoint = CGPoint(x: 0.5, y: 0.5)
roadSpriteL.position = CGPoint(x: 0, y: CGFloat(self.physicsCounterL) * self.frame.size.height)
roadSpriteL.zPosition = 0
roadSpriteL.name = "RoadL \(index)"
roadSpriteL.physicsBody = SKPhysicsBody(polygonFrom: backgroundImageLeft)
roadSpriteL.physicsBody?.mass = backgroundMass
roadSpriteL.physicsBody?.affectedByGravity = false
roadSpriteL.physicsBody?.isDynamic = false
roadSpriteL.physicsBody?.categoryBitMask = PhysicsCategory.RoadSidesCategory.rawValue
roadSpriteL.physicsBody?.collisionBitMask = PhysicsCategory.CarCategory.rawValue
roadSpriteL.physicsBody?.contactTestBitMask = PhysicsCategory.CarCategory.rawValue
roadSpriteLArray.append(roadSpriteL)
}
}

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.

Nodes in a Scene from different layers will not interact

Apologies in advance as this Question involves allot of code.
I have a GameScene with multiple layers holding Nodes/SpriteNodes for HUD/Scores/ etc. One layer, LayerProjectile, holds a Weapon Entity.
Said Entity has both Sprite and Physics Components.
Problem is the Projectile nodes in the LayerProjectile do not interact with nodes in the main layer (worldLayer), of the GameScene.
All physics bodies are dynamic, and the GameScene has its PhysicWorldDelgate
class GamePlayMode: SGScene, SKPhysicsContactDelegate {...
// Weapon Enity (SGEntity is a GKEntity)
class ThrowWeapon : SGEntity {
var spriteComponent: SpriteComponent!
var physicsComponent: PhysicsComponent
init(position: CGPoint, size: CGSize, texture:SKTexture){
super.init();
//Initilse Compnemnets
spriteComponent = SpriteComponent(entity: self, texture: texture, size: size, position: position);
addComponent(spriteComponent);
physicsComponent = PhysicsComponent(entity: self, bodySize: CGSize(width: spriteComponent.node.size.width * 0.8, height: spriteComponent.node.size.height * 0.8), bodyShape: .square, rotation: true);
physicsComponent.setCategoryBitmask(ColliderType.Projectile.rawValue, dynamic: true);
physicsComponent.setPhysicsCollisions(ColliderType.None.rawValue);
physicsComponent.setPhysicsContacts( ColliderType.Destroyable.rawValue | ColliderType.Enemy.rawValue);
physicsComponent.setEffectedByGravity(false);
addComponent(physicsComponent);
//Final Steps of compnements
spriteComponent.node.physicsBody = physicsComponent.physicsBody;
spriteComponent.node.name = "weapon";
name = "weapon";
spriteComponent.node.zPosition = 150;
spriteComponent.node.userData = ["startX" : position.x];
// Handle Contacts
override func contactWith(entity: SGEntity) {
//If contact with Enity in GemeScene
switch entity.name {
case "destructableEntity":
//Remove from Scsene self.parent?.parent?.runAction(SKAction.playSoundFileNamed("110548__noisehag__comp-cover-02.wav", waitForCompletion: false))
if let spriteComponentDestructable = entity.componentForClass(SpriteComponent.self){
let pos = spriteComponentDestructable.node.position;
spriteComponent.node.removeFromParent();
spriteComponentDestructable.node.parent?.runAction(SKAction.playSoundFileNamed("110548__noisehag__comp-cover-02.wav", waitForCompletion: false));
spriteComponentDestructable.node.removeAllActions();
spriteComponentDestructable.node.removeFromParent();
emitterExplosion(pos);
}
// self.runAction(SKAction.sequence([SKAction.fadeAlphaTo(0.0, duration: 0.5), SKAction.removeFromParent()]))
break;
case "enemyEntity":
if let spriteComponentEnemy = entity.componentForClass(SpriteComponent.self){
let pos = spriteComponentEnemy.node.position;
spriteComponent.node.removeFromParent();
spriteComponentEnemy.node.parent?.runAction(SKAction.playSoundFileNamed("110548__noisehag__comp-cover-02.wav", waitForCompletion: false));
spriteComponentEnemy.node.removeAllActions();
spriteComponentEnemy.node.removeFromParent();
emitterExplosion(pos);
} break;
default:
//
break;
}
LayerProjectile, which inherits from Layer- a SKNode, and which handles all weapon entities added to GameScene
class LayerProjectiles: Layer {
override func updateNodes(delta:CFTimeInterval,childNumber:Int,childNode: SKNode) {
print("Player movenment direction: \(playerMovingRighToLeft)");
if playerMovingRighToLeft {
// player moving to right so throw weapon same direction
childNode.position = childNode.position + CGPoint(x: delta * -weaponSpeed, y: 0.0);
childNode.zRotation = childNode.zRotation - (CGFloat(delta) * 10);
} else{
childNode.position = childNode.position + CGPoint(x: delta * weaponSpeed, y: 0.0)
childNode.zRotation = childNode.zRotation - (CGFloat(delta) * 10)
}
// When arrow within this layer moves out of the screen remove it, and the reverse
if childNode.position.x > ((childNode.userData!["startX"] as! CGFloat) + (SKMSceneSize!.width * 1.2)) || childNode.position.x < ((childNode.userData!["startX"]as! CGFloat) - (SKMSceneSize!.width * 1.2)){
childNode.removeFromParent()
}
}
Adding Instance of LayerProjectile and worldLayer (holding all other nodes, some of which have catBitMask set to notify when contact with Weapon Entity), to the GameScene (an instance of GamePlayMode, a SGScene)
class GameSceneState: GKState {
unowned let gs: GamePlayMode
init(scene: GamePlayMode) {
self.gs = scene
}
}
gs.physicsWorld.contactDelegate = gs
gs.physicsWorld.gravity = CGVector(dx: 0.0, dy: -1.0)
//Layers
gs.layerProjectile = LayerProjectiles();
gs.worldLayer = TileLayer(levelIndex: gs.levelIndex, typeIndex: .setMain)
gs.backgroundLayer = SKNode()
gs.overlayGUI = SKNode()
gs.addChild(gs.worldLayer)
gs.addChild(gs.layerProjectile);// For throwing wepaons, made in GamePlayMode
And finally the physics/sprite component settings for a Destrutable entity, which is a child of worldLayer (added to the GaemScene along with LayerProjectile)
class Destructable : SGEntity{
var spriteComponent: SpriteComponent!
var animationComponent: AnimationComponent!
var physicsComponent: PhysicsComponent!
var scrollerComponent: FullControlComponent!
init(position: CGPoint, size: CGSize, texture:SKTexture){
super.init();
//Initilse Compnemnets
spriteComponent = SpriteComponent(entity: self, texture: texture, size: size, position: position);
addComponent(spriteComponent);
physicsComponent = PhysicsComponent(entity: self, bodySize: CGSize(width: spriteComponent.node.size.width * 0.8, height: spriteComponent.node.size.height * 0.8), bodyShape: .square, rotation: false);
physicsComponent.setCategoryBitmask(ColliderType.Destroyable.rawValue , dynamic: true);
physicsComponent.setPhysicsCollisions(ColliderType.Wall.rawValue | ColliderType.Player.rawValue | ColliderType.Destroyable.rawValue); // collides with so bpounces off
physicsComponent.setPhysicsContacts(ColliderType.Wall.rawValue | ColliderType.Player.rawValue | ColliderType.Projectile.rawValue);
physicsComponent.setEffectedByGravity(true);
addComponent(physicsComponent);
//Final Steps of compnements
spriteComponent.node.physicsBody = physicsComponent.physicsBody;
spriteComponent.node.name = "destructableEntity";
name = "destructableEntity";
spriteComponent.node.zPosition = 150;
//spriteComponent.node.userData = ["startX" : position.x];
}
// Handle contacts
override func contactWith(entity: SGEntity) {
//
if entity.name == "weapon"{
print("Crate hit Projectile");
}
}
Instantionation of the Weapon Projectile (when user hits throw button during game play) and Destructible Entities when initializing GameScene and adding all sprite nodes:
let throwWeapon = ThrowWeapon(position: CGPoint(x: self.spriteComponent.node.position.x, y: self.spriteComponent.node.position.y + (self.spriteComponent.node.size.height / 2)), size: CGSize(width: 9, height: 40), texture: textureAtlas.textureNamed("kunai"));
playerEnt.gameScene.layerProjectile.addChild(throwWeapon.spriteComponent.node);
// Destructible Entity
gs.worldLayer.enumerateChildNodesWithName("placeholder_Destructable") { (node, stop) -> Void in
let crate = Destructable(position: node.position, size: CGSize(width: 32, height: 32), texture: tileAtlasInstance.textureNamed("Crate"));
//crate.name = "placeholder_Destructable";
//crate.spriteComponent.node.zPosition = GameSettings.GameParams.zValues.zWorldFront;
self.gs.addEntity(crate, toLayer: self.gs.worldLayer)
}
Any input appreciated.
Added: Category bitmask enum for ColliderTypes:
enum ColliderType:UInt32 {
case Player = 0
case Destroyable = 0b1
case Wall = 0b10
case Collectable = 0b100 // get points
case EndLevel = 0b1000
case Projectile = 0b10000
case None = 0b100000
case KillZone = 0b1000000
case arrow = 0b10000000;
case fire = 0b100000000; // die
case blueFire = 0b1000000000; // die
case greenCrystal = 0b10000000000; //more points like collectables
case barrel1 = 0b100000000000; //die
case barrell2 = 0b1000000000000; //die
case swordPoints = 0b10000000000000; //points
case spikes = 0b100000000000000; //die
case Enemy = 0b1000000000000000; // see Eneemy Entity
// against arrow and fire
}
// The SGEnity class where func handling physics contacts for Enities are called and overridden in Entity (Weapon, Destructible) classes
class SGEntity: GKEntity {
var name = ""
func contactWith(entity:SGEntity) {
//Overridden by subclass
}
// PhyscisComponent
enum PhysicsBodyShape {
case square
case squareOffset;// so the player knowns where the tile physics body is
case circle
case topOutline
case bottomOutline
}
class PhysicsComponent: GKComponent {
var physicsBody = SKPhysicsBody()
init(entity: GKEntity, bodySize: CGSize, bodyShape: PhysicsBodyShape, rotation: Bool) {
switch bodyShape {
case.square:
physicsBody = SKPhysicsBody(rectangleOfSize: bodySize)
break
case.squareOffset:
physicsBody = SKPhysicsBody(rectangleOfSize: bodySize, center: CGPoint(x: 0, y: bodySize.height/2 + 2))
break
case .circle:
physicsBody = SKPhysicsBody(circleOfRadius: bodySize.width / 2)
break
case .topOutline:
physicsBody = SKPhysicsBody(edgeFromPoint: CGPoint(x: (bodySize.width/2) * -1, y: bodySize.height/2), toPoint: CGPoint(x: bodySize.width/2, y: bodySize.height/2))
break
case .bottomOutline:
physicsBody = SKPhysicsBody(edgeFromPoint: CGPoint(x: (bodySize.width/2) * -1, y: (bodySize.height/2) * -1), toPoint: CGPoint(x: bodySize.width/2, y: (bodySize.height/2) * -1))
break
}
physicsBody.allowsRotation = rotation
//Defaults
physicsBody.dynamic = true
physicsBody.contactTestBitMask = ColliderType.None.rawValue
physicsBody.collisionBitMask = ColliderType.None.rawValue
}
func setCategoryBitmask(bitmask:UInt32, dynamic: Bool) {
physicsBody.categoryBitMask = bitmask
physicsBody.dynamic = dynamic
}
func setPhysicsCollisions(bitmask:UInt32) {
physicsBody.collisionBitMask = bitmask
}
func setPhysicsContacts(bitmask:UInt32) {
physicsBody.contactTestBitMask = bitmask
}
func setEffectedByGravity (gravity: Bool){
physicsBody.affectedByGravity = gravity;
}
}
So, contactsBegan is called via PhysicsDelage of the GameScene, :
if let bodyA = contact.bodyA.node as? EntityNode, //SpriteNode
let bodyAent = bodyA.entity as? SGEntity, // COnverted to SGEntity
let bodyB = contact.bodyB.node as? EntityNode, //SprtiteNode
let bodyBent = bodyB.entity as? SGEntity //ERROR HERE : Cast to SGEnity
{
print("SGENity Description: \(bodyBent)");
contactBegan(bodyAent, nodeB: bodyBent)
contactBegan(bodyBent, nodeB: bodyAent)
}else{
print("Cast error")
}
Issue appears to be casting the Projectile Entity to a SGEntity, this fails and contactsWith... of Projectile Entity is never called. All other SGEnities (Destructible) cast to SGEntity with no issues.,...

How to detect collision without physics

I want to detect when two bodies contact without using didBeginContact() function
I try with CGRectIntersectsRect() without any effect.
class GameScene: SKScene, SKPhysicsContactDelegate {
// Player
var _player = SKNode()
var platform = SKNode()
let heroCategory: UInt32 = 1 << 0
let platformCategory: UInt32 = 1 << 1
override func didMoveToView(view: SKView) {
/* Setup your scene here */
// Setup
self.anchorPoint = CGPointMake(0.5, 0.5)
self.physicsWorld.gravity = CGVectorMake(0.0, -2.0);
self.physicsWorld.contactDelegate = self
// Start populating
_player = creatPlayer()
self.addChild(_player)
platform = creatPlatform(1, atPosition: CGPoint(x: 0, y: -200))
self.addChild(platform)
let platformTwo = creatPlatform(2, atPosition: CGPoint(x: 0, y: 200))
self.addChild(platformTwo)
}
func creatPlatform(type: Int, atPosition: CGPoint) -> PlatformNode {
let node = PlatformNode()
node.platformType = type
node.position = atPosition
let sprite = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 200, height: 50))
node.addChild(sprite)
node.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
node.physicsBody?.dynamic = false;
node.physicsBody?.categoryBitMask = platformCategory
node.physicsBody?.contactTestBitMask = heroCategory
node.physicsBody?.collisionBitMask = 0;
return node
}
func creatPlayer() -> SKNode {
let playerNode = SKNode()
let sprite = SKSpriteNode(color: UIColor.whiteColor(), size: CGSize(width: 50, height: 50))
playerNode.addChild(sprite)
playerNode.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
playerNode.physicsBody?.dynamic = false
playerNode.physicsBody?.allowsRotation = false
playerNode.physicsBody?.restitution = 1.0
playerNode.physicsBody?.friction = 0.0
playerNode.physicsBody?.angularDamping = 0.0
playerNode.physicsBody?.linearDamping = 0.0
playerNode.physicsBody?.categoryBitMask = heroCategory
playerNode.physicsBody?.contactTestBitMask = platformCategory
playerNode.physicsBody?.collisionBitMask = 0;
return playerNode
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
_player.physicsBody?.dynamic = true
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if(CGRectIntersectsRect(platform.frame, _player.frame)){
println("Collision")
}
}
}
Maybe using CGRectIntersectsRect is a wrong approach, but I can be used instead?
The frame property
The frame property is a rectangle in the parent’s coordinate system
that contains the node’s content, ignoring the node’s children.
Also from docs:
The frame is non-empty if the node’s class draws content.
And because SKNode class does not perform any drawing of its own, and your platform is probably subclass of SKNode and player is SKNode the frame property is always (0,0,0,0).
You can access the real size of a frame in few ways:
You can make player and platform as subclasses of SKSpriteNode.
You can leave them as they are and access the child SKSpriteNode inside them.
You can leave as they are and use calcualteAccumulatedFrame() method.
About calcualteAccumulatedFrame() from docs:
A node’s accumulated frame, retrieved by calling the
calculateAccumulatedFrame method, is the largest rectangle that
includes the frame of the node and the frames of all its descendants.