4
votes

I'm trying to get two physicsBody, that are already in contact with each other (in fact they overlap because physicBody.collisionCategory = 0; for both physicsBody's), to restart/re-register/re-trigger their contact, on command. I've been unable to do this even though I tried to get body1 to move by one pixel within body2 to re-trigger the contact. I've also deleted one of the physicsBody's & re-instantiated it several seconds after that to re-trigger the contact. But nothing works. Is there some sort of method or technique to restart the contact process while both bodies are already in contact?

So, to make things a little clearer:

1) SpriteHuman walks onto SpritePanel.

2) -(void)didBeginContact:(SKPhysicsContact *)contact registers the contact.

3) I call a method that starts step 2 again, while SpriteHuman doesn't move and is still contacting SpritePanel.

1
Have you considered using node.physicsBody.allContactedBodies ? It returns all the physics bodes that are currently in contact with a node's physics body.0x141E
Ah, I see. It returns an array of all the SKPhysicsBodies currently in contact with this one. Pretty nefty. But if I were to use an if statement, such as [self.physicsBody.allContactedBodies.firstObject isEqual:[_oTC.worldNode childNodeWithName:@"strt1N1"]], how would I restart their contact?Krekin
@Krekin You would need to keep checking in your game loop and add a flag to the node for whether it's contacting or not. But keep in mind this is very expensive, so if you do use this solution I would no longer use the contact delegate and instead only use the allContactedBodies. Also have you tried toggling the contactBitMask off and then back on (maybe in the next iteration) or perhaps try setting the physics body to nil and recreating it?Epic Byte
Why do you need to have didBeginContact called continuously while two sprite are touching? What are you trying to accomplish?0x141E
No no. I don't need to have didBeginContact called continuously while two sprites are touching. Only called that one extra time, after they are touching, but at a specific moment. There is a logical error in my code that creates unintentional behaviour amongst NPC's when one of them is contacting the player character, while they are on a path node. Path nodes are invisible SKSpriteNodes that I use as points in my RPG map that NPC's walk to. Due to the way they randomly choose nodes to walk to, the player can interrupt the process upon contact. This is not good. NPC walking gets messed up.Krekin

1 Answers

1
votes

You can set a BOOL property to YES for your player object if it contacts a certain object. This will allow you to continuously run whatever code you need to run. Just remember to do also set the BOOL back to NO when contact is lost.