0

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?

Christian
  • 22,585
  • 9
  • 80
  • 106

1 Answers1

0

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
hamobi
  • 7,940
  • 4
  • 35
  • 64
  • The problem is, that my background is always behind my node. I should be able to check if my node touches my background or if there is a node between my player and the background node. – Christian Jun 19 '15 at 19:49
  • You can do that by checking to see if your node is intersecting any other nodes. That's what I described above. – hamobi Jun 19 '15 at 19:51
  • @Christian If memory servers me correctly physics bodies ignore layers, children and zPositions. That means if your path is inside one physics body and you add a new physics body to path it will trigger didBeginContact if path was inside the first physics body. It sounds like you are trying to detect when a player leaves the path. Hamobi is correct you wouldn't need physics to detect that and you could check to see if it still intersects the node or not. – Skyler Lauren Jun 21 '15 at 00:31