Reputation: 65
I want objects to be able to pass through the bottom and top of the screen but not be able to leave the right and left side. I have this code but when my objects go to the left or right it doesn't act as a wall and lets it go right through it. I used the contactTestBitMask to make sure there was contact but there is no collision. This is the code for the wall on the left:
func wall() {
let leftWall = SKNode()
leftWall.physicsBody = SKPhysicsBody(edgeLoopFromRect:CGRectMake(1.0,
1.0, 1.0, CGRectGetHeight(self.frame)));
leftWall.physicsBody?.categoryBitMask = OutsideCategory
leftWall.physicsBody?.contactTestBitMask = HeroCategory
leftWall.physicsBody?.collisionBitMask = 0
leftWall.physicsBody?.mass = 5
leftWall.physicsBody?.friction = 1.0
leftWall.physicsBody?.restitution = 1
leftWall.physicsBody?.usesPreciseCollisionDetection = true
leftWall.physicsBody?.dynamic = false
leftWall.physicsBody?.affectedByGravity = false
leftWall.position = CGPointMake(0, self.size.height / 40)
leftWall.setScale(1.0)
self.addChild(leftWall)
Upvotes: 1
Views: 1390
Reputation: 3439
As long as the category bit mask is set on your wall, and your walls do in fact exist where you expect them to exist (you can set a flag on the scene to draw physics bodies which will display your wall), and the collisionBitMask of the nodes that should collide with the walls are set to the categoryBitMask of the wall (and you have created world physics body on the scene), you should be good to go.
Example:
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsWorld.contactDelegate = self;
self.physicsBody.categoryBitMask = CNPhysicsCategoryEdge;
_catNode.physicsBody.collisionBitMask = CNPhysicsCategoryBlock | CNPhysicsCategoryEdge | CNPhysicsCategorySpring;
_catNode.physicsBody.contactTestBitMask = CNPhysicsCategoryBed | CNPhysicsCategoryEdge;
The cat sprite node in this example will collide with the world physics body now.
Your collisionBitMask cannot be set to 0. It must match an enum value that coinsides with the categoryBitMask of the other objects. Per apple's documentation:
collisionBitMask
Property A mask that defines which categories of physics bodies can collide with this physics body.
Declaration
SWIFT var collisionBitMask: UInt32 OBJECTIVE-C
@property(nonatomic, assign) uint32_t collisionBitMask
Discussion When
two physics bodies contact each other, a collision may occur. This body’s collision mask is compared to the other body’s category mask by performing a logical AND operation. If the result is a nonzero value, this body is affected by the collision. Each body independently chooses whether it wants to be affected by the other body. For example, you might use this to avoid collision calculations that would make negligible changes to a body’s velocity.
The default value is 0xFFFFFFFF (all bits set).
Example:
CGRect bodyRect = CGRectInset(frame, 2, 2);
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bodyRect.size];
self.physicsBody.categoryBitMask = CNPhysicsCategoryBlock;
self.physicsBody.collisionBitMask = CNPhysicsCategoryBlock | CNPhysicsCategoryCat | CNPhysicsCategoryEdge;
Upvotes: 2