1

I have situation where I need to create two SKPhysicsBody for a single texture because the collision response need to be different

 let leftBigBox = SKPhysicsBody(rectangleOf :CGSize(width: 490, height: 90) , center:bCenterPoint )

    leftBigBox.contactTestBitMask = category_kitty
    leftBigBox.categoryBitMask = category_train
    leftBigBox.collisionBitMask = category_kitty | category_track

    let rightSmallBox = SKPhysicsBody(rectangleOf :CGSize(width: 100, height: 45) , center:sCenterPoint )
   rightSmallBox.categoryBitMask = category_wagon
   rightSmallBox.collisionBitMask = category_kitty | category_track
   rightSmallBox.contactTestBitMask = category_kitty

    physicsBody=SKPhysicsBody(bodies: [leftBigBox,rightSmallBox])

but didBegin(_ contact: SKPhysicsContact) does not detect when my kitty hits the child node. I can see the debug mode it is happening but the delegate won't print , if I specify the collisionBitMask , contactTestBitMask for the parent physicsBody it works but my goal is to split them into two * I tried using joints and two different texture the performance is so bad

These are the values for kitty

 physicsBody!.categoryBitMask = category_kitty
 physicsBody!.contactTestBitMask = category_wagon | category_border | category_train
 physicsBody!.collisionBitMask =  category_border | category_wagon | category_train
Steve Ives
  • 7,894
  • 3
  • 24
  • 55
merloin
  • 31
  • 3

1 Answers1

1

According to Apple: https://developer.apple.com/documentation/spritekit/skphysicsbody/1519736-init

"The properties on the children, such as mass or friction, are ignored. Only the shapes of the child bodies are used."

This means that the bitMasks you have set up on the 2 individual physics bodies are not retained when you create the new physicsBody via:

physicsBody=SKPhysicsBody(bodies: [leftBigBox,rightSmallBox])

(as KnightOfdragon says, it uses only the shape of the physicsbodies in the array to create the shape of the new physicsBody. You then have to set all the properties of the new physics body - none of the properties from the bodies being joined are used).

So you might be better off either joining the 2 physics bodies with a joint of some sort (if you do not know how to do this, ask a new question) or using some other method to determine which part of your object has bit hit by kitty.

Steve Ives
  • 7,894
  • 3
  • 24
  • 55