1
votes

Desired behaviour

enter image description here

  • The red node (players arm) is connected to the blue one with a pinned joint.
  • The red node has a physicsBody because: 1) it should move if the blue node does and 2) it should collide with the rest of the world but not with the blue shape
  • Vice versa: The blue node (players body) should collide with the rest of the world – but obviously not with the red node.

Problem

Nodes and joint are setup fine (see left), but the physicsBody do interact with each other (right):

enter image description here

Please note: In order to get the joint right I’ve had to add a child node (circle) to the red shape (players arm). So we’re talking about three nodes in total here.

Recent attempt

Tried a bunch of things. Following Apples’ documentation and this thread I learned that this could be solved with the correct combination of categoryBitMask and collisionBitMask.

BitMasks:

struct Category {
    static let none: UInt32 = 0
    static let all: UInt32 = UInt32.max
    static let playerBody: UInt32 = 0b1 // 1
    static let playerArm: UInt32 = 0b10 // 2
    static let regularObject: UInt32 = 0b100 // 4
}

???? Blue Shape:

body.categoryBitMask = Category.playerBody
body.collisionBitMask = Category.all

???? Red Shape (as well as its child node for the joint):

body.categoryBitMask = Category.playerArm
body.collisionBitMask = Category.none

... but this did not solve the problem.

Struggeling for three days in a row now. I know this sort of question got already asked a numerous of times but none of them lead me to the right direction. Really appreciate any advice!

2
Just some guides I've written for this potentially confusing subject: My step-by-step guide for collisions and contacts: stackoverflow.com/a/51041474/1430420 And a guide to collision and contactTest bit masks: stackoverflow.com/a/40596890/1430420 Manipulating bit masks to turn individual collision and contacts off and on. stackoverflow.com/a/46495864/1430420 - Steve Ives

2 Answers

0
votes

This is from Apple documentation: collisionBitMask (but for Scenekit, maybe it is the same in Spritekit)

The value for this key is an NSNumber object containing an NSUInteger value. SceneKit tests for contacts only with physics bodies whose categoryBitMask property overlaps with this bit mask. The default value is all, specifying that searches should test all physics bodies regardless of their category.

so you should not use Category.all

0
votes

Turns out that I’ve unwrapped the wrong physicsBody for the red shape so code changes affected the wrong nodes 🤦

It basically works now the way I’ve described it in the original question.

However—if you’re new to this topic like me: the very best explanation I could find is this one (especially the »ask your self« part). Happy coding everyone!