2

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!

ixany
  • 5,433
  • 9
  • 41
  • 65
  • Just some guides I've written for this potentially confusing subject:   My step-by-step guide for collisions and contacts: https://stackoverflow.com/a/51041474/1430420   And a guide to collision and contactTest bit masks: https://stackoverflow.com/a/40596890/1430420   Manipulating bit masks to turn individual collision and contacts off and on. https://stackoverflow.com/a/46495864/1430420 – Steve Ives Feb 10 '20 at 12:38

2 Answers2

0

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

Chris
  • 7,579
  • 3
  • 18
  • 38
  • Thank you for your reply – it seems that it behaves differently in SpriteKit. From the Apple Docs: `Every physics body in a scene can be assigned to up to 32 different categories, each corresponding to a bit in the bit mask. You define the mask values used in your game. In conjunction with the collisionBitMask and contactTestBitMask properties, you define which physics bodies interact with each other and when your game is notified of these interactions.` – ixany Feb 08 '20 at 12:05
  • I think you are interpreting this incorrectly - by *default* everything collides with everything, so it doesn't; matter what category and object is, it will collide. That's not the same as saying you shouldn't use categories, because a) You might want to turn off collisions between various object and b) categories are also used for contacts. – Steve Ives Feb 10 '20 at 12:37
0

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!

ixany
  • 5,433
  • 9
  • 41
  • 65