Swift: Contact detection between 2 nodes? - swift

Alright, so I've been following various other SO links and trying to figure this out- I need to have contact detection between 2 nodes. Not collision detection, which I learned results in the nodes bouncing each other around. I don't want them to knock each other around, I just want to know when they touch.
Right now I have physics bodies for the 2 nodes, savior and chicken1 as well as the ground (ground) upon which savior sits. These are all set up here:
savior.physicsBody?.dynamic = true
savior.physicsBody?.allowsRotation = false
savior.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(savior.size.width, savior.size.height))
chicken1.physicsBody?.dynamic = true
chicken1.physicsBody?.allowsRotation = false
chicken1.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(chicken1.size.width, chicken1.size.height))
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundTexture.size().height*2))
ground.physicsBody?.dynamic = false
I need to set up contact detection between savior and chicken1. There seem to be various ways to do this, but this is what I put together:
//Contact detection
self.physicsWorld.contactDelegate = self
savior.physicsBody?.categoryBitMask = saviorCategory
savior.physicsBody?.contactTestBitMask = animalCategory
savior.physicsBody?.collisionBitMask = 0
chicken1.physicsBody?.categoryBitMask = animalCategory
chicken1.physicsBody?.contactTestBitMask = saviorCategory
chicken1.physicsBody?.collisionBitMask = 0
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 == 0 && secondBody.categoryBitMask == 1 {
println("they made contact")
}
}
This code results in savior falling right through ground and going right through chicken1, with no contact detection because even when savior and chicken1 touch, nothing happens.
I need savior and ground to continue to collide, but I don't want savior and chicken1 to collide, just touch.
The program needs to execute something when they touch.
It's a mess but how can I fix this?
EDIT:
Here is what I have, animalCategory has been changed to chickenCategory for clarity and no contact is detected. Also savior still falls through ground.
self.physicsWorld.contactDelegate = self
var screenTouches = Bool()
let saviorCategory: UInt32 = 0x1 << 0
let chickenCategory: UInt32 = 0x1 << 1
savior.physicsBody?.categoryBitMask = saviorCategory
savior.physicsBody?.contactTestBitMask = chickenCategory
savior.physicsBody?.collisionBitMask = chickenCategory
chicken1.physicsBody?.categoryBitMask = chickenCategory
chicken1.physicsBody?.contactTestBitMask = saviorCategory
chicken1.physicsBody?.collisionBitMask = saviorCategory
func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody
var secondBody : SKPhysicsBody
if contact.bodyA.categoryBitMask == chickenCategory && contact.bodyB.categoryBitMask == saviorCategory {
println("contact made")
savior.hidden = true
}
else if contact.bodyA.categoryBitMask == saviorCategory && contact.bodyB.categoryBitMask == chickenCategory {
println("contact made")
savior.hidden = true
}
}

Ok so it seems like all you want is for just detection that they touched, but you don't want them to push each other. I would in the didBeginContact where ever you put your collision detection between them , make their physicsbody = nil, so that when they collide it knows that they collide and when you put this it makes them go through each other. And if you want to put code to do something else just put that code before you make the physicsbody nil. Also if you want to put back their physicsbody, just put it back.

For anyone who is still stuck on this, if you want to detect that two sprites are touching each other without them actually having a physical effect on each other (i.e. causing movement), then the key is to set the collisionBitMask property of the physicsBody to 0:
node.physicsBody!.collisionBitMask = 0
This means that you will receive events in didBeginContact but the interacting objects will not cause an actual physical effect on each other.

This is how to do it:
Set up your category bit masks:
let saviorCategory: UInt32 = 0x1 << 0
let chickenCategory: UInt32 = 0x1 << 1
let groundCategory: UInt32 = 0x1 << 2
Set up the physics bodies:
func didMove(to: View) {
savior.physicsBody?.categoryBitMask = saviorCategory
savior.physicsBody?.contactTestBitMask = chickenCategory
savior.physicsBody?.collisionBitMask = groundCategory
chicken1.physicsBody?.categoryBitMask = animalCategory
chicken1.physicsBody?.contactTestBitMask = saviorCategory
chicken1.physicsBody?.collisionBitMask = groundCategory
ground.physicsBody?.categoryBitMask = groundCategory
ground.physicsBody?.contactTestBitMask = 0 // No contact detection for ground
ground.physicsBody?.collisionBitMask = UInt32.Max // Everything collides with the ground
physicsWorld.contactDelegate = self
// Rest of didMoveToView
}
Note: It isn't actually necessary to define chicken as contacting saviour and saviour as contacting chicken; you only need to define that one contacts the other, but it can make the code more readable to say that each contacts the other.
Implement didBegin:
func didBeginContact(contact: SKPhysicsContact) {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contactMask {
case chickenCategory | saviourCategory:
print("Collision between chicken and saviour")
let saviour = contact.bodyA.categoryBitMask == saviourCategory ? contact.bodyA.node! : contact.bodyB.node!
saviour.hidden = true
default :
//Some other contact has occurred
print("Some other contact")
}
}
Don't forget to set your class as an SKPhysicsContactDelegate
Check out the examples here:
Attack button in SpriteKit

Related

SKShapeNodes Colliding with my SKPhysics body when I don't want to

I am making a game where I have a ball created as an SKSpriteNode that is colliding with some PhysicsBodies I do not want to. Here is my ball:
orangeBall = OrangeBall(path: ballTrajectory, color: UIColor.orange, borderColor: UIColor.black)
orangeBall.physicsBody = SKPhysicsBody(circleOfRadius: Physics.PhysicsNumbers.ballRadius)
orangeBall.position = Physics.PhysicsNumbers.ballRestPos
orangeBall.physicsBody?.categoryBitMask = CollisionsChecker.Ball
orangeBall.physicsBody?.collisionBitMask = CollisionsChecker.Frame | CollisionsChecker.Box | CollisionsChecker.Score
orangeBall.physicsBody?.contactTestBitMask = CollisionsChecker.Frame | CollisionsChecker.Box | CollisionsChecker.Score
orangeBall.physicsBody?.isDynamic = false
orangeBall.physicsBody?.affectedByGravity = false
addChild(orangeBall)
and I want it to detect that it has been in contact with this score physics body but not actually collide:
score = NetBoxes(path: scoreStationary, color: UIColor.red, borderColor: UIColor.red)
score.position = Physics.PhysicsNumbers.scoreRest
score.physicsBody = SKPhysicsBody(circleOfRadius: 50)
score.physicsBody?.categoryBitMask = CollisionsChecker.Score
score.physicsBody?.contactTestBitMask = 0
score.physicsBody?.collisionBitMask = CollisionsChecker.Ball
score.physicsBody?.affectedByGravity = false
score.physicsBody?.isDynamic = false
addChild(score)
(circleOfRadius is only for testing purposes) Every time I try to check my collisions it seems to not output what I want it to do:
func didBegin(_ contact: SKPhysicsContact){
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if firstBody.categoryBitMask == CollisionsChecker.Ball && secondBody.categoryBitMask == CollisionsChecker.Score || firstBody.categoryBitMask == CollisionsChecker.Score && secondBody.categoryBitMask == CollisionsChecker.Ball{
print("Ball in hoop")
}
Essentially, my orangeBall which is a SKSphapeNode always collide with my Score PhysicsBody. It might also be that both physics bodies around my ball and score get into contact. I am at a loss, any help would be appreciated!
I have figured it out. I was recreating a physicsBody for my orangeBall later in the code. Forming another physicsBody around it making it collide with my other nodes!

Swift - Random collisions are not detected

In my game, I use SKSprite. Some collisions are not detected. I did 10 tries, collisions are working well but about 25% of collisions that are supposed to be detected are not detected. I have NO idea why, I tried many things. Collisions are only with nodes of the same category.
I have no idea why randomly some collisions are not made when I can obviously see them, do you have any idea? Thanks for your help.
Here is the code of didBeginContact:
func didBeginContact(contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody = contact.bodyA
var secondBody: SKPhysicsBody = contact.bodyB
if firstBody.categoryBitMask == secondBody.categoryBitMask {
listContacts.append([firstBody.node!,secondBody.node!])
}
}
}
Here is the code of didEndContact:
func didEndContact(contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody = contact.bodyA
var secondBody: SKPhysicsBody = contact.bodyB
if contact.bodyA.categoryBitMask == contact.bodyB.categoryBitMask {
for i in listContacts{
if (i.contains(firstBody.node!) && i.contains(secondBody.node!)){
let findIndex = listContacts.indexOf { $0 == i }
listContacts.removeFirst(findIndex!)
}
}
}
Finally when I declare a new SKSpriteNode I set this:
rectangle.physicsBody = SKPhysicsBody(rectangleOfSize: rectangle.size)
rectangle.physicsBody?.dynamic = true
rectangle.physicsBody?.collisionBitMask = PhysicsCategory.None
usesPreciseCollisionDetection = true doesn't change anything so I don't use usePrecisionCollisionDetection
Every SKSpriteNode has his categoryBitmask and contactTestBitmask equal because only same SKSpriteNodes are supposed to collide.
Also:
physicsWorld.gravity = CGVectorMake(0, 0)
physicsWorld.contactDelegate = self
Finally here is a short video of my game if you want to understand easily what happens (problem of collisions are between rectangles) https://www.youtube.com/watch?v=-pbmKwQiE9U
You seem to be checking if the categoryBitMask of body A equals body B.
if firstBody.categoryBitMask == secondBody.categoryBitMask {
listContacts.append([firstBody.node!,secondBody.node!])
}
}
This will only run an action if a node hits a node with the same categoryBitMask
You should be checking the collisionBitMasks to see if you have a collision, or you can check the name.
For an example using categoryBitMask you can do something like this:
func didBeginContact(contact: SKPhysicsContact) {
var sprite: SKSpriteNode!
if contact.bodyA.categoryBitMask == <your category bitmask> {
sprite = contact.bodyA.node! as! SKSpriteNode
}
else if contact.bodyB.categoryBitMask == <your category bitmask> {
sprite = contact.bodyB.node! as! SKSpriteNode
}
// Do something with sprite....
// You can also create another sprite, and assign it to the other body, and perform functions on it, or both A and B.
// It is good to have different functions you send can send the nodes to once you find out which ones they are.
}
You can do these checks to see which sprites are hitting each other.
I just fixed it! The reason was that in the function "touchesEnded", I had a recursive function that was deleting bad connections in the listContacts!

SKPhysics not working properly?

struct PhysicsCategory {
static let None : UInt32 = 0
static let All : UInt32 = UInt32.max
static let planet : UInt32 = 0b1
static let rocket : UInt32 = 0b10 }
planet.physicsBody = SKPhysicsBody(circleOfRadius: planet.size.width / 2)
planet.physicsBody?.dynamic = true planet.physicsBody?.categoryBitMask = PhysicsCategory.planet
planet.physicsBody?.contactTestBitMask = PhysicsCategory.rocket
planet.physicsBody?.collisionBitMask = PhysicsCategory.None
func planetCollidedWithRocket(rocket: SKSpriteNode, planet: SKSpriteNode) {
println("collision planet") }
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.planet != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.rocket != 0)) {
planetCollidedWithRocket(firstBody.node as! SKSpriteNode, planet: secondBody.node as! SKSpriteNode)}
I'm currently using this to add collision detection to my game (I've taken only taken out the bits of code I think are relevant). The first time I ran the game on the simulator it worked fine, and I added a game over scene to the planetCollidedWithRocket method. But I tested it again today and for some reason the game is detecting a collision between the rocket and the planet when they aren't colliding! Can anyone see what Ive done wrong??
Your contact isn't set up correctly. This is what you have, and what it translates to:
if((firstBody.categoryBitMask & PhysicsCategory.planet !=0)
// This is checking if the firstBody *has* a categoryBitMask, but it is not evaluating what it is.
// In addition to that, it is requiring that the PhysicsCategory.planet does not equal zero, which it doesn't.
// This would evaluate to true every time, no matter what.
// It is also requiring that...
&& (secondBody.categoryBitMask & PhysicsCategory.rocket != 0))
// This is checking if the secondBody *has* a categoryBitMask, but it is not evaluating what it is.
// In addition to that, it is requiring that the PhysicsCategory.planet does not equal zero, which it doesn't.
// This would evaluate to true every time, no matter what.
What you need to do is check that you categoryBitMask equals one of your PhysicsCategory variables, such that when a planet and a rocket contact, something evaluates to true. Try this:
if ((firstBody.categoryBitMask == PhysicsCategory.planet) &&
(secondBody.categoryBitMask == PhysicsCategory.rocket)){
// Code Here
}
Alternatively, since you know the hard-coded numbers in your PhysicsCategory struct, you can evaluate to hard numbers:
if ((firstBody.categoryBitMask == 1) &&
(secondBody.categoryBitMask == 2)){
// Code Here
}
What this will do is check to see if the physicsBody's categoryBitMask equals certain numbers when they contact. If they do, something happens, if not, nothing happens.

Swift: skaction not executed after contact between nodes is made?

Alright, so I have contact detection set up between 2 nodes - savior and chicken1. This is set up here:
//This is within GameScene class
var screenTouches = Bool()
enum ColliderType:UInt32 {
case Savior = 1
case Chicken1 = 2
}
savior.physicsBody?.categoryBitMask = ColliderType.Savior.toRaw()
savior.physicsBody?.contactTestBitMask = ColliderType.Chicken1.toRaw()
savior.physicsBody?.collisionBitMask = ColliderType.Chicken1.toRaw()
chicken1.physicsBody?.categoryBitMask = ColliderType.Chicken1.toRaw()
chicken1.physicsBody?.contactTestBitMask = ColliderType.Savior.toRaw()
chicken1.physicsBody?.collisionBitMask = ColliderType.Savior.toRaw()
//This is outside of Gamescene class
//Collision detection
func didBeginContact(contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == ColliderType.Savior.toRaw() && contact.bodyB.categoryBitMask == ColliderType.Chicken1.toRaw() ) {
chicken1.hidden = true
let chickenGrabbedLeft = SKAction.moveTo(CGPointMake(self.size.width * 0.1,self.size.height * 1.2), duration:0)
chicken1.runAction(chickenGrabbedLeft)
println("contact made")
} else if (contact.bodyA.categoryBitMask == ColliderType.Chicken1.toRaw() && contact.bodyB.categoryBitMask == ColliderType.Savior.toRaw()) {
chicken1.hidden = true
let chickenGrabbedLeft = SKAction.moveTo(CGPointMake(self.size.width * 0.1,self.size.height * 1.2), duration:0)
chicken1.runAction(chickenGrabbedLeft)
println("contact made")
}
}
When savior comes in contact with chicken1, I need it to look like chicken1 has disappeared. As it is, I have it so that chicken1 becomes hidden when it touches savior, but this isn't enough because savior still collides with it and the user can tell that the object is still there even if it isn't visible.
I don't want to delete chicken1 because I still need it to be present within the game. So I am now trying to get chicken1 to move back to its starting position (which is offscreen) when it touches savior. I did this by placing the SKAction in the above function.
It is not working. When savior touches chicken1, chicken1 still just gets hidden. It doesn't move. What should I do?
From your question, I assume that your function still runs even when chicken1 becomes hidden.
What you can do is to use a BOOL that changes to true when savior comes into contact with chicken1 and use that as a condition before running your action and change it back to false when you want it to no longer interact with those objects.

beginner swift sprite kit - node collision detection help (SKPhysicsContact)

I want a sprite to delete itself when touching another sprite. Right now when they touch, they just push each other.
I have this:
let alphaCategory: UInt32 = 0x1 << 0
let betaCategory: UInt32 = 0x1 << 1
I made the sprites dynamic and not affected by gravity
self.physicsworld.contactDelegate = self
alpha.physicsBody?.categoryBitMask = alphaCategory
alpha.physicsBody?.contactTestBitmask = betaCategory
and
beta.physicsBody?.categoryBitMask = betaCategory
beta.physicsBody?.contactTestBitmask = alphaCategory
I couldn't find anything in swift that made sense to me, but I think the problem is here
func didBeginContact(contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.BodyB
beta.removeFromParent()
}
}
First you should set your firstBody & secondBody to the order of their collisionBitMask:
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask
{
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.categoryBitMask=0 && secondBody.categoryBitMask=1 {
secondBody.removeFromParent()
}
This will prevent your sprites from colliding with anything (including each other). Set this where you set the other BitMask properties:
alpha.physicsBody.collisionBitMask = 0
beta.physicsBody.collisionBitMask = 0
This will actually cause problems as I found out last summer when working on a SpritKit game. If you modify the scene's node structure during any of the SKPhysicsContact callbacks the results are undefined according to Apple. What I was told after filing a bug on this was that you should never modify the scene Node structure from inside any of those callbacks. Instead, Apple says to simply set a flag and then deal with the scene modification in your update loop where it's safe.
Try:
secondBody = contact.bodyB