contact even if not directly on physicsbody

94 views Asked by At

I've got a strange behavior of SKPhysicsbody.

First let me show you my main-node:

self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0, 0)
self.physicsBody = SKPhysicsBody(rectangleOfSize: self.frame.size)
self.physicsBody!.collisionBitMask = player
self.physicsBody!.categoryBitMask = wall
self.physicsBody!.contactTestBitMask = player
self.physicsBody?.dynamic = true
self.name = "background"

Now on top of that, there is another SKSpriteNode without any physicsbody, let's call it path.

After some time, I spawn another node, the playernode on top of that path:

playerNode = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(tileSize/1, tileSize/1))
playerNode.position = CGPointMake(self.frame.width/2, self.frame.height/2)
playerNode.zPosition = 3

playerNode.name = "player"

playerNode.physicsBody = SKPhysicsBody(rectangleOfSize: playerNode.size)
playerNode.physicsBody!.categoryBitMask = player
playerNode.physicsBody!.collisionBitMask = wall
playerNode.physicsBody!.contactTestBitMask = wall
playerNode.physicsBody?.dynamic = false

So in my opinion, the didBeginContact should trigger only if the playerNode is moved outside of the path node and if it really touches the background.

But at the moment, the didBeginContact triggers immediately after I spawn my playerNode on top of my path.

Am I missing something?

1

There are 1 answers

3
hamobi On

Having a little trouble understanding.. but what it sounds like is you want to detect when your playernode exits the position of your path node.

it's triggering a physics contact because your player node is always going to be intersecting your scene's physics body no matter what.

in reality you dont need physics for any of this. in your update method you can just check to see if your player is intersecting your path node (or whichever nodes you care about). If it's not intersecting those, then it's sitting directly in your world, scene, etc.

inside of update:

if !playerNode.intersectsNode(path) {
    // your code here