How to detect collision between nodes of two different .scn files in Scenekit- iOS?

1k views Asked by At

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.

1

There are 1 answers

3
Lësha Turkowski On BEST ANSWER

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