2

I am trying to create walls in Swift using an SKShapenode. Even with the collision bit mask set to 'All', the characters still move through the rectangle and don't interact with it.

Set up physics categories:

struct PhysicsCategory {
    static let None      : UInt32 = 0
    static let All       : UInt32 = UInt32.max
    static let Monster   : UInt32 = 0b1
    static let Projectile: UInt32 = 0b10
    static let Walls     : UInt32 = 0b11
    static let Player    : UInt32 = 0b100
}

How I tried to set up the walls:

let rectangle = SKShapeNode(rectOfSize: CGSize(width:390, height:200))
rectangle.position = CGPointMake(frame.midX-10, frame.midY + 50)
rectangle.strokeColor = SKColor.blackColor()
rectangle.glowWidth = 1.0

rectangle.physicsBody = SKPhysicsBody(edgeLoopFromRect: rectangle.frame)
rectangle.physicsBody?.dynamic = false
rectangle.physicsBody?.categoryBitMask = PhysicsCategory.Walls
rectangle.physicsBody?.contactTestBitMask = PhysicsCategory.None
rectangle.physicsBody?.collisionBitMask = PhysicsCategory.All
self.addChild(rectangle)
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
nick
  • 45
  • 7

2 Answers2

0

You have to set contactTestBitMask to 'All' not collisionBitMask

rectangle.physicsBody.contactTestBitMask = PhysicsCategory.All
white5tone
  • 166
  • 8
  • 1
    contactTestBitMask is used to check if contact happened, and call the `didBeginContact` method, collisionBitMask is the appropriate one to create walls where objects don't pass through – Pieter Dec 04 '15 at 07:31
0

What are the settings of the characters? Their physicsBody must be set properly too for this to work.

Also, at least one SKNode's physicsBody must be set to dynamic = true, if both have dynamic = false, it won't work.

Pieter
  • 17,435
  • 8
  • 50
  • 89
  • everything else's physics bodies are set to dynamic = true and all of their collision bit masks are set to "Walls". – nick Dec 04 '15 at 15:25
  • @nick A lot of things.... you want me to just randomly guess? I'd suggest you make a minimal project and see if that works, if not post the code, if it does, see where your real project differs and try to find where it goes wrong? – Pieter Dec 07 '15 at 08:30
  • No, I was hoping that you might see that the line "rectangle.physicsBody = SKPhysicsBody(edgeLoopFromRect: rectangle.frame)" is wrong and that you need to use "edgeLoopFromPath" for this to work. – nick Dec 08 '15 at 22:52
  • It does not make sense that edgeLoopFromPath would work where edgeLoopFromRect doesn't, something else must have changed too, or you bumped into a bug. But I'm happy you solved your problem. – Pieter Dec 09 '15 at 07:48