My code detects collisions with a sprite but doesn't collide? - Swift - ios

Here is my didBeginContact:
func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
let ballWasContacted = firstBody.categoryBitMask == PhysicsCat.Ball || secondBody.categoryBitMask == PhysicsCat.Ball
let wallWasContacted = firstBody.categoryBitMask == PhysicsCat.Wall || secondBody.categoryBitMask == PhysicsCat.Wall
let wallCheckWasContacted = firstBody.categoryBitMask == PhysicsCat.wallSpace || secondBody.categoryBitMask == PhysicsCat.wallSpace
let scoreWasContacted = firstBody.categoryBitMask == PhysicsCat.Score || secondBody.categoryBitMask == PhysicsCat.Score
let boundaryWasContacted = firstBody.categoryBitMask == PhysicsCat.Boundaries || secondBody.categoryBitMask == PhysicsCat.Boundaries
if boundaryWasContacted {
print("I collided with the boundary.")
}
Here is my boundary sprite node:
let boundary = SKSpriteNode(color: SKColor.whiteColor(), size: CGSize(width: 50, height: 150))
boundary.name = "boundary"
boundary.position = CGPoint(x: self.frame.width / 1.37, y: self.frame.height / 2)
boundary.physicsBody = SKPhysicsBody(rectangleOfSize: boundary.size)
boundary.physicsBody!.categoryBitMask = PhysicsCat.Boundaries
boundary.physicsBody?.contactTestBitMask = PhysicsCat.Ball
boundary.physicsBody?.collisionBitMask = PhysicsCat.Ball
boundary.physicsBody!.dynamic = false
boundary.physicsBody!.friction = 0
boundary.physicsBody!.restitution = 1
boundary.zPosition = 5
self.addChild(boundary)
And finally here is my ball:
Ball = SKSpriteNode(imageNamed: "Ball")
Ball.size = CGSize(width: 38, height: 38)
Ball.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
Ball.physicsBody = SKPhysicsBody(circleOfRadius: Ball.frame.width / 2)
Ball.physicsBody?.categoryBitMask = PhysicsCat.Ball
Ball.physicsBody?.collisionBitMask = PhysicsCat.Wall
Ball.physicsBody?.contactTestBitMask = PhysicsCat.Wall | PhysicsCat.Score | PhysicsCat.wallSpace | PhysicsCat.Boundaries
Ball.physicsBody?.affectedByGravity = false
Ball.physicsBody?.dynamic = true
Ball.physicsBody!.allowsRotation = true
Ball.zRotation = CGFloat(M_PI)
self.addChild(Ball)
When I come in contact with the boundary, it prints "I collided with the boundary", but I go straight through it. What exactly am I missing from my code to get the actual collision to occur between the ball and boundary?

Alter you didBeginContact to do something on contact.
This question has been asked and the solution in this thread is by applying an Impluse in your didBeginContact.
Currently, you only have a print statement happening.
Try adding the relevant code like in this answer.
https://stackoverflow.com/a/29433778/6407353

Related

collision detection not working fully

import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let balls = [
SKSpriteNode(imageNamed: "blueball.png"),
SKSpriteNode(imageNamed: "greenball.png"),
SKSpriteNode(imageNamed: "realredball.png"),
]
let redRectangle = SKSpriteNode(imageNamed: "redrectangle.png")
let blueRectangle = SKSpriteNode(imageNamed: "bluerectangle.png")
let greenRectangle = SKSpriteNode(imageNamed: "greenrectangle.png")
let blueBallCategory :UInt32 = 0x1 << 0
let greenBallCategory :UInt32 = 0x1 << 1
let realRedBallCategory :UInt32 = 0x1 << 2
let redRectangleCategory : UInt32 = 0x1 << 3
let blueRectangleCategory : UInt32 = 0x1 << 4
let greenRectangleCategory : UInt32 = 0x1 << 5
override func didMove(to view: SKView) {
spawnBallsandRectangles()
physicsWorld.contactDelegate = self
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
physics()
}
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 == blueBallCategory && secondBody.categoryBitMask == blueRectangleCategory {
print("alive")
}
else if firstBody.categoryBitMask == greenBallCategory && secondBody.categoryBitMask == greenRectangleCategory {
print("alive")
}
else if firstBody.categoryBitMask == realRedBallCategory && secondBody.categoryBitMask == redRectangleCategory {
print("alive")
}
else if firstBody.categoryBitMask == blueBallCategory || firstBody.categoryBitMask == greenBallCategory && secondBody.categoryBitMask == redRectangleCategory{
print("dead")
}
else if firstBody.categoryBitMask == realRedBallCategory || firstBody.categoryBitMask == greenBallCategory && secondBody.categoryBitMask == blueRectangleCategory {
print("dead")
}
else if firstBody.categoryBitMask == realRedBallCategory || firstBody.categoryBitMask == blueBallCategory && secondBody.categoryBitMask == greenBallCategory {
print("dead")
}
}
func spawnBallsandRectangles() {
let ball = balls[Int(arc4random_uniform(UInt32(balls.count)))]
ball.position = CGPoint(x: -200, y: 250)
ball.size = CGSize(width: 70, height: 70)
redRectangle.position = CGPoint(x: 0, y: -600)
redRectangle.size = CGSize(width: 200, height: 20)
blueRectangle.position = CGPoint(x: -200, y: -600)
blueRectangle.size = CGSize(width: 200, height: 20)
greenRectangle.position = CGPoint(x: 200, y: -600)
greenRectangle.size = CGSize(width: 200, height: 20)
self.addChild(ball)
self.addChild(redRectangle)
self.addChild(blueRectangle)
self.addChild(greenRectangle)
}
func physics(){
for ball in balls{
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height/2)
}
redRectangle.physicsBody = SKPhysicsBody(rectangleOf: redRectangle.size)
redRectangle.physicsBody?.affectedByGravity = false
redRectangle.physicsBody?.isDynamic = false
blueRectangle.physicsBody = SKPhysicsBody(rectangleOf: redRectangle.size)
blueRectangle.physicsBody?.affectedByGravity = false
blueRectangle.physicsBody?.isDynamic = false
greenRectangle.physicsBody = SKPhysicsBody(rectangleOf: redRectangle.size)
greenRectangle.physicsBody?.isDynamic = false
greenRectangle.physicsBody?.affectedByGravity = false
balls[0].physicsBody?.categoryBitMask = blueBallCategory
balls[1].physicsBody?.categoryBitMask = greenBallCategory
balls[2].physicsBody?.categoryBitMask = realRedBallCategory
balls[0].physicsBody?.contactTestBitMask = redRectangleCategory
balls[1].physicsBody?.contactTestBitMask = redRectangleCategory
balls[1].physicsBody?.contactTestBitMask = blueRectangleCategory
balls[2].physicsBody?.contactTestBitMask = blueRectangleCategory
balls[0].physicsBody?.contactTestBitMask = greenRectangleCategory
balls[2].physicsBody?.contactTestBitMask = greenRectangleCategory
balls[0].physicsBody?.contactTestBitMask = blueRectangleCategory
balls[1].physicsBody?.contactTestBitMask = greenRectangleCategory
balls[2].physicsBody?.contactTestBitMask = redRectangleCategory
redRectangle.physicsBody?.categoryBitMask = redRectangleCategory
blueRectangle.physicsBody?.categoryBitMask = blueRectangleCategory
greenRectangle.physicsBody?.categoryBitMask = greenRectangleCategory
}
}
I have set up collision detection so that if a ball collides into a rectangle that match colors, the program prints ("alive"). And the program is supposed to print ("dead") if the colors don't match. It detects collision if the colors match and it prints ("alive") but it doesn't print ("dead"). Could someone please help ,thanks.
You might find that structuring your didBegin() like this might help to get rid of all those else ifs:
func didBegin(_ contact: SKPhysicsContact) {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contactMask {
case blueBallCategory | blueRectangleCategory:
print("Alive! Blue ball has hit blue rectangle.")
case greenBallCategory | greenRectangleCategory:
print("Alive! Green ball has hit green rectangle.")
case redBallCategory | redRectangleCategory:
print("Alive! Red ball has hit red rectangle.")
default :
print("Dead! Some other contact has occurred")
}
}
What do you want to happen if a ball hits a rectangle of a different colour?
EDIT:
From this code in your physics() function:
balls[0].physicsBody?.contactTestBitMask = redRectangleCategory
balls[1].physicsBody?.contactTestBitMask = redRectangleCategory
balls[1].physicsBody?.contactTestBitMask = blueRectangleCategory
balls[2].physicsBody?.contactTestBitMask = blueRectangleCategory
balls[0].physicsBody?.contactTestBitMask = greenRectangleCategory
balls[2].physicsBody?.contactTestBitMask = greenRectangleCategory
balls[0].physicsBody?.contactTestBitMask = blueRectangleCategory
balls[1].physicsBody?.contactTestBitMask = greenRectangleCategory
balls[2].physicsBody?.contactTestBitMask = redRectangleCategory
each time you assign a new value to the contactTestbitMask for a specific physics body, you are overwriting the previous value, not adding to it. Thus the only lines that matter are:
balls[0].physicsBody?.contactTestBitMask = blueRectangleCategory
balls[1].physicsBody?.contactTestBitMask = greenRectangleCategory
balls[2].physicsBody?.contactTestBitMask = redRectangleCategory
So balls[0] (blue ball) only contacts blue rectangles, balls[1] (green ball) only contacts green rectangles and balls[2] (real red ball) only contacts red rectangles. So didBegin() will not be called if a ball contacts a rectangle of a different colour, which is why you never see your 'dead' message.
Another Edit:
If you want all balls to trigger didBegin() when then contact any rectangle, then your contactTestBitMasks for the balls should include ALL the rectangles:
for ball in balls {
balls.physicsBody?.contactTestBitMask = blueRectangleCategory | greenRectangleCategory | redRectangleCategory
}
or if the balls already have some contacts defined and you just want to add the rectangles to their collision bit masks:
for ball in balls {
balls.physicsBody?.contactTestBitMask |= (blueRectangleCategory | greenRectangleCategory | redRectangleCategory)
}

Keeping an Object from moving beyond a certain point

Im am making a game in sprite kit that requires the user to move the player across the x axis to catch things. I am trying to make it impossible for the player to move off the screen because that looks bad. I have tried to do this with an if statement saying if it moves past a certain point teleport it back to the correct point but am not sure how to do this. That might be the right way to do this but i'm not sure. Please tell / show me what I should do to make this work. I will post the code below.
import SpriteKit
struct physicsCatagory {
static let person : UInt32 = 0x1 << 1
static let Ice : UInt32 = 0x1 << 2
static let IceTwo : UInt32 = 0x1 << 3
static let IceThree : UInt32 = 0x1 << 4
static let Score : UInt32 = 0x1 << 5
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var timeOfLastSpawn: CFTimeInterval = 0.0
var timePerSpawn: CFTimeInterval = 1.2
var scorenumber = Int()
var lifenumber = Int()
var SpeedNumber : Double = 0.5
var person = SKSpriteNode(imageNamed: "Person1")
let Score = SKSpriteNode()
var ScoreLable = SKLabelNode()
let BackGround = SKSpriteNode (imageNamed: "BackGround")
override func didMoveToView(view: SKView) {
self.scene?.backgroundColor = UIColor.blueColor()
physicsWorld.contactDelegate = self
self.scene?.size = CGSize(width: 640, height: 1136)
lifenumber = 0
SpeedNumber = 1
BackGround.size = CGSize(width: self.frame.width, height: self.frame.height)
BackGround.position = CGPointMake(self.size.width / 2, self.size.height / 2)
BackGround.zPosition = -5
self.addChild(BackGround)
Score.size = CGSize(width: 648, height: 1)
Score.position = CGPoint(x: 320, y: -90)
Score.physicsBody = SKPhysicsBody(rectangleOfSize: Score.size)
Score.physicsBody?.affectedByGravity = false
Score.physicsBody?.dynamic = false
Score.physicsBody?.categoryBitMask = physicsCatagory.Score
Score.physicsBody?.collisionBitMask = 0
Score.physicsBody?.contactTestBitMask = physicsCatagory.IceThree
Score.color = SKColor.blueColor()
self.addChild(Score)
person.zPosition = 1
person.position = CGPointMake(self.size.width/2, self.size.height/10)
person.setScale(0.32)
person.physicsBody = SKPhysicsBody (rectangleOfSize: CGSize(width: 40, height: 50))
person.physicsBody?.affectedByGravity = false
person.physicsBody?.categoryBitMask = physicsCatagory.person
person.physicsBody?.contactTestBitMask = physicsCatagory.Ice
person.physicsBody?.collisionBitMask = physicsCatagory.Ice
person.physicsBody?.dynamic = false
ScoreLable = SKLabelNode(fontNamed: "Zapfino")
ScoreLable.position = CGPoint(x: self.frame.width / 2, y: 1000)
ScoreLable.text = "\(scorenumber)"
ScoreLable.fontColor = UIColor.yellowColor()
ScoreLable.fontSize = 100
ScoreLable.fontName = "Zapfino "
self.addChild(ScoreLable)
self.addChild(person)
}
func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if firstBody.categoryBitMask == physicsCatagory.person && secondBody.categoryBitMask == physicsCatagory.IceThree || firstBody.categoryBitMask == physicsCatagory.IceThree && secondBody.categoryBitMask == physicsCatagory.person{
scorenumber++
if scorenumber == 20 {
timePerSpawn = 1.0
}
if scorenumber == 40{
timePerSpawn = 0.89
}
if scorenumber == 60{
timePerSpawn = 0.6
}
if scorenumber == 80{
timePerSpawn = 0.5
}
if scorenumber == 100{
timePerSpawn = 0.4
}
if scorenumber == 120{
timePerSpawn = 0.3
}
ScoreLable.text = "\(scorenumber)"
CollisionWithPerson(firstBody.node as! SKSpriteNode, Person: secondBody.node as! SKSpriteNode)
}
if firstBody.categoryBitMask == physicsCatagory.Score && secondBody.categoryBitMask == physicsCatagory.IceThree ||
firstBody.categoryBitMask == physicsCatagory.IceThree && secondBody.categoryBitMask == physicsCatagory.Score{
lifenumber++
if lifenumber == 1{
//person.texture
person.texture = SKTexture (imageNamed: "Flower#2")
}
if lifenumber == 2{
person.texture = SKTexture (imageNamed: "Flower #3")
}
if lifenumber == 3{
self.view?.presentScene(EndScene())
}
}
}
func CollisionWithPerson (Ice: SKSpriteNode, Person: SKSpriteNode){
Person.removeFromParent()
}
func spawnThirdIce(){
var Ice = SKSpriteNode(imageNamed: "Ice")
Ice.zPosition = 2
Ice.setScale(0.9)
Ice.physicsBody = SKPhysicsBody(rectangleOfSize: Ice.size)
Ice.physicsBody?.categoryBitMask = physicsCatagory.IceThree
Ice.physicsBody?.contactTestBitMask = physicsCatagory.person | physicsCatagory.Score
Ice.physicsBody?.affectedByGravity = false
Ice.physicsBody?.dynamic = true
let MinValue = self.size.width / 8
let MaxValue = self.size.width - 20
let SpawnPoint = UInt32(MaxValue - MinValue)
Ice.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.size.height)
self.addChild(Ice)
let action = SKAction.moveToY(-85, duration: 2.0)
let actionDone = SKAction.removeFromParent()
Ice.runAction(SKAction.sequence([action,actionDone]))
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let previousTouch = touch.previousLocationInNode(self)
let ammountDragged = location.x - previousTouch.x
person.position.x += ammountDragged
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if (currentTime - timeOfLastSpawn > timePerSpawn) {
spawnThirdIce()
self.timeOfLastSpawn = currentTime
}
}
}
You might do something like:
if node.position.x > scene.width {
node.position.x = scene.width
}
if node.position.x < 0 {
node.position.x = 0
}

SpriteKit + Swift - Not Firing on contact

So I am playing around with Swift and SpriteKit. I decided to create a simple game where a character will walk to a chest of coins and get all the treasure.
I am trying to detect when the two objects contact eachother, however nothing seems to be happening.
What I did:
1) I set the delegate:
SKPhysicsContactDelegate
2) Created the scene:
override func didMoveToView(view: SKView) {
backgroundColor = SKColor.whiteColor()
physicsWorld.contactDelegate = self
let sceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
sceneBody.friction = 0
background.zPosition = -20
background.anchorPoint = CGPointZero
background.position = CGPointZero
backgroundRepeated.zPosition = -20
backgroundRepeated.anchorPoint = CGPointZero
backgroundRepeated.position = CGPointMake(background.size.width-1,0)
addChest()
addCharacter()
addChild(background)
addChild(backgroundRepeated)
}
3) Created the items:
func addChest() {
chestOfCoins.anchorPoint = CGPointZero
chestOfCoins.size = CGSize(width: 200,height: 200)
chestOfCoins.position = CGPoint(x: self.frame.width-1, y: 300)
chestOfCoins.zPosition = 20
chestOfCoins.physicsBody = SKPhysicsBody(rectangleOfSize: chestOfCoins.size)
chestOfCoins.physicsBody?.affectedByGravity = false
chestOfCoins.physicsBody?.dynamic = false
chestOfCoins.physicsBody?.categoryBitMask = physicsCategory.chests
chestOfCoins.physicsBody?.collisionBitMask = 0
chestOfCoins.physicsBody?.contactTestBitMask = physicsCategory.character
addChild(chestOfCoins)
}
func addCharacter() {
character.anchorPoint = CGPointZero
character.position = CGPoint(x: 300, y: 300)
character.zPosition = 20
character.size = CGSize(width: 200, height: 200)
character.physicsBody = SKPhysicsBody(rectangleOfSize: character.size)
character.physicsBody?.affectedByGravity = false
character.physicsBody?.dynamic = false
character.physicsBody?.categoryBitMask = physicsCategory.character
character.physicsBody?.contactTestBitMask = physicsCategory.chests
character.physicsBody?.collisionBitMask = 0
addChild(character)
}
4) created didBeginContact:
func didBeginContact(contact: SKPhysicsContact) {
var contactBody1: SKPhysicsBody
var contactBody2: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
contactBody1 = contact.bodyA
contactBody2 = contact.bodyB
} else {
contactBody1 = contact.bodyB
contactBody2 = contact.bodyA
}
if((contactBody1.categoryBitMask == 1) && (contactBody2.categoryBitMask == 2)) {
print("touched")
} else {
print("not these two")
}
}
FYI, this IF if((contactBody1.categoryBitMask == 1) && (contactBody2.categoryBitMask == 2)) does not output Either the main if block or the else.
5) I created the category physics :
struct physicsCategory {
static let none: UInt32 = 0 //0
static let character: UInt32 = 0b1 //1
static let chests: UInt32 = 0b10 //2
static let snake: UInt32 = 0b100 //4
}
Full GameScene.Swift:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
struct physicsCategory {
static let none: UInt32 = 0 //0
static let character: UInt32 = 0b1 //1
static let chests: UInt32 = 0b10 //2
static let snake: UInt32 = 0b100 //4
static let all: UInt32 = UInt32.max
}
let background = SKSpriteNode(imageNamed: "Backgrounds")
let backgroundRepeated = SKSpriteNode(imageNamed: "Backgrounds")
let chestOfCoins = SKSpriteNode(imageNamed: "chest")
let character = SKSpriteNode(imageNamed: "character")
let gameActions = NSUserDefaults.standardUserDefaults()
func addChest() {
chestOfCoins.anchorPoint = CGPointZero
chestOfCoins.size = CGSize(width: 200,height: 200)
chestOfCoins.position = CGPoint(x: self.frame.width-1, y: 300)
chestOfCoins.zPosition = 20
chestOfCoins.physicsBody = SKPhysicsBody(rectangleOfSize: chestOfCoins.size)
chestOfCoins.physicsBody?.affectedByGravity = false
chestOfCoins.physicsBody?.dynamic = false
chestOfCoins.physicsBody?.categoryBitMask = physicsCategory.chests
chestOfCoins.physicsBody?.collisionBitMask = 0
chestOfCoins.physicsBody?.contactTestBitMask = physicsCategory.character
addChild(chestOfCoins)
}
func addCharacter() {
character.anchorPoint = CGPointZero
character.position = CGPoint(x: 300, y: 300)
character.zPosition = 20
character.size = CGSize(width: 200, height: 200)
character.physicsBody = SKPhysicsBody(rectangleOfSize: character.size)
character.physicsBody?.affectedByGravity = false
character.physicsBody?.dynamic = false
character.physicsBody?.categoryBitMask = physicsCategory.character
character.physicsBody?.contactTestBitMask = physicsCategory.chests
character.physicsBody?.collisionBitMask = 0
addChild(character)
}
override func didMoveToView(view: SKView) {
backgroundColor = SKColor.whiteColor()
physicsWorld.contactDelegate = self
let sceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
sceneBody.friction = 0
background.zPosition = -20
background.anchorPoint = CGPointZero
background.position = CGPointZero
backgroundRepeated.zPosition = -20
backgroundRepeated.anchorPoint = CGPointZero
backgroundRepeated.position = CGPointMake(background.size.width-1,0)
addChest()
addCharacter()
addChild(background)
addChild(backgroundRepeated)
}
func didBeginContact(contact: SKPhysicsContact) {
var contactBody1: SKPhysicsBody
var contactBody2: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
contactBody1 = contact.bodyA
contactBody2 = contact.bodyB
} else {
contactBody1 = contact.bodyB
contactBody2 = contact.bodyA
}
if((contactBody1.categoryBitMask == 1) && (contactBody2.categoryBitMask == 2)) {
print("touched")
} else {
print("not these two")
}
}
override func update(currentTime: NSTimeInterval) {
let startWalking = gameActions.boolForKey("Walking")
if (startWalking == true) {
chestOfCoins.position = CGPoint(x: chestOfCoins.position.x - 5, y: chestOfCoins.position.y)
background.position = CGPoint(x: background.position.x - 5, y: background.position.y)
backgroundRepeated.position = CGPoint(x: backgroundRepeated.position.x - 5, y: backgroundRepeated.position.y)
if (background.position.x < -background.size.width) {
background.position = CGPointMake(backgroundRepeated.position.x + backgroundRepeated.size.width, background.position.y)
}
if (backgroundRepeated.position.x < -backgroundRepeated.size.width) {
backgroundRepeated.position = CGPointMake(background.position.x + background.size.width, backgroundRepeated.position.y)
}
}
}
}
Question
Can anyone tell me why, when the screen starts scrolling and those two items come into contact nothing happens and tell me how to fix it?
One also seems to go behind even though I set the zPosition of both to the same
Here is a pic, Don't judge about images :(
set either your chest or your other thingies property to dynamic = true
two non-dynamic physics bodies wont register contacts

SpriteKit SKShapeNode stops prematurely when colliding due to gravity

This is the setup
// Create a scene
scene = SKScene(size: self.view.frame.size)
let sceneHeight = scene.frame.height
let sceneWidth = scene.frame.width
// Create nodes
// Rectangle
let shapeWidth: CGFloat = 100
let shapeHeight = shapeWidth
let shapeNode = SKShapeNode(rect: CGRectMake(0, 0, shapeWidth, 100))
shapeNode.position = CGPoint(x: sceneWidth/2 - shapeWidth/2, y: 300)
shapeNode.fillColor = UIColor.redColor()
shapeNode.strokeColor = UIColor.redColor()
// Floor
let floorWidth: CGFloat = sceneWidth
let floorHeight: CGFloat = 10
let floorNode = SKShapeNode(rect: CGRectMake(0, 0, floorWidth, floorHeight))
floorNode.position = CGPoint(x: 0, y: 100)
floorNode.fillColor = UIColor.greenColor()
floorNode.strokeColor = UIColor.greenColor()
// Create the node tree
scene.addChild(floorNode)
scene.addChild(shapeNode)
Then I define and add two physics bodies to my two nodes.
// Create physics
let shapePhysicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: shapeWidth, height: shapeHeight))
shapePhysicsBody.dynamic = true
shapePhysicsBody.categoryBitMask = PhysicsCategory.Shape
shapePhysicsBody.contactTestBitMask = PhysicsCategory.Floor
shapePhysicsBody.collisionBitMask = PhysicsCategory.None
shapePhysicsBody.usesPreciseCollisionDetection = true
let floorPhysicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: floorWidth, height: floorHeight))
floorPhysicsBody.dynamic = true
floorPhysicsBody.categoryBitMask = PhysicsCategory.Floor
floorPhysicsBody.contactTestBitMask = PhysicsCategory.Shape
floorPhysicsBody.collisionBitMask = PhysicsCategory.None
floorPhysicsBody.usesPreciseCollisionDetection = true
floorPhysicsBody.affectedByGravity = false
// Add physics
shapeNode.physicsBody = shapePhysicsBody
floorNode.physicsBody = floorPhysicsBody
scene.physicsWorld.gravity = CGVector(dx: 0, dy: -1)
scene.physicsWorld.contactDelegate = self
This sort of works, that is the rectangle shape does fall from it's initial position and the collision delegate is called when a collision happens.
The contents of my didBeginContact: method.
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 & PhysicsCategory.Floor != 0) && (secondBody.categoryBitMask & PhysicsCategory.Shape != 0)) {
secondBody.dynamic = false
println("collision")
}
}
But the shape node stops way before there's a visual contact. I hope these images give an impression of how that looks.
Starts here
This is where the contact happens
Why is there such a gap between them? What am I missing? I want them to visually touch each other before the rectangle stops moving.
SKShapeNode(rect:) draws the SKShapeNode with the node.position as origin. For the SKPhysicsBody position corresponds to the center of the rectangle. This creates an offset between the physics body and the actual node. You can see that if you enable
scene.view?.showsPhysics = true.
You can correct the offset by creating the SKShapeNode using following code.
let shapeNode = SKShapeNode(rect: CGRectMake(-shapeWidth/2, -50, shapeWidth, 100))
// Other code
let floorNode = SKShapeNode(rect: CGRectMake(-floorWidth/2, -floorHeight/2, floorWidth, floorHeight))

spritekit didBeginContact three object not worked

I am tring to [didBeginContact] by three nodes. I wrote under code.
But not work correctly. when white rectangle fole without hitting black or white rectangle,
println("black"), and println("blue") work...
whien white rectangle hit black rectangle, println("blue") work...
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let blackCategory: UInt32 = 0x1 << 0
let whiteCategory: UInt32 = 0x1 << 1
let blueCategory: UInt32 = 0x1 << 2
override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
self.size = view.bounds.size
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsWorld.gravity = CGVectorMake(0.0, -3.0)
let blackSquare = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(50, 50))
blackSquare.position = CGPoint(
x: CGRectGetMidX(self.frame),
y: CGRectGetMidY(self.frame)
)
blackSquare.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(50, 50))
blackSquare.physicsBody?.affectedByGravity = false
blackSquare.physicsBody?.dynamic = false
blackSquare.physicsBody?.categoryBitMask = blackCategory
blackSquare.physicsBody?.contactTestBitMask = whiteCategory
let blueSquare = SKSpriteNode(color: UIColor.blueColor(), size: CGSizeMake(50, 50))
blueSquare.position = CGPoint(
x: CGRectGetMidX(self.frame) - 100,
y: CGRectGetMidY(self.frame) - 100
)
blueSquare.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(50, 50))
blueSquare.physicsBody?.affectedByGravity = false
blueSquare.physicsBody?.dynamic = false
blackSquare.physicsBody?.categoryBitMask = blueCategory
blackSquare.physicsBody?.contactTestBitMask = whiteCategory
self.addChild(blackSquare)
self.addChild(blueSquare)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch in touches {
let location = touch.locationInNode(self)
let whiteRectangle = SKSpriteNode(color: UIColor.whiteColor(), size: CGSizeMake(50, 50))
whiteRectangle.position = location
whiteRectangle.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(50, 50))
whiteRectangle.physicsBody?.categoryBitMask = whiteCategory
whiteRectangle.physicsBody?.contactTestBitMask = blackCategory
whiteRectangle.physicsBody?.contactTestBitMask = blueCategory
self.addChild(whiteRectangle)
}
}
override func update(currentTime: CFTimeInterval) {
}
func didBeginContact(contact: SKPhysicsContact!) {
var firstBody, secondBody, thirdBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
thirdBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
thirdBody = contact.bodyA
}
if firstBody.categoryBitMask & whiteCategory != 0 &&
secondBody.categoryBitMask & blackCategory != 0 {
//secondBody.node?.removeFromParent()
println("black")
}
if firstBody.categoryBitMask & whiteCategory != 0 &&
thirdBody.categoryBitMask & blueCategory != 0 {
//secondBody.node?.removeFromParent()
println("blue")
}
}
}
1) Based on your contact handler, you should set the categories to be
let whiteCategory: UInt32 = 0x1 << 0
let blackCategory: UInt32 = 0x1 << 1
let blueCategory: UInt32 = 0x1 << 2
2) You are incorrectly setting the blackSquare's physicsBody's bit masks twice
blackSquare.physicsBody?.categoryBitMask = blueCategory
blackSquare.physicsBody?.contactTestBitMask = whiteCategory
The above should be
blueSquare.physicsBody?.categoryBitMask = blueCategory
blueSquare.physicsBody?.contactTestBitMask = whiteCategory
3) The third contact body is not needed here.
func didBeginContact(contact: SKPhysicsContact!) {
var firstBody, 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 & whiteCategory) != 0 &&
(secondBody.categoryBitMask & blackCategory != 0)) {
//secondBody.node?.removeFromParent()
println("black")
}
if ((firstBody.categoryBitMask & whiteCategory != 0) &&
(secondBody.categoryBitMask & blueCategory != 0)) {
//secondBody.node?.removeFromParent()
println("blue")
}
}
4) The following is not needed (in touchesBegan). Setting the contact bit masks for the blue and black nodes is sufficient.
whiteRectangle.physicsBody?.contactTestBitMask = blackCategory
whiteRectangle.physicsBody?.contactTestBitMask = blueCategory

Resources