2

I have two different .scn files with two different parent node with multiple childnodes. I have implemented collision delegates methods and it is getting called correctly. My intention is to find collision only if any child node from second .scn file collides with any child node or root node of first .scn file or vice versa.

But right now since in both the .scn files, all child nodes are closely placed, these delegates are always getting called providing contact.nodeA and contact.nodeB as two child nodes that collide with each other from same .scn file.

Is it possible to detect collision only if contact.nodeA is from different .scn file(can be any child node) and contact.nodeB is from different .scn file(can be any child node)?

Also i may add the node from first or second .scn file multiple times to the same scene. In this case it should detect collision between nodes of these two .scn files even if they are from same .scn file (added two times to scene), since i am adding it two times as a separate object

I have set the categorymask, collisionmask and contact mask as 1 in Xcode scene editor and have set the physics body to kinematic for all the child and root nodes of both the .scn files.

Vidhya Sri
  • 1,773
  • 1
  • 17
  • 46

1 Answers1

4

Every scene should have it's own Category mask (categoryBitMask in code), while its Collision mask (collisionBitMask) and Contact mask (contactTestBitMask) should be equal to the Category mask of the other scene.

Collision mask of a node tells the physics world which categories of nodes (based on Category mask) should it collide with.

Category mask of a node tells the physics world to notify its delegate about the contact with certain categories of nodes.

So in order to achieve what you want (given you want them to actually collide and not just detect that they've had a contact):

Nodes of scene 1:

Category mask = 1
Collision mask = 2
Contact mask = 2

Nodes of scene 2:

Category mask = 2
Collision mask = 1
Contact mask = 1

That way they would only collide with nodes from the other scene and not with their "sisters".

Remember that Category mask should be a power of 2 (2^0 = 1 and 2^1 = 2) so that you are be able to combine them by adding them up.

For example, if you were to add the third scene, it should've had a Category mask = 4 (2^2 = 4):

Nodes of scene 1:

Category mask = 1
Collision mask = 6 [2 + 4] (Category mask of scene 2 and scene 3 added up) 
Contact mask = 6

Nodes of scene 2:

Category mask = 2
Collision mask = 5 [1 + 4] (Category mask of scene 1 and scene 3 added up)
Contact mask = 5

Nodes of scene 3:

Category mask = 4
Collision mask = 3 [1 + 2] (Category mask of scene 1 and scene 2 added up)
Contact mask = 3
Lësha Turkowski
  • 1,361
  • 1
  • 10
  • 21
  • I followed the same procedure as you have explained. I have 6 scenefiles and i have given values from 1, 2, 4, 8, 16, 32 as category masks and 62, 61, 59, 55, 47, 31 as collision and contact masks respectively for nodes of each scenefile. But it is not properly detecting contact between any two scene files. It is detecting contact correctly for 1st and 2nd scene files only. If i add 3rd and 4th to the scene, even if there is no contact it is still saying there is contact between 2nd and 3rd (or) 2nd and 4th? – Vidhya Sri Sep 01 '17 at 08:56
  • @yaali Does 2nd contact with 1st during this? If it does, the delegate methods will be called. Do you explicitly check which scene every node in SCNPhysicsContact belongs to when the method's being called? – Lësha Turkowski Sep 01 '17 at 12:00
  • I checked both Node A and Node B from the delegate methods. It works fine for only 1st and 2nd scene file. But when i place the 3rd one, the contact delegate is getting called and it says there is contact between the 2nd and the 3rd node even when there is no contact and lot of distance between these two. Is the category and contact mask that i set is correct? – Vidhya Sri Sep 05 '17 at 06:22